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

More jinja functionality #786

Open
LecrisUT opened this issue Jun 29, 2023 · 7 comments
Open

More jinja functionality #786

LecrisUT opened this issue Jun 29, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@LecrisUT
Copy link

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?

@LecrisUT LecrisUT added the enhancement New feature or request label Jun 29, 2023
@welcome
Copy link

welcome bot commented Jun 29, 2023

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

@chrisjsewell
Copy link
Member

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
Copy link
Author

LecrisUT commented Jun 30, 2023

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
Copy link
Member

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

@LecrisUT
Copy link
Author

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
Copy link
Member

app.env.doc2path

@LecrisUT
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants