From 47c005b8523437ff28001c8c47a6360f985c2a28 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 2 Jan 2022 17:29:27 +0100 Subject: [PATCH] pelican-theme, m.code, m.dot: make stuff finally work on Pelican 4.5.1+. Since Pelican 4.5 moved to "namespace plugins", the way plugins are loaded is different and thus the root plugins/ directory is not in PATH anymore, leading to errors like No module named 'ansilexer' No module named 'latex2svg' After spending a bit of time looking into how "namespace plugins" are, I decided to stay with what they say "legacy plugins" because that doesn't require me to move everything into a pelican.plugins namespace and thus allows me to reuse the exact same file for plugins to other m.css tools like the Python doc generator. Version 4.5.0 had loading of namespaced plugins (the `m.` here) broken completely, which is why the CI got pinned to 4.2. With 4.5.1 it started working again and due to how the tests were executed the PATH issues weren't hit either, leading me to a false sense of security that everything works again on 4.5.1, while it wasn't. This fix is the final piece to make everything work again. Sorry that it took over a year to get in. Co-authored-by: Lukas Pirl --- package/ci/circleci.yml | 4 +--- pelican-theme/test/test_page.py | 4 +++- plugins/m/code.py | 24 +++++++++++++++++++++++- plugins/m/dot.py | 11 ++++++++++- plugins/m/math.py | 14 ++++++++++++-- 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/package/ci/circleci.yml b/package/ci/circleci.yml index eba0b03a..e2605188 100644 --- a/package/ci/circleci.yml +++ b/package/ci/circleci.yml @@ -48,8 +48,6 @@ commands: steps: - run: name: Install Python dependencies - # Everything broken with Pelican 4.5, stay on older version until - # that's resolved: https://github.com/mosra/m.css/issues/178 # Pyphen 0.10 has significantly different hyphenation results, staying # on an older version until I can investigate # Matplotlib 3.4.1 has different output AGAIN, staying on something @@ -58,7 +56,7 @@ commands: # Attrs 20.3 add some new properties that I need to ignore first, using # 19.3 instead command: | - pip install jinja2 pelican==4.2.0 Pyphen==0.9.5 Pillow coverage codecov qrcode matplotlib<< parameters.matplotlib-version >> << parameters.extra >> + pip install jinja2 pelican Pyphen==0.9.5 Pillow coverage codecov qrcode matplotlib<< parameters.matplotlib-version >> << parameters.extra >> - run: name: Fix unheard-of cursed issues # otherwise i get Error: unsupported locale setting diff --git a/pelican-theme/test/test_page.py b/pelican-theme/test/test_page.py index 219c3d98..0f6c3acd 100644 --- a/pelican-theme/test/test_page.py +++ b/pelican-theme/test/test_page.py @@ -159,7 +159,9 @@ class HtmlEscape(PageTestCase): self.assertEqual(*self.actual_expected_contents('landing.html')) self.assertEqual(*self.actual_expected_contents('breadcrumb.html')) - @unittest.skipUnless(LooseVersion(pelican.__version__) > LooseVersion("4.2.0"), + # Not merged for 4.7 yet and no time from my side to push the PR through, + # so let's defer this to blow up at some point in the future. + @unittest.skipUnless(LooseVersion(pelican.__version__) > LooseVersion("5.0.0"), "https://github.com/getpelican/pelican/pull/2260") def test_content(self): self.run_pelican({ diff --git a/plugins/m/code.py b/plugins/m/code.py index 10593178..0878440f 100644 --- a/plugins/m/code.py +++ b/plugins/m/code.py @@ -40,7 +40,29 @@ import logging logger = logging.getLogger(__name__) -import ansilexer +try: + import ansilexer +except ImportError: + # The above worked well on Pelican 4.2 and before, and also works with + # other m.css tools like the Python doc generator. Pelican 4.5.0 changed to + # "namespace plugins" and broke packaged plugins completely, 4.5.1 was + # fixed to load namespaced plugins again, however the loading code is + # different from 4.2 and thus anything from the root plugins/ directory + # *isn't* in PATH anymore. Thus attempting to import those modules fails + # and as a DIRTY hack I have to add the path back. + # + # TODO: Pelican 4.5+ treats everything that isn't in the pelican.plugins + # namespace as "legacy plugins", which is unfortunate because then I + # wouldn't be able to easily share the plugin code with other m.css tools + # which don't (and shouldn't need to) care about Pelican at all. Allowing + # 3rd party plugins without enforcing implicit assumptions on them (the + # namespace, an unprefixed register() function...) would probably involve a + # complex discussion with Pelican maintainers which I don't have the energy + # for right now. Let's hope the "legacy plugins" codepath stays in for the + # foreseeable future. + import sys + sys.path.append(os.path.join(os.path.dirname(__file__), '..')) + import ansilexer filters_pre = None filters_post = None diff --git a/plugins/m/dot.py b/plugins/m/dot.py index 4a3a2324..54d99a45 100644 --- a/plugins/m/dot.py +++ b/plugins/m/dot.py @@ -30,7 +30,16 @@ from docutils.parsers import rst from docutils.parsers.rst import directives from docutils.parsers.rst.roles import set_classes -import dot2svg +try: + import dot2svg +except ImportError: + # While the above was enough to make things work with Pelican 4.2 and + # before (and also works with other m.css tools like the Python doc + # generator), Pelican 4.5.1+ needs the below (4.5.0 didn't work with + # namespaced plugins at all). See the comment in m.code for further info. + import sys + sys.path.append(os.path.join(os.path.dirname(__file__), '..')) + import dot2svg def _is_graph_figure(parent): # The parent has to be a figure, marked as m-figure diff --git a/plugins/m/math.py b/plugins/m/math.py index 1dd7ae70..f2399d47 100644 --- a/plugins/m/math.py +++ b/plugins/m/math.py @@ -32,8 +32,18 @@ from docutils.parsers import rst from docutils.parsers.rst import directives from docutils.parsers.rst.roles import set_classes -import latex2svg -import latex2svgextra +try: + import latex2svg + import latex2svgextra +except ImportError: + # While the above was enough to make things work with Pelican 4.2 and + # before (and also works with other m.css tools like the Python doc + # generator), Pelican 4.5.1+ needs the below (4.5.0 didn't work with + # namespaced plugins at all). See the comment in m.code for further info. + import sys + sys.path.append(os.path.join(os.path.dirname(__file__), '..')) + import latex2svg + import latex2svgextra default_settings = { 'INPUT': '', -- 2.30.2