From: Vladimír Vondruš Date: Mon, 22 Jan 2018 13:13:28 +0000 (+0100) Subject: m.math: ability of fallback to rendering math as code blocks. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=2639bdba6be7cbba5fcb5857ba447dff4c5b05be;p=blog.git m.math: ability of fallback to rendering math as code blocks. --- diff --git a/doc/plugins/math-and-code.rst b/doc/plugins/math-and-code.rst index 1a4f4739..36f7da02 100644 --- a/doc/plugins/math-and-code.rst +++ b/doc/plugins/math-and-code.rst @@ -146,6 +146,23 @@ want to add additional CSS classes, derive a custom role from it. Quaternion-conjugated dual quaternion is :math-info:`\hat q^* = q_0^* + q_\epsilon^*`, while dual-conjugation gives :math:`\overline{\hat q} = q_0 - \epsilon q_\epsilon`. +.. note-info:: + + LaTeX can be sometimes a real pain to set up. In order to make it possible + to work on sites that use the :py:`m.math` plugin on machines without LaTeX + installed, you can enable a fallback option to render all math as code + blocks using the :py:`M_MATH_RENDER_AS_CODE` setting. That can be, for + example, combined with a check for presence of the LaTeX binary: + + .. code:: py + + import shutil + import logging + + if not shutil.which('latex'): + logging.warning("LaTeX not found, fallback to rendering math as code") + M_MATH_RENDER_AS_CODE = True + `Code`_ ======= diff --git a/pelican-plugins/m/math.py b/pelican-plugins/m/math.py index 41e0a8c9..5129206e 100644 --- a/pelican-plugins/m/math.py +++ b/pelican-plugins/m/math.py @@ -24,7 +24,7 @@ import re -from docutils import nodes +from docutils import nodes, utils from docutils.parsers import rst from docutils.parsers.rst import directives from docutils.parsers.rst.roles import set_classes @@ -64,6 +64,8 @@ unique_dst = r"""\g='\geq{counter}-\g'""" counter = 0 +render_as_code = False + def _patch(formula, out, attribs): global counter counter += 1 @@ -77,6 +79,14 @@ class Math(rst.Directive): def run(self): set_classes(self.options) self.assert_has_content() + + # Fallback rendering as code requested + if render_as_code: + content = nodes.raw('', '\n'.join(self.content), format='html') + pre = nodes.literal_block('') + pre.append(content) + return [pre] + # join lines, separate blocks content = '\n'.join(self.content).split('\n\n') _nodes = [] @@ -104,6 +114,19 @@ def math(role, rawtext, text, lineno, inliner, options={}, content=[]): i = rawtext.find('`') text = rawtext.split('`')[1] + # Fallback rendering as code requested + if render_as_code: + set_classes(options) + classes = [] + if 'classes' in options: + classes += options['classes'] + del options['classes'] + + content = nodes.raw('', utils.unescape(text), format='html') + node = nodes.literal(rawtext, '', **options) + node.append(content) + return [node], [] + # Apply classes to the element instead of some outer set_classes(options) classes = 'm-math' @@ -121,7 +144,12 @@ def math(role, rawtext, text, lineno, inliner, options={}, content=[]): node = nodes.raw(rawtext, _patch(text, out, attribs), format='html', **options) return [node], [] +def configure_pelican(pelicanobj): + global render_as_code + render_as_code = pelicanobj.settings.get('M_MATH_RENDER_AS_CODE', False) + def register(): + pelican.signals.initialized.connect(configure_pelican) pelican.signals.content_object_init.connect(new_page) rst.directives.register_directive('math', Math) rst.roles.register_canonical_role('math', math) diff --git a/pelican-plugins/m/test/math/page-code-fallback.html b/pelican-plugins/m/test/math/page-code-fallback.html new file mode 100644 index 00000000..ba3df221 --- /dev/null +++ b/pelican-plugins/m/test/math/page-code-fallback.html @@ -0,0 +1,42 @@ + + + + + m.math | A Pelican Blog + + + + + + +
+
+
+
+
+
+

m.math

+ +

Inline colored math a^2 and colored math block:

+
a^2 + b^2 = c^2
+

Properly align huge formulas vertically on a line: +\hat q^{-1} = \frac{\hat q^*}{|\hat q|^2} +and make sure there's enough space for all the complex W things between +the lines W = \sum_{i=0}^{n} \frac{w_i}{h_i} because +Y = \sum_{i=0}^{n} B

+

The \cfrac thing doesn't align well: W = \sum_{i=0}^{n} \cfrac{w_i}{h_i}

+ +
+
+
+
+
+ + + diff --git a/pelican-plugins/m/test/test_math.py b/pelican-plugins/m/test/test_math.py index 735e15d0..8a933685 100644 --- a/pelican-plugins/m/test/test_math.py +++ b/pelican-plugins/m/test/test_math.py @@ -42,3 +42,11 @@ class Math(PluginTestCase): }) self.assertEqual(*self.actual_expected_contents('page.html')) + + def test_code_fallback(self): + self.run_pelican({ + 'PLUGINS': ['m.htmlsanity', 'm.math'], + 'M_MATH_RENDER_AS_CODE': True + }) + + self.assertEqual(*self.actual_expected_contents('page.html', 'page-code-fallback.html'))