getnikola / plugins

Extra plugins for Nikola
https://plugins.getnikola.com/
MIT License
56 stars 92 forks source link

[myst] the plugin is incompatible with myst-parser > 0.17.2 #428

Closed lorenzo-rovigatti closed 1 year ago

lorenzo-rovigatti commented 1 year ago

This PR restructured myst's code, breaking this plugin. A quick fix would be to have myst-parser==0.17.2 instead of myst-parser in the requirements.txt file (and update the docs accordingly).

Kwpolska commented 1 year ago

Could you provide a patch for this?

lorenzo-rovigatti commented 1 year ago

I'm not exactly familiar with either myst's internals or nikola plugins, but applying the following patch to myst.py seems to do the trick:

--- myst_old.py 2023-06-26 19:59:23.447605537 +0200
+++ myst.py     2023-06-26 23:41:20.177490223 +0200
@@ -31,7 +31,6 @@

 try:
     import myst_parser
-    import myst_parser.main
 except ImportError:
     myst_parser = None
     nikola_extension = None
@@ -63,7 +62,39 @@
         if not is_two_file:
             _, data = self.split_metadata(data, post, lang)
         new_data, shortcodes = sc.extract_shortcodes(data)
-        output = myst_parser.main.to_html(new_data)
+        
+        # this works for myst-parser versions <= 0.17.2
+        try:
+            from myst_parser.main import to_html
+            output = to_html(new_data)
+        except ImportError:
+            from docutils.core import publish_string
+            from myst_parser.docutils_ import Parser
+            output = publish_string(
+                source=new_data,
+                writer_name="html5",
+                settings_overrides={
+                    "myst_enable_extensions": [ # enable all extension
+                            "amsmath",
+                            "attrs_inline",
+                            "colon_fence",
+                            "deflist",
+                            "dollarmath",
+                            "fieldlist",
+                            "html_admonition",
+                            "html_image",
+                            "linkify",
+                            "replacements",
+                            "smartquotes",
+                            "strikethrough",
+                            "substitution",
+                            "tasklist",
+                        ],
+                    "embed_stylesheet": True,
+                    'output_encoding': 'unicode',
+                },
+                parser=Parser(),
+            )
         output, shortcode_deps = self.site.apply_shortcodes_uuid(
             output, shortcodes, filename=source_path, extra_context={"post": post}
         )

Note that with the "new" myst-parser versions you can also enable some extensions that can come in handy (such as admonitions), but these require to style specific css classes.

lorenzo-rovigatti commented 1 year ago

BTW, If something like this is fine, I can try to improve the code, add some comments and submit a PR.

Kwpolska commented 1 year ago

If the patch works with some test inputs, please make a pull request with it.