From 1da065cb6ae2761c3cf95dcb51dbda0e39df68ec Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 14 Oct 2018 16:38:38 +0200 Subject: [PATCH] m.math: compatibility with math figures from m.components. --- doc/plugins/math-and-code.rst | 41 ++++++++++ pelican-plugins/m/math.py | 28 ++++++- pelican-plugins/m/test/dot/page.rst | 9 +++ .../m/test/math/page-code-fallback.html | 11 ++- pelican-plugins/m/test/math/page.html | 75 +++++++++++++++++++ pelican-plugins/m/test/math/page.rst | 22 ++++++ pelican-plugins/m/test/test_math.py | 2 +- 7 files changed, 185 insertions(+), 3 deletions(-) diff --git a/doc/plugins/math-and-code.rst b/doc/plugins/math-and-code.rst index 04fc8ebf..58ac70a9 100644 --- a/doc/plugins/math-and-code.rst +++ b/doc/plugins/math-and-code.rst @@ -219,6 +219,47 @@ to disable caching. logging.warning("LaTeX not found, fallback to rendering math as code") M_MATH_RENDER_AS_CODE = True +`Math figure`_ +-------------- + +See the `m.components <{filename}/plugins/components.rst#code-math-and-graph-figure>`__ +plugin for details about code figures using the :rst:`.. math-figure::` +directive. + +.. code-figure:: + + .. code:: rst + + .. math-figure:: Infinite projection matrix + + .. math:: + :class: m-success + + \boldsymbol{A} = \begin{pmatrix} + \frac{2n}{s_x} & 0 & 0 & 0 \\ + 0 & \frac{2n}{s_y} & 0 & 0 \\ + 0 & 0 & -1 & -2n \\ + 0 & 0 & -1 & 0 + \end{pmatrix} + + With :math:`f = \infty`. + + .. math-figure:: Infinite projection matrix + + .. math:: + :class: m-success + + \boldsymbol{A} = \begin{pmatrix} + \frac{2n}{s_x} & 0 & 0 & 0 \\ + 0 & \frac{2n}{s_y} & 0 & 0 \\ + 0 & 0 & -1 & -2n \\ + 0 & 0 & -1 & 0 + \end{pmatrix} + + .. class:: m-noindent + + With :math:`f = \infty`. + `Code`_ ======= diff --git a/pelican-plugins/m/math.py b/pelican-plugins/m/math.py index c3d8b085..c32e2e80 100644 --- a/pelican-plugins/m/math.py +++ b/pelican-plugins/m/math.py @@ -38,6 +38,17 @@ import latex2svgextra render_as_code = False +def _is_math_figure(parent): + # The parent has to be a figure, marked as m-figure + if not isinstance(parent, nodes.figure): return False + if 'm-figure' not in parent.get('classes', []): return False + + # And as a first visible node of such type + for child in parent: + if not isinstance(child, nodes.Invisible): return False + + return True + class Math(rst.Directive): option_spec = {'class': directives.class_option, 'name': directives.unchanged} @@ -47,8 +58,15 @@ class Math(rst.Directive): set_classes(self.options) self.assert_has_content() + parent = self.state.parent + # Fallback rendering as code requested if render_as_code: + # If this is a math figure, replace the figure CSS class to have a + # matching border + if _is_math_figure(parent): + parent['classes'][parent['classes'].index('m-figure')] = 'm-code-figure' + content = nodes.raw('', html.escape('\n'.join(self.content)), format='html') pre = nodes.literal_block('') pre.append(content) @@ -58,10 +76,18 @@ class Math(rst.Directive): _, svg = latex2svgextra.fetch_cached_or_render("$$" + content + "$$") + # If this is the first real node inside a math figure, put the SVG + # directly inside + if _is_math_figure(parent): + node = nodes.raw(self.block_text, latex2svgextra.patch(content, svg, None, ' class="{}"'.format(' '.join(['m-math'] + self.options.get('classes', [])))), format='html') + node.line = self.content_offset + 1 + self.add_name(node) + return [node] + + # Otherwise wrap it in a
node = nodes.raw(self.block_text, latex2svgextra.patch(content, svg, None, ''), format='html') node.line = self.content_offset + 1 self.add_name(node) - container = nodes.container(**self.options) container['classes'] += ['m-math'] container.append(node) diff --git a/pelican-plugins/m/test/dot/page.rst b/pelican-plugins/m/test/dot/page.rst index a1fc85fe..42e39649 100644 --- a/pelican-plugins/m/test/dot/page.rst +++ b/pelican-plugins/m/test/dot/page.rst @@ -54,3 +54,12 @@ Structs: struct [label="{ a | b | { c | d | e }}" shape=record class="m-info"] another [label="a | { b | c } | d | e" shape=record] + +.. graph-figure:: This is a title. + + .. digraph:: A to B + :class: m-info + + a -> b + + This is a description. diff --git a/pelican-plugins/m/test/math/page-code-fallback.html b/pelican-plugins/m/test/math/page-code-fallback.html index 683e28dc..32203a2a 100644 --- a/pelican-plugins/m/test/math/page-code-fallback.html +++ b/pelican-plugins/m/test/math/page-code-fallback.html @@ -39,6 +39,16 @@ the lines W = \sum_{i=0}^{n} \frac{w_i}{h_i} because \end{array}

Formulas a^2 in big text are big.

Formulas a^2 in small text are small.

+
+
a^2 + b^2 = c^2
+
This is a title.
+

This is a description.

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

The math below should not be styled as a part of the figure:

+
a^2 + b^2 = c^2
+
@@ -47,4 +57,3 @@ the lines W = \sum_{i=0}^{n} \frac{w_i}{h_i} because - diff --git a/pelican-plugins/m/test/math/page.html b/pelican-plugins/m/test/math/page.html index 41f2b3c2..975af51f 100644 --- a/pelican-plugins/m/test/math/page.html +++ b/pelican-plugins/m/test/math/page.html @@ -289,6 +289,81 @@ a^2 in small text are small.

+
+ + +a^2 + b^2 = c^2 + + + + + + + + + + + + + + + + + + + +
This is a title.
+

This is a description.

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

The math below should not be styled as a part of the figure:

+
+ + +a^2 + b^2 = c^2 + + + + + + + + + + + + + + + + + + + +
+
diff --git a/pelican-plugins/m/test/math/page.rst b/pelican-plugins/m/test/math/page.rst index 3f58c9a7..465cb246 100644 --- a/pelican-plugins/m/test/math/page.rst +++ b/pelican-plugins/m/test/math/page.rst @@ -42,3 +42,25 @@ Formulas :math:`a^2` in big text are big. .. class:: m-text m-small Formulas :math:`a^2` in small text are small. + +.. math-figure:: This is a title. + + .. math:: + :class: m-info + + a^2 + b^2 = c^2 + + This is a description. + +.. math-figure:: + + .. math:: + + a^2 + b^2 = c^2 + + The math below should not be styled as a part of the figure: + + .. math:: + :class: m-danger + + a^2 + b^2 = c^2 diff --git a/pelican-plugins/m/test/test_math.py b/pelican-plugins/m/test/test_math.py index 272d35af..68659e6a 100644 --- a/pelican-plugins/m/test/test_math.py +++ b/pelican-plugins/m/test/test_math.py @@ -42,7 +42,7 @@ class Math(PluginTestCase): "The math plugin requires at least Python 3.5 and LaTeX installed") def test(self): self.run_pelican({ - 'PLUGINS': ['m.htmlsanity', 'm.math'], + 'PLUGINS': ['m.htmlsanity', 'm.components', 'm.math'], 'M_MATH_CACHE_FILE': None }) -- 2.30.2