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

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

Closed
lorenzo-rovigatti opened this issue Jun 25, 2023 · 4 comments
Closed

Comments

@lorenzo-rovigatti
Copy link
Contributor

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
Copy link
Member

Could you provide a patch for this?

@lorenzo-rovigatti
Copy link
Contributor Author

lorenzo-rovigatti commented Jun 26, 2023

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
Copy link
Contributor Author

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

@Kwpolska
Copy link
Member

Kwpolska commented Jul 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants