Skip to content

More jinja functionality #786

Open
Open

Description

@LecrisUT

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?

Activity

  1. welcome commented on Jun 29, 2023

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

  2. chrisjsewell commented on Jun 30, 2023

    @chrisjsewell
    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

  3. LecrisUT commented on Jun 30, 2023

    @LecrisUT
    Author

    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

  4. chrisjsewell commented on Jun 30, 2023

    @chrisjsewell
    Member

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

  5. LecrisUT commented on Jun 30, 2023

    @LecrisUT
    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?

  6. chrisjsewell commented on Jun 30, 2023

    @chrisjsewell
    Member

    app.env.doc2path

  7. LecrisUT commented on Jun 30, 2023

    @LecrisUT
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions