Skip to content

Spurious newline in generated literal_block nodes #407

Open

Description

@cpitclaudel

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

Activity

  1. added
    bug
    on Jul 27, 2021
  2. welcome commented on Jul 27, 2021

    @welcome

    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! 🎉

  3. cpitclaudel commented on Mar 31, 2022

    @cpitclaudel
    ContributorAuthor

    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>
    
  4. cpitclaudel commented on Mar 31, 2022

    @cpitclaudel
    ContributorAuthor

    The problem is due to this code in docutils:

        # add newline
        def depart_literal_block(self, node):
            self.body.append('\n</pre>\n')
    
  5. added 2 commits that reference this issue on Apr 1, 2022
    135e77f
    6cd61a5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions