Skip to content

Commit 6f91c51

Browse files
author
Laurent Franceschetti
committed
Add troubleshooting and debug features
- 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)
1 parent 09f57f6 commit 6f91c51

14 files changed

Lines changed: 476 additions & 31 deletions

File tree

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include README.md
2+
include LICENSE.md
3+
include macros/*.md

README.md

Lines changed: 114 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ markdown-toc -i README.md
2222
* [Standard installation](#standard-installation)
2323
* ["Manual installation"](#manual-installation)
2424
* [Declaration of plugin](#declaration-of-plugin)
25+
* [Check that it works](#check-that-it-works)
2526
- [How to use the macros plugin](#how-to-use-the-macros-plugin)
2627
* [Definitions](#definitions)
2728
* [Defining variables in the configuration file](#defining-variables-in-the-configuration-file)
@@ -43,8 +44,14 @@ markdown-toc -i README.md
4344
* [Using includes](#using-includes)
4445
* [Solving syntax conflicts](#solving-syntax-conflicts)
4546
+ [Issue](#issue)
46-
+ [Solution 1: Explicitly marking the snippets as 'raw'](#solution-1-explicitly-marking-the-snippets-as-raw)
47-
+ [Solution 2: Altering the syntax of jinja2](#solution-2-altering-the-syntax-of-jinja2)
47+
+ [Solution 1: Inline snippets as jinja2 strings](#solution-1-inline-snippets-as-jinja2-strings)
48+
+ [Solution 2: Explicitly marking the snippets as 'raw'](#solution-2-explicitly-marking-the-snippets-as-raw)
49+
+ [Solution 1: Altering the syntax of jinja2](#solution-1-altering-the-syntax-of-jinja2)
50+
* [Troubleshooting](#troubleshooting)
51+
+ [Error Information in case of module error](#error-information-in-case-of-module-error)
52+
+ [`macros_info()` as the go-to tool](#macros_info-as-the-go-to-tool)
53+
+ [Is there some function or variable for information XYZ?](#is-there-some-function-or-variable-for-information-xyz)
54+
+ [How can I get detailed debug information on an object?](#how-can-i-get-detailed-debug-information-on-an-object)
4855

4956
<!-- tocstop -->
5057

@@ -283,6 +290,38 @@ you should also add the `search` plugin.
283290
If no `plugins` entry is set, MkDocs enables `search` by default; but
284291
if you use it, then you have to declare it explicitly.
285292

293+
### Check that it works
294+
The recommended way to check that the plugin works properly is to add the
295+
following command in one of the pages of your site (let's say `info.md`):
296+
297+
```
298+
{{ macros_info() }}
299+
```
300+
301+
In the terminak, restart the environment:
302+
303+
```
304+
> mkdocs serve
305+
````
306+
You will notice that additional information now appears in the terminal:
307+
308+
```
309+
INFO - Building documentation...
310+
[macros] Macros arguments: {'module_name': 'main', 'include_yaml': [], 'j2_block_start_string': '', 'j2_block_end_string': '', 'j2_variable_start_string': '', 'j2_variable_end_string': ''}
311+
Found: Darwin
312+
```
313+
314+
Within the browser (e.g. http://127.0.0.1:8000/info), you should
315+
see a description of the plugins environment:
316+
317+
![macros_info()](macros_info.png)
318+
319+
If you see it that information, you should be all set.
320+
321+
Give a good look at the General List, since it gives you an overview
322+
of what you can do out of the box with the macros plugin.
323+
324+
The other parts give you more detailed information.
286325
287326
## How to use the macros plugin
288327
@@ -642,8 +681,9 @@ Contrary to variables defined in the `extra` section of the `mkdocs.yml` file,
642681
they are accessible only within the specific page.
643682
They are not accessible from the python code.
644683

645-
> If you need reference information on the page
646-
you can use it in the form `{{ page.title }}` and `{{ page.url }}`.
684+
> If you need reference information on the page, there is a page object,
685+
which you could use in the form e.g.: `{{ page.title }}`, `{{ page.url }}`,
686+
`{{ page.is_homepage }}`, etc.
647687

648688
#### Macros and other templating tools
649689
> In fact, you can do
@@ -679,7 +719,7 @@ in your markdown code e.g.:
679719
{% include 'snippet.md' %}
680720
```
681721

682-
Including another markdown file **will** therefore the macros.
722+
Including another markdown file **will** therefore execute the macros.
683723

684724
The root directory for your included files is in
685725
[docs_dir](https://www.mkdocs.org/user-guide/configuration/#docs_dir),
@@ -721,7 +761,26 @@ because the plugin deliberately ignores them.
721761
This is to allow advanced use cases where the content of the code block
722762
can be computed on the fly.
723763

724-
#### Solution 1: Explicitly marking the snippets as 'raw'
764+
> Note that, in principle, there is no risk that jinja2 syntax will
765+
interfere at a later stage, when mkdocs will convert the markdown
766+
into html.
767+
768+
#### Solution 1: Inline snippets as jinja2 strings
769+
This works for simple one-line snippets. Suppose you want to prevent the string
770+
`{{ 2 + 2 }}` from being interpreted. It is sufficient to treat it
771+
as if it was a string in jinja2.
772+
773+
```
774+
{{ "{{ 2 + 2 }}" }}
775+
```
776+
777+
You could also use expressions with double quotes, but in this case use the
778+
simple quotes:
779+
```
780+
{{ '{{ "Hello world" }}' }}
781+
```
782+
783+
#### Solution 2: Explicitly marking the snippets as 'raw'
725784
The prefered solution is to isolate each snippet of code
726785
that should not be interpreted, using the standard jinja2 directive
727786
for that purpose:
@@ -735,7 +794,7 @@ for that purpose:
735794
{% endraw %}
736795
```
737796
738-
#### Solution 2: Altering the syntax of jinja2
797+
#### Solution 1: Altering the syntax of jinja2
739798
Sometimes the use of mkdocs-macros comes late in the chain,
740799
and rather than refactoring all the markdown pages, it may be
741800
preferable to alter the markers for variables or blocks.
@@ -781,4 +840,51 @@ You may of course chose the combination that best suits your needs
781840
accidental combinations of markers may have unpredictable consequences.
782841
**Use with discretion, and at your own risk**.
783842
In case of trouble, please do not expect help from the maintainers
784-
of this plugin.
843+
of this plugin.
844+
845+
### Troubleshooting
846+
#### Error Information in case of module error
847+
In principle a rendering error in a macro will not stop the server, but
848+
display the error in the browser's page (as you would expect, e.g.
849+
with php).
850+
The terminal's running log also displays errors when they occur.
851+
852+
#### `macros_info()` as the go-to tool
853+
Attempting to run the following line in a page:
854+
855+
```
856+
{{ macros_info() }}
857+
```
858+
859+
and restarting the server in the temrinal with `mkdocs serve` will usually give
860+
you a wealth of information within the browser:
861+
862+
- If the information page appears (as e.g. phpinfo() for php),
863+
then you know that the plugin must be working.
864+
- If the page displays and an error message appears, then there
865+
may be a problem with the plugin's installation.
866+
- If the page does not display at all, then the mkdocs server might not
867+
be running or there can be a problem running it.
868+
869+
#### Is there some function or variable for information XYZ?
870+
If you cannot find an answer in this readme,
871+
use `macros_info()` to display the information on all the variables,
872+
functions and filters available in a page.
873+
874+
#### How can I get detailed debug information on an object?
875+
For example, if you want to have more information on the `config` object:
876+
877+
```
878+
{{ context('config') | pretty }}
879+
```
880+
(the `pretty` filter displays the result in a nice table form)
881+
882+
You can use this pattern for pretty much any object, even those
883+
you declared in a module.
884+
885+
When used on its own, `context()` gives the general list of variables
886+
in the plugin's environment:
887+
```
888+
{{ context() | pretty }}
889+
```
890+

0 commit comments

Comments
 (0)