Replies: 2 comments 1 reply
-
I think it's an excellent question and it requires to take a step back. Rather than answering it in your specific case, I will provide a general answer (and let you work it out according to your own judgement and preferences). The tools in the toolboxWhen you work with different tools at the same time, the first step to get a crystal clear picture of what each tool is for (with its strengths and weaknesses) and what essential role it is playing in the ecosystem. So its important to differentiate them, roughly:
How Mkdocs-Macros expands the toolboxMkdocs-Macros expand this toolbox by adding the power of jinja2 templates. So here would be the guiding principles, if want to you wish to extract maximum power from Mkdocs-Macros:
jinja2 versus Python macros?TLDR: write your macros in Python. The long answer: Which you chose is, really, whichever you prefer, since they do the same. However, here is my viewpoint about it, i.e. the philosophy behind Mkdocs-Macros. I suppose that the designers of Jinja2 added the possibility of defining jinja2 macros inside templates as a convenience solution, an extra flexibility; that's because writing a Python function (loaded when booting the jinja2 environment) would imply a change to the project's "hard-code"; and depending on how the code or project is organized, it can be too much work. The syntax of jinja2 macros works and is actually quite elegant, so it could be a time saver. But let's be honest: compared to Python, that syntax is fussy. But with Mkdocs-Macros, the operating principle has been turned on its head, with a little white magic in the plugin (dynamic loading of Python code). A key aim of the project was to make it super-easy to write a Python module to define macros (and filters) specifically for a website. This was born out of my frustration with wikis like Confluence, which turned writing macros into an adventure into Javassic Park, complete with skeletons and Velocity raptors. I just wanted to express: "here is my function" with a Allowing a programmer to quickly write macros in Python on a "per-website" basis, was the whole point of this plugin. More on it on the About page of the project. |
Beta Was this translation helpful? Give feedback.
-
Bottom line: You can write a macro that does everything, but it would be a pity not to use the power of templating.
|
Beta Was this translation helpful? Give feedback.
-
I want to improve the documentation of a badge provider by having the pages listing these badges be auto-generated.
The files exist outside the docs folder and are structured like this:
.../
means there are multiple folders with various names here.<name>
can be any name the file has.<height>
is a numberI now considered making a macro that would allow me to list images from one of the folders relative to
assets/
with each sub-folder being used as a header on the page.The listed content should contain a preview of the image(s) and an example on how to include it through HTML or Markdown.
Also, images of the same
<name>
should be grouped together.To explain it a bit better, imagine this setup:
In
compact.md
I would include the macro{{ gen_list("compact") }}
and it would generate a page containing something similar to this:I really hope what I explain here makes sense, because I honestly have a hard time explaining it...
I'm not sure if it would be better making the macro in Python or having it as a jinja2 macro and use that... Or if there is a better alternative that doesn't even use Macros or smth...
I'm fine with the content being in HTML rather than markdown, if that is unavoidable...
Beta Was this translation helpful? Give feedback.
All reactions