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

Use on_env event instead of on_page_read_source() #24

Closed
timvink opened this issue Jun 24, 2020 · 5 comments
Closed

Use on_env event instead of on_page_read_source() #24

timvink opened this issue Jun 24, 2020 · 5 comments

Comments

@timvink
Copy link
Contributor

timvink commented Jun 24, 2020

Hi!

I'm the developer of mkdocs-table-reader-plugin, and I got a PM from a user saying it doesn't work together with mkdocs-markdownextradata-plugin.

table-reader (and other plugins like mkdocs-git-authors-plugin) add jinja tags to the markdown file as well, for example {{ read_csv() }}. Enabling together with markdownextradata results in the following error:

jinja2.exceptions.UndefinedError: 'read_csv' is undefined

You can find a reproducable example in timvink/mkdocs-table-reader-plugin#7

I think the root cause are in these lines:

with open(page.file.abs_src_path, 'r', encoding='utf-8-sig', errors='strict') as f:
md_template = Template(f.read())
return md_template.render(**config.get("extra"))

Have you considered using the on_env() mkdocs event instead of on_page_read_source()? You can use it to alter the jinja2 environment, making all the info from extra: available. It might be cleaner than re-implementing core functionality from MkDocs, it might improve compatiblity with other plugins, and I suspect it will also solve #21.

On my side, I use on_page_markdown() because I initially had trouble getting the custom jinja filter (basically adding functions instead of variables to jinja) to work, but perhaps I should research again. Curious to hear your perspective on this :)

@rosscdh
Copy link
Owner

rosscdh commented Jun 24, 2020 via email

@rosscdh
Copy link
Owner

rosscdh commented Jun 24, 2020 via email

@rosscdh
Copy link
Owner

rosscdh commented Jun 24, 2020

I've reverted and released 0.1.7 https://pypi.org/project/mkdocs-markdownextradata-plugin/
Thanks for getting involved! Happy to discuss the suggested option when I get more time

@rosscdh rosscdh closed this as completed Jun 24, 2020
@timvink
Copy link
Contributor Author

timvink commented Jun 25, 2020

Wow, that was quick, thanks! And it solves the compatibility issue (unit tests now passing: https://github.com/timvink/mkdocs-table-reader-plugin/actions/runs/147141623)

On second thought, I'm not sure if on_env() is the best way to go, I can't get it to work. For reference, here's my attempt:

# Add a {{ foo }} tag to a markdown page
# And following methods to your plugin
def on_env(self, env, **kwargs):
    """
    Modify the jinja2 environment. 
    Docs: https://jinja.palletsprojects.com/en/2.11.x/api/#jinja2.Environment
    """
    
    env.globals['foo'] = "bar_from_on_env"
    return env

def on_template_context(self, context, **kwargs):
    
    context["foo"] = "bar_from_template_context"
    return context

def on_page_context(self, context, **kwargs):
    
    context["foo"] = "bar_from_page_context"
    return context

Feel free to close the issue.

@rosscdh
Copy link
Owner

rosscdh commented Jun 25, 2020 via email

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

No branches or pull requests

2 participants