executablebooks / MyST-Parser

An extended commonmark compliant parser, with bridges to docutils/sphinx
https://myst-parser.readthedocs.io
MIT License
736 stars 194 forks source link

More jinja functionality #786

Open LecrisUT opened 1 year ago

LecrisUT commented 1 year ago

Describe the feature you'd like to request

Currently only substitution works for direct expansion of variables. It would be nice to have support for {% for %} directives and so on.

Describe the solution you'd like

No response

Describe alternatives you've considered

I've tried simply parsing the files as jinja templates, but it fails at directives like :::{include} because of curly brackets. How does myst get arround this?

welcome[bot] commented 1 year ago

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
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! :tada:

chrisjsewell commented 1 year ago

I've tried simply parsing the files as jinja templates

Heya, to note, myst-parser does not use jinja to actually parse the file, but only for expression evaluation.

Parsing is done with a plugin to the Markdown parser, e.g.

from markdown_it import MarkdownIt
from markdown_it.tree import SyntaxTreeNode
from mdit_py_plugins.substitution import substitution_plugin

md = MarkdownIt().use(substitution_plugin)
tree = SyntaxTreeNode(md.parse(
    "An {{ inline_expr }}\n\n"
    "{{ block_expr }}"
))
print(tree.pretty())

gives

<root>
  <paragraph>
    <inline>
      <text>
      <substitution_inline text='inline_expr'>
  <substitution_block text='block_expr'>

expressions are then evaluated in a post-processing step

LecrisUT commented 1 year ago

Ok, it seems that the error I encountered was not on that side, and indeed I am able to hack in the jinja rendered with:

def jinja_render(app: Sphinx, docname: str, source: list[str]) -> None:
    """
    Render pages as jinja templates

    :param app: Sphinx app
    :param docname: Name of the doc file
    :param source: Single element list with the source as first element
    """
    if app.builder.format != 'html':
        # Only parsing html for now
        return
    file = Path(app.env.doc2path(docname, base=False))
    if ".j2" not in file.suffixes:
        # If the file is not explicitly a jinja template do not parse it
        return
    src = source[0]
    rendered = app.builder.templates.render_string(src, app.config.html_context)
    source[0] = rendered

def setup(app: Sphinx):
    app.connect("source-read", jinja_render)

Only issue there is I can't extract the filename so that id doesn't try to render non-jinja file. If you have a clue, that would be wonderful

chrisjsewell commented 1 year ago

You might want to have a look at https://pypi.org/project/sphinx-jinja/

LecrisUT commented 1 year ago

I did check that one out, and it uses directives. I want this one to be for the whole file. I've used this one for inspiration. Do you know how to extract the filename in general from app object?

chrisjsewell commented 1 year ago

app.env.doc2path

LecrisUT commented 1 year ago

Thanks a bunch! I'll update the comment above with a short implementation. Not sure how to share it more generally though