From: Vladimír Vondruš Date: Sun, 14 Oct 2018 00:17:46 +0000 (+0200) Subject: m.components: support math and graph figures, figure captions. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=cfbdcb30c27aba32a67cf1e7b9979fdcb12608f9;p=blog.git m.components: support math and graph figures, figure captions. The m.math and m.dot plugins need to be updated as well, to put the directly inside instead of wrapping it in a
. THat'll get done in later commits. --- diff --git a/doc/plugins/components.rst b/doc/plugins/components.rst index bf9bd12f..09a92ca6 100644 --- a/doc/plugins/components.rst +++ b/doc/plugins/components.rst @@ -142,13 +142,14 @@ Use the :rst:`:class:` option to specify additional CSS classes. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ultrices a erat eu suscipit. `Link. <#>`_ -`Code figure`_ -============== +`Code, math and graph figure`_ +============================== Use :rst:`.. code-figure::` to denote a `code figure <{filename}/css/components.rst#code-figure>`_. Then put a literal code block denoted by :rst:`::` or a :rst:`.. code::` directive as the first element inside. Use the :rst:`:class:` option to specify -additional CSS classes. +additional CSS classes. The optional directive parameter can be used for a +figure caption. .. code-figure:: @@ -176,6 +177,8 @@ additional CSS classes. Use :rst:`.. console-figure::` to denote code figure styled for a `console listing <{filename}/css/components.rst#colored-terminal-output>`_. +Similarly, :rst:`.. math-figure::` denotes a `math figure <{filename}/css/components.rst#math-figure>`_ +and :rst:`.. graph-figure::` denotes a `graph figure <{filename}/css/components.rst#graph-figure>`_. `Text`_ ======= diff --git a/pelican-plugins/m/components.py b/pelican-plugins/m/components.py index d554dc35..72616c8c 100644 --- a/pelican-plugins/m/components.py +++ b/pelican-plugins/m/components.py @@ -163,25 +163,51 @@ class Frame(rst.Directive): topic_node) return [topic_node] -class CodeFigure(rst.Directive): +class _Figure(rst.Directive): + final_argument_whitespace = True has_content = True + optional_arguments = 1 option_spec = {'class': directives.class_option} - style_class = 'm-code-figure' + style_classes = [] def run(self): set_classes(self.options) text = '\n'.join(self.content) figure_node = nodes.figure(text, **self.options) - figure_node['classes'] += [self.style_class] + figure_node['classes'] += self.style_classes self.state.nested_parse(self.content, self.content_offset, figure_node) + + # Insert the title node, if any, right after the code / math / graph. + # There could be things like the class directive before, so be sure to + # find the right node. + if len(self.arguments) == 1: + title_text = self.arguments[0] + title_nodes, _ = self.state.inline_text(title_text, self.lineno) + title_node = nodes.caption('', '', *title_nodes) + + for i, child in enumerate(figure_node): + if isinstance(child, nodes.raw) or isinstance(child, nodes.literal_block): + figure_node.insert(i + 1, title_node) + break + else: assert False # pragma: no cover + return [figure_node] +class CodeFigure(_Figure): + style_classes = ['m-code-figure'] + class ConsoleFigure(CodeFigure): - style_class = 'm-console-figure' + style_classes = ['m-console-figure'] + +class MathFigure(_Figure): + style_classes = ['m-figure'] + +class GraphFigure(_Figure): + style_classes = ['m-figure'] class Text(rst.Directive): has_content = True @@ -363,6 +389,8 @@ def register(): rst.directives.register_directive('frame', Frame) rst.directives.register_directive('code-figure', CodeFigure) rst.directives.register_directive('console-figure', ConsoleFigure) + rst.directives.register_directive('math-figure', MathFigure) + rst.directives.register_directive('graph-figure', GraphFigure) rst.directives.register_directive('text-default', DefaultText) rst.directives.register_directive('text-primary', PrimaryText) diff --git a/pelican-plugins/m/test/components/page.html b/pelican-plugins/m/test/components/page.html index e5a62ff5..b5ba3435 100644 --- a/pelican-plugins/m/test/components/page.html +++ b/pelican-plugins/m/test/components/page.html @@ -57,8 +57,16 @@ snippet
Console
+
Figure caption

And text.

+
+

Math figure contents.

+
+
+
Caption
+

Graph figure contents.

+
Dim text.

~ ~ ~

diff --git a/pelican-plugins/m/test/components/page.rst b/pelican-plugins/m/test/components/page.rst index 68e05789..c4793737 100644 --- a/pelican-plugins/m/test/components/page.rst +++ b/pelican-plugins/m/test/components/page.rst @@ -47,7 +47,7 @@ m.components And a resulting output. -.. console-figure:: +.. console-figure:: Figure caption .. class:: m-console @@ -57,6 +57,22 @@ m.components And text. +.. math-figure:: + + .. raw:: html + + + + Math figure contents. + +.. graph-figure:: Caption + + .. raw:: html + + + + Graph figure contents. + .. text-dim:: Dim text.