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

Support Markdown fence syntax #281

Open
chrimaho opened this issue Sep 4, 2023 · 2 comments · May be fixed by #282 or #274
Open

Support Markdown fence syntax #281

chrimaho opened this issue Sep 4, 2023 · 2 comments · May be fixed by #282 or #274

Comments

@chrimaho
Copy link

chrimaho commented Sep 4, 2023

Description

When I'm writing docs, I frequently add code blocks like this:

def func(var1):
    """
    My docstrings

    ```py
    ...
    ```
    """
    return var1

Or like this:

def func(var1):
    """
    My docstrings

    ```python
    ...
    ```
    """
    return var1

I can see that both of these code blocks are supported in your package, as defined in this line:

PYGMENTS_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy"))

However, I'd like to extend this functionality to include two other identifiers for the Pygment langs:

  • ".py"
  • ".python"

The reason why is because in the pymdown-extensions package, it has some functionality called Super Fences: SuperFences. Here, it enables you to define some code blocks which allow you to inject some special syntax to generate html code with additional classes, id's, titles, line nums, etc.

Here's an example:

def func(var1):
    """
    My docstring

    ```{.python .extra-html-class linenums="1"}
    ...
    ```
    """
    return var1

Or like this:

def func(var1):
    """
    My docstring

    ```{.py .extra-html-class linenums="1"}
    ...
    ```
    """
    return var1

You'll notice, also, that in order to use the functionality from the PyMdown-Extensions package, we also need to use curly brackets { and }.

When I use the syntax like this, then my docs are not automatically 'blackened' thanks to your excellent package.

Can you please extend your package to handle this functionality also?

I would recommend adjusting this line:

PYGMENTS_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy"))

To be like this:

PYGMENTS_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy", ".py", ".python"))

And also adjusting this line:

r"(?P<before>^(?P<indent> *)```\s*" + PYGMENTS_PY_LANGS_RE_FRAGMENT + r"( .*?)?\n)"

To be like this:

    r"(?P<before>^(?P<indent> *)```\s*\{?" + PYGMENTS_PY_LANGS_RE_FRAGMENT + r"( .*?)?\}?\n)"

I hope that helps?

Thanks 👍

@adamchainz adamchainz changed the title Handle code chunks with .py and .python Support Markdown superfences syntax Jul 30, 2024
@adamchainz adamchainz linked a pull request Jul 30, 2024 that will close this issue
1 task
@adamchainz
Copy link
Owner

#357 was a fresh dupe from @maxschulz-COL. If anyone wants to rebase #274 and finish it off (docs, changelog, more tests), that would get this moving.

@maartenbreddels
Copy link

Note that this is not 'superfaces' syntax, it's regular markdown for Python:
https://python-markdown.github.io/extensions/fenced_code_blocks/

From the docs:

So long as the language is the only option specified, the curly brackets and/or the dot may be excluded:

Superfences are about other things, like nesting and custom fences

@adamchainz adamchainz changed the title Support Markdown superfences syntax Support Markdown fence syntax Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants