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

Allow pages to opt-in, instead of opt-out. #159

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions mkdocs_macros/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class MacrosPlugin(BasePlugin):
default=DEFAULT_MODULE_NAME)),
('modules', PluginType(list,
default=[])),
# allow default 'ignore' configuration
('ignore_macros', PluginType(bool, default=False)),
# include directory for templates ({% include ....%}):
('include_dir', J2_STRING),
# list of additional yaml files:
Expand Down Expand Up @@ -466,10 +468,15 @@ def render(self, markdown: str):
page_variables = copy(self.variables)
meta_variables = self.variables['page'].meta
# it must be possible to completely
if meta_variables:
if meta_variables and 'ignore_macros' in meta_variables:
ignore_macros = meta_variables.get('ignore_macros')
else:
ignore_macros = self.config['ignore_macros']
if ignore_macros:
# a trick to force of a page NOT to be interpreted,
if meta_variables.get('ignore_macros') == True:
return markdown
return markdown

if meta_variables:
# trace("Metavariables for '%s':" % self.variables['page'].title,
# meta_variables)
page_variables.update(meta_variables)
Expand Down
6 changes: 6 additions & 0 deletions test/opt-in/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
ignore_macros: false
---

{{ macros_info() }}

18 changes: 18 additions & 0 deletions test/opt-in/docs/literal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# In this document, macros are ignored (`ignore_macros: true`)

## Some pre-existing directive (not Jinja2)

`{% Vimeo ID}`


## Offending LaTeX

```LaTeX
\begin{tabular}{|ccc|}
\hline
2 & 9 & 4\\
7 & \multicolumn{2}{c|} {\multirow{2}*{{?}}} \\
6 & &\\
\hline
\end{tabular}
```
14 changes: 14 additions & 0 deletions test/opt-in/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
site_name: Main test site
theme: material
copyright: (C) Laurent Franceschetti 2020

nav:
- Home: index.md
- Not interpreted: literal.md

plugins:
- macros:
ignore_macros: true
# toggle to true if you are in CD/CI environment
on_error_fail: true
verbose: true # toggle to true if you want to macro debug
22 changes: 22 additions & 0 deletions webdoc/docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,28 @@ You may, of course, chose the combination that best suits your needs.
of trouble, please do not expect help from the maintainers of this
plugin.

#### Solution 5: Make the rendering process opt-in, instead of opt-out

_From version x.x.x_
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to show here, please feel free to fixup in your own commit!


Sometimes, only a select few pages need to make use of the Jinja2 statements, or
the introduction of mkdocs-macros is late. To facilitate this, it's possible to
make the rengering process opt-in, instead of opt-out.

For example, place the following parameter in the `macros` section of the config:

- macros:
ignore_macros: true

And then, for the pages which require rendering, add the following to the page's
header section:

```yaml
---
ignore_macros: false
---
```



Including snippets in pages
Expand Down
1 change: 1 addition & 0 deletions webdoc/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ of the MkDocs' config file:
| -- | -- | --
| `module_name` | `main` | [Name of the Python module](python/#local-module) containing macros, filters and variables. Indicate the file or directory, without extension; you may specify a path (e.g. `include/module`). If no `main` module is available, it is ignored.
| `modules` | `[]`| [List of preinstalled Python modules](python/#adding-pre-installed-modules), i.e. listed by `pip list`.
| `ignore_macros` | `False` | [Allow pages to opt-in, instead of opt-out](advanced/#solution-5-make-the-rendering-process-opt-in-instead-of-opt-out)
| `include_dir` | | [Directory for including external files](advanced/#changing-the-directory-of-the-includes)
| `include_yaml`| `[]` | [List of yaml files or `key: filename` pairs to be included](advanced/#including-external-yaml-files)
| `j2_block_start_string` | | [Non-standard Jinja2 marker for start of block](advanced/#solution-3-altering-the-syntax-of-jinja2-for-mkdocs-macros)
Expand Down