-
Notifications
You must be signed in to change notification settings - Fork 196
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
Comments
Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗 |
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 |
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 |
You might want to have a look at https://pypi.org/project/sphinx-jinja/ |
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 |
|
Thanks a bunch! I'll update the comment above with a short implementation. Not sure how to share it more generally though |
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?The text was updated successfully, but these errors were encountered: