trentm/python-markdown2

Markdown2 requires blank lines around Fenced Code blocks to render them as code blocks

mangelozzi opened this issue · 1 comments

I could not find anything specifiying that fence code blocks require blank lines around them to work:

(Github) Fenced code blocks
You can create fenced code blocks by placing triple backticks ``` before and after the code block. We recommend placing a blank line before and after code blocks to make the raw formatting easier to read.

In this snippet:

# Django Templates

## NOTES

- The name should map to the URL.
- No distro or app name prefix, they are namespaced by their dirs already
- Since templates are made in python, the are `named_with_underscores.html` (not web style dashes).

## URL PARAMETERS IN THE TEMPLATE

- All views (except `generic.View`) from `django.forms.generic` inherit from `ContextMixin`
- `ContextMixin` defines the method `get_context_data`:
    ```python
    def get_context_data(self, **kwargs):
        kwargs.setdefault('view', self)
        if self.extra_context is not None:
            kwargs.update(self.extra_context)
        return kwargs
    ```
    So when overriding one must be careful to extends `super`'s `kwargs`:
    ```py
    def get_context_data(self, **kwargs):
        kwargs = super().get_context_data(**kwargs)
        kwargs['page_title'] = "Documentation"
        return kwargs
    ```

rendered as (note the 2 code blocks):
In this snippet:

Django Templates

NOTES

  • The name should map to the URL.
  • No distro or app name prefix, they are namespaced by their dirs already
  • Since templates are made in python, the are named_with_underscores.html (not web style dashes).

URL PARAMETERS IN THE TEMPLATE

  • All views (except generic.View) from django.forms.generic inherit from ContextMixin
  • ContextMixin defines the method get_context_data:
    def get_context_data(self, **kwargs):
        kwargs.setdefault('view', self)
        if self.extra_context is not None:
            kwargs.update(self.extra_context)
        return kwargs
    So when overriding one must be careful to extends super's kwargs:
    def get_context_data(self, **kwargs):
        kwargs = super().get_context_data(**kwargs)
        kwargs['page_title'] = "Documentation"
        return kwargs

Is rendered by markdown2 without the 2 code blocks:
image

I think the problem might be the _fenced_code_block_re not properly recognising indented fenced code blocks. The regexp goes as follows:

_fenced_code_block_re = re.compile(r'''
        (?:\n+|\A\n?|(?<=\n))
        (^`{3,})\s{0,99}?([\w+-]+)?\s{0,99}?\n  # $1 = opening fence (captured for back-referencing), $2 = optional lang
        (.*?)                             # $3 = code block content
        \1[ \t]*\n                      # closing fence
        ''', re.M | re.X | re.S)

I believe that (^`{3,}) matches the start of a line so allowing optional whitespace would probably fix the blocks not being recognized.

_fenced_code_block_re = re.compile(r'''
        (?:\n+|\A\n?|(?<=\n))
        (^[ \t]*`{3,})\s{0,99}?([\w+-]+)?\s{0,99}?\n  # $1 = opening fence (captured for back-referencing), $2 = optional lang
        (.*?)                             # $3 = code block content
        \1[ \t]*\n                      # closing fence
        ''', re.M | re.X | re.S)

And from my testing, this does work but it causes some other problems.
image

Putting the fenced code blocks there causes the list to be broken out of. The text separating the two code blocks is now just some indented text floating about, which get recognised as another code block.

I think the reason the list is broken out of is because the fenced code blocks get swapped out for some md5 hashes like so:

<h2>URL PARAMETERS IN THE TEMPLATE</h2>

<ul>
<li>All views (except <code>md5-e492587862bc07a3e8a1766f3b6cdf9f</code>) from <code>md5-13862f106a0084500eb9518428d220d5</code> inherit from <code>md5-cb5cca48ea3a39d2e5f4f32ed57fe64e</code></li>
<li><code>md5-cb5cca48ea3a39d2e5f4f32ed57fe64e</code> defines the method <code>md5-e7f43ed59796b5da4b301f5cdc8ee0d1</code>:</li>
</ul>

md5-9b355e37bf17872e1d8fa0ef265c0b94


    So when overriding one must be careful to extends `super`'s `kwargs`:


md5-e6db16759f697cd8b6f6220aaa2a9f78

And because they don't match the indentation of the list, they break it.