chiark / gitweb /
m.components: support math and graph figures, figure captions.
authorVladimír Vondruš <mosra@centrum.cz>
Sun, 14 Oct 2018 00:17:46 +0000 (02:17 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Sun, 14 Oct 2018 18:17:49 +0000 (20:17 +0200)
The m.math and m.dot plugins need to be updated as well, to put the
<svg> directly inside instead of wrapping it in a <div>. THat'll get
done in later commits.

doc/plugins/components.rst
pelican-plugins/m/components.py
pelican-plugins/m/test/components/page.html
pelican-plugins/m/test/components/page.rst

index bf9bd12fe64dc9050058bc7d65e8243172fda16e..09a92ca64b38ff6f73b898eddfbe8d959f767997 100644 (file)
@@ -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`_
 =======
index d554dc35d3436a65f63db74e59e901e0dd698b9f..72616c8c3b7c8df83ab6437eafe52b1a231f4edf 100644 (file)
@@ -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)
index e5a62ff5394508cb43a58e3d27916f770537952e..b5ba3435157004136b13b5486fbe88310cc3ce94 100644 (file)
@@ -57,8 +57,16 @@ snippet</pre>
 </figure>
 <figure class="m-console-figure">
 <pre class="m-console">Console</pre>
+<figcaption>Figure caption</figcaption>
 <p>And text.</p>
 </figure>
+<figure class="m-figure">
+<svg class="m-math" style="width: 5rem; height: 5rem;"></svg><p>Math figure contents.</p>
+</figure>
+<figure class="m-figure">
+<svg class="m-graph" style="width: 5rem; height: 5rem;"></svg><figcaption>Caption</figcaption>
+<p>Graph figure contents.</p>
+</figure>
 <div class="m-text m-dim">
 Dim text.</div>
 <p class="m-transition">~ ~ ~</p>
index 68e057898c0f065466af4d4694a610413dc89980..c479373718b3fd2e43f7360bc7228fb30041ca10 100644 (file)
@@ -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
+
+        <svg class="m-math" style="width: 5rem; height: 5rem;"></svg>
+
+    Math figure contents.
+
+.. graph-figure:: Caption
+
+    .. raw:: html
+
+        <svg class="m-graph" style="width: 5rem; height: 5rem;"></svg>
+
+    Graph figure contents.
+
 .. text-dim::
 
     Dim text.