Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spurious newline in generated literal_block nodes #407

Open
cpitclaudel opened this issue Jul 27, 2021 · 3 comments · May be fixed by #541
Open

Spurious newline in generated literal_block nodes #407

cpitclaudel opened this issue Jul 27, 2021 · 3 comments · May be fixed by #541
Labels
bug Something isn't working

Comments

@cpitclaudel
Copy link
Contributor

cpitclaudel commented Jul 27, 2021

Describe the problem

The result of main.to_docutils include an extra newline in each literal_block node.

Link to your repository or website

No response

Steps to reproduce

Script:

from myst_parser.main import to_docutils
from docutils.core import publish_doctree

doc = to_docutils("```\ncode\n```\n")
print(doc, end="\n\n")

ref = publish_doctree("::\n\n   code\n\n")
print(ref, end="\n\n")

from docutils.core import publish_from_doctree
print(publish_from_doctree(doc, writer_name="html4css1", settings_overrides={"embed_stylesheet": False}).decode("utf-8"), end="\n\n")
print(publish_from_doctree(ref, writer_name="html4css1", settings_overrides={"embed_stylesheet": False}).decode("utf-8"), end="\n\n")

Output:

<document source="notset"><literal_block language="" xml:space="preserve">code
</literal_block></document>

<document source="<string>"><literal_block xml:space="preserve">code</literal_block></document>

…
<body>
<div class="document">


<pre class="literal-block">
code

</pre>
</div>
</body>
</html>

…
<body>
<div class="document">


<pre class="literal-block">
code
</pre>
</div>
</body>
</html>

Notice the extra newlines in the output of MyST (compared to the expected docutils output ref)

The version of Python you're using

3.8

Versions of your packages

MyST: 5fd4c51

@cpitclaudel cpitclaudel added the bug Something isn't working label Jul 27, 2021
@welcome
Copy link

welcome bot commented Jul 27, 2021

Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.

If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).

Welcome to the EBP community! 🎉

@cpitclaudel
Copy link
Contributor Author

This issue is also present in the new myst-docutils-html command line tool. myst-docutils-html5 doesn't have that issue, nor does the native myst to html pipeline.

Input file:

SOME TEXT

```
SOME CODE
```

SOME TEXT

HTML4 output (notice the unwanted blank line):

<body>
<div class="document">


<p>SOME TEXT</p>
<pre class="code literal-block">
SOME CODE

</pre>
<p>SOME TEXT</p>
</div>
</body>

HTML5 output:

<body>
<main>


<p>SOME TEXT</p>
<pre class="code literal-block"><code>SOME CODE
</code></pre>
<p>SOME TEXT</p>
</main>
</body>

Native MyST output:

import pathlib, myst_parser.main
print(myst_parser.main.to_html(pathlib.Path("test.md").read_text()))

<p>SOME TEXT</p>
<pre><code>SOME CODE
</code></pre>
<p>SOME 
<p>SOME TEXT</p>
<pre><code>SOME CODE
</code></pre>
<p>SOME TEXT</p>TEXT</p>

@cpitclaudel
Copy link
Contributor Author

The problem is due to this code in docutils:

    # add newline
    def depart_literal_block(self, node):
        self.body.append('\n</pre>\n')

cpitclaudel added a commit to cpitclaudel/MyST-Parser that referenced this issue Apr 1, 2022
cpitclaudel added a commit to cpitclaudel/MyST-Parser that referenced this issue Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant