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

fix for #215: Fix title and nav parsing #217

Closed
wants to merge 243 commits into from

Conversation

AnvithLobo
Copy link

  • allow macros in meta variables
  • allow automatic parsing of title by mkdocs as explicitly assigning page.title will override that.
  • Fix macro parsing in nested nav Sections
  • Be unopinionated and let just parse macros. Let Mkdocs handle title priority

Laurent Franceschetti and others added 30 commits June 27, 2018 07:17
  - 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
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)
```
  - 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.
fralau and others added 19 commits November 28, 2023 21:01
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
  - 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
- allow macros in meta variables
- allow automatic parsing of title by mkdocs as explicitly  assigning page.title will override that.
@fralau fralau linked an issue Mar 6, 2024 that may be closed by this pull request
@fralau fralau added the bug Something isn't working label Mar 6, 2024
@fralau
Copy link
Owner

fralau commented Mar 10, 2024

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:

  1. I see only processing of variables, in the rendering. What about expressions containing Jinja2 macros and filters?
  2. Do you have a theory of why it would be sufficient to intervene at the stage of on_nav, and it is no longer necessary at on_page_markdown? What about the processing of page variables (e.g. defined in the page's YAML front-matter)?

@@ -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,
Copy link
Owner

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?

Copy link
Author

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.

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.

# 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.


# only update the title if it is not empty
if nav.title:
nav.title = self.env.from_string(nav.title).render(**self.variables)
Copy link
Owner

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).

@AnvithLobo
Copy link
Author

2. Do you have a theory of why it would be sufficient to intervene at the stage of on_nav, and it is no longer necessary at on_page_markdown? What about the processing of page variables (e.g. defined in the page's YAML front-matter)?

This is only for the nav items. Trying to render nav items from on_page_markdown is not ideal. As we would have to get the page and it's current nav then walk up the tree to find the first parent and then again walk down the tree all the way down to render the nav items.
First this wouldn't be elegant nor efficient. As we would need to do this for every page.
But If we only consider the last parent and try to render it's child. We will miss rendering any empty sections with no pages currently linked to that section.

The only disadvantage of rendering Nav items on on_nav instead of on_page_markdown is that we will miss out on any meta variables defined in the page. But this is more trouble than it is worth solving IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Icons / Emoji fail to render in header with Macros enabled