-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Using mkdocs-table-reader-plugin as a module/pluglet for Mkdocs-Macros? #57
Comments
Great package, Your example is actually really clean! I used regex and some obscure logic to safely parse the args and kwargs from the matched group. I benchmarked it, and the overhead is very small. I do recognize the problem of interfering jinja2 environments, hence this ugly bit to raise a warning when mkdocs-table-reader-plugin/mkdocs_table_reader_plugin/plugin.py Lines 37 to 40 in 165af58
I have seen your concept of pluglets. To be honest, I don't like depending on
Here's another idea. To improve compatible with
We might be able to use |
ContextYour points about not wishing to rely on Mkdocs-Macros are well received (they make sense, and I defend freedom of choice for the members of the ecosystem). Having developed a custom parser is actually an advantage for your plugin, since it is not perturbed by the presence of other similar syntax (hence it can be run before Mkdocs-Macros). I understand a solution based on that kind of tolerance would be self-evident. IssueUnfortunately, it's easier said than done for Jinja2, bacause (at least to my knowledge) it structurally cannot do that. Any two plugins that rely on Jinja2 are plainly incompatible among themselves, because whichever runs first will be thrown off by the macros declared in the other. Once code operates within Jinja2, it is no longer dealing with the full calling string, but with the function SolutionThe only solution I see for the moment, would be pre-processing, by some other piece of software, which would replace the strings to be ignored ( Other pointsRegardless of that issue, your idea of "registering" macros with Mkdocs-Macros, would mean to agree on a simple data structure (let's assume a plain list, to start with) which we could call, e.g. E.g.: The idea of relying on |
I opened a discussion on Jinja2's repository: pallets/jinja#1943 |
Note that if you need a quick fix solution to guarantee that MkDocs-Macros won't break the call any of your plugin's functions, it would be sufficient to write: {% raw %}
{{ read_csv('cities.csv')}}
{% endraw %} I realize it's a little verbose, but it could also envelop a section of a page, or even a full page. |
Thanks for explaining so clearly, I understand the issue with two jinja2 templating engines. Indeed the proposal for a 'tolerant mode' (that you bring up in pallets/jinja#1943) could work.. it would leave missing macros untouched. The downside of course is that now you're never sure if your input macros are valid; which is what I have in this plugin. for example Coming back to the idea of using
It's nice, because now you always have interop, users get notified of typos, and mkdocs-macros does not need to rely on the (quite heavy) pandas dependency that table-reader has. We might even be able to generalize this further, as I have more plugins that use regex to replace jinja2 plugins. If we agree on exposing a Thoughts? |
@timvink It's a brilliant course of thought! A question to solve, would be the human coordination. I believ it would be more practical if the original writer of the plugin wrote their own pluglet, since it's simple to do, and they know how it works and how that want the pluglet to behave (especially in the corner cases, etc.). Plus there is only one of me 🙂 . For the sake of simplicity (of development and maintenance), don't you think it would be better (rather to manage a coexistence) to simply cause a graceful failure of MkDoc's serve or build process, with a recommendation to install the corresponding pluglet? The pluglet's installation would consists of installing the pluglet (with MkDocs-Macros can implement that graceful failure; but I would prefer not to create a precedent where MkDocs-Macros is one-sidedly discriminating against other plugins. Perhaps we could do it in a coordinated way 🤔 : MkDocs-Macros could register, in an equivalence file, the plugins and equivalent replacement pluglets? In that case, I could fairly easily implement the change (once) in MkDocs-Macros. And then it would be MkDocs-Macros that fails in case of a collision with an incompatible plugin, with the recommendation for the user to update their own config file with the pluglet. You wouldn't have to change your plugin's code; only to provide the pluglet (a short piece of code) and let me know so that I can update the equivalence file. What do you think? |
I was looking back at this issue. Is there still some interest in it? |
I had to revisit this as I wanted to combine I managed to get it working, see #68. The core solution was this: mkdocs-table-reader-plugin/mkdocs_table_reader_plugin/plugin.py Lines 51 to 57 in 2cdc786
Now.. if you have A second tricky part I solved was related to the search paths. A jinja2 macro is of course not aware of the source file page it is currently run in, or the docs_dir or config_dir for that matter. Solved that using an additional mkdocs event combined with a decorator that has the plugin config in it's state. The good news: we now have backwards-compatible support for the # index.md
{% set table_names = ["basic_table.csv","basic_table2.csv"] %}
{% for table_name in table_names %}
{ { read_csv(table_name) }}
{% endfor %}
Will release to pypi soon |
Now released: https://pypi.org/project/mkdocs-table-reader-plugin/ |
See also: squidfunk/mkdocs-material#5933 @timvink Interestingly, it should auto-document if you type |
@timvink I am the developer of Mkdocs-Macros. I am contacting you since I noticed that you have given thought to the production of reproducible reports.
I noticed that several MkDocs plugins operate with the idea of using a "macro" style notation inside a Markdown page, e.g.:
So my idea was to put together a generalized plugin that would provide a powerful reference syntax (Jinja2), as well as all the boilerplate (template engine, error management, etc., while leaving some flexibility). The developer would only have to focus on developing the Python functions (which would then become macros within the Markdown page). It works and the plugin is used by many Mkdocs projects (more than 2'500, according to Github).
To facilitate that migration effort, I implemented the concept of pluglets, which are installable as any Python package. Basically, any plugin that uses the concept of macros (like this table reader) could be rewritten by their author. But of course, that would be a matter of time, effort versus return on investment, etc.
However, there is a "transient" issue because Mkdocs-Macros and the other existing plugins that use a similar syntax might interfere with each other (depending on in which order they are declared and so on). This is a question that comes up a lot.
So, in the mean time, what if we used the code of an existing plugin in Mkdocs Macros?
To get an idea of how this idea could work in practice, I tried the experiment of writing a module that would import the applicable code of
mkdocs-table-reader-plugin
. It was very straightforward.Here is the minimal code for a module (
main.py
in the root directory of the MkDocs project):It does the following: it lists all the readers, which are conveniently listed in
READERS
(['read_csv', 'read_table', 'read_fwf', 'read_excel', 'read_yaml', 'read_json', 'read_feather', 'read_raw']
) and declares them as macros. And voilà they were imported as macros available in every markdown page! Now its even possible to add programming logic (for repetitive actions), etc..I attached the whole Mkdocs test project (mkdocs-tables-module.zip), so that you could reproduce my experiment, if you wish. It only requires a
pip-install mkdocs-macros-plugin
.I just wished to know what you think about it, and if there could be potentially workable ideas there?
The text was updated successfully, but these errors were encountered: