-
Notifications
You must be signed in to change notification settings - Fork 51
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
fix for #215: Fix title and nav parsing #217
Conversation
Support Jinja includes
- document include function - clarify access to variables dictionary
- load_variables() becomes load_module() - added comments
- all functionality is concentrated into plugin.py (MacrosPlugin class) - introduction of define_env() function, more general than declarare_variables() - improve documentation - added conf property to the env object: it makes the whole config file accessible to the Python module - improved trace of the plugin in the build/serve phase (in color)
- argument macro -> include_yaml for the list of files - module_name is also defined under argument_macro - created a util.py module de declutter plugin.py - the sample is called main_sample.py
- document inclusion of yaml files - other improvements - corrected bug in ImportError message, in case of missing module
Update README.md
Otherwise I cannot load a custom module ("ModuleNotFoundError: No module named")
- now each page can access `{{ page.title }}` and `{{ page.url }}`
- function `context()` to analyse an object with a `pretty` filter (provides a table) - function `macros_info() for general documentation/debug (provides paragraphs and tables) - error in a module or template is now reflected within the page (does not crash mkdocs)
Use the absolute path for project_dir
mkdocs.utils no longer contains string_types (mkdocs.utils) This should provide a smooth fallback. My failed builds: ``` ImportError: cannot import name 'string_types' from 'mkdocs.utils' (/usr/local/lib/python3.7/site-packages/mkdocs/utils/__init__.py) ```
Compatible to mkdocs 1.1
- that was an mkdocs artifact to ensure compatibility with Python 2.7. Since support is removed in mkdocs 1.1, we also remove it.
- Some users may want to separate the files to be included (with {% include 'foo.md' %} from the usual doc directory so as to prevent those partials to be rendered. This can be done with the `include_dir` parameter in the yaml config ile.
- if an `include_dir` argument is used to specify a different directory for markdown files to be included, that directory will be watched.
Add example of escaping Handlebars definitions with `{% raw %}`
- clarification of examples in first page - fix all relative links, following clarification of link management in MkDocs
- The Jinja2 engine used for HTML templates is the standard one of MkDocs, and distinct from the macros engine.
Add link to wiki for pluglets
- Now it is possible to specify to additional standard jinja2 parameters, e.g. j2_comment_start_string: '[#' j2_comment_end_string: '#]' - Updated documentation - Updated `new_syntax` use case
Fix typos and formatting
- this is a mechanism that forces rendering of files for the case when `render_by_default` is set to `false`. - it uses a git-like syntax to define files to include
- `render_macros` in the YAML header has the last word - update `opt-in` test case - update documentation
- to prevent a problem in case pymdownx.emoji is being used
Files were missing
- allow macros in meta variables - allow automatic parsing of title by mkdocs as explicitly assigning page.title will override that.
- In the Advanced Usage page
Thank you for solving the conflicts. I looked at your solution and I see elegance in it (modifications are well-localized). That is definitely an advantage. What will matter, in final analysis, is whether it solves the issue or not, without creating new ones. I have two objections at this stage:
|
mkdocs_macros/plugin.py
Outdated
@@ -571,6 +570,13 @@ def render(self, markdown: str, force_rendering:bool=False): | |||
# expand the template | |||
on_error_fail = self.config['on_error_fail'] | |||
try: | |||
# If title meta variable is present, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see only processing of variables. What about macros and filters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just mimics the current behavior of the existing jinja2 rendering code for the page.
mkdocs-macros-plugin/mkdocs_macros/plugin.py
Lines 580 to 583 in f97565f
md_template = self.env.from_string(markdown) | |
# Execute the jinja2 template and return | |
return md_template.render(**page_variables) | |
Unless I misread the code the on_config
method should load all the filters and macros to self.env
and self.variables
right? and page_variables
in render
is just a copy of self.variables
. So all macros and filters should work fine like usual.
mkdocs-macros-plugin/mkdocs_macros/plugin.py
Lines 696 to 716 in f97565f
# finally build the environment: | |
self.env = Environment(**env_config) | |
# ------------------- | |
# Process macros | |
# ------------------- | |
# reference all macros | |
self.variables['macros'] = copy(self.macros) | |
# add the macros to the environment's global (not to the template!) | |
self.env.globals.update(self.macros) | |
# ------------------- | |
# Process filters | |
# ------------------- | |
# reference all filters, for doc [these are copies, so no black magic] | |
# NOTE: self.variables is reflected in the list of variables | |
# in the jinja2 environment (same object) | |
self.variables['filters'] = copy(self.filters) | |
self.variables['filters_builtin'] = copy(self.env.filters) | |
# update environment with the custom filters: | |
self.env.filters.update(self.filters) |
Let me know if i'm wrong. I'll look into it a bit more then.
mkdocs_macros/plugin.py
Outdated
|
||
# only update the title if it is not empty | ||
if nav.title: | ||
nav.title = self.env.from_string(nav.title).render(**self.variables) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same remark here (variables and not filters and macros).
This is only for the nav items. Trying to render nav items from The only disadvantage of rendering Nav items on |
Section
s