Commit 2852de83 authored by Ingo Heimbach's avatar Ingo Heimbach

Added an `embedded-figure` directive

parent 16844e60
from .code_directive import CodeBlock
from .embedded_image_directive import EmbeddedImage
from .embedded_image_directive import EmbeddedImage, EmbeddedFigure
from .jupyter_outbox_directive import JupyterOutbox
from .tikz_directive import Tikz, TikzFigure
from .output_mode import get_output_mode, set_output_mode
__all__ = ("CodeBlock", "EmbeddedImage", "JupyterOutbox", "Tikz", "TikzFigure", "get_output_mode", "set_output_mode")
__all__ = ("CodeBlock", "EmbeddedImage", "EmbeddedFigure", "JupyterOutbox", "Tikz", "TikzFigure", "get_output_mode",
"set_output_mode")
......@@ -181,3 +181,32 @@ class EmbeddedImage(Directive):
return messages + [reference_node]
else:
return messages + [image_node]
class EmbeddedFigure(EmbeddedImage):
option_spec = EmbeddedImage.option_spec.copy()
option_spec["figwidth"] = lambda argument: directives.length_or_percentage_or_unitless(argument, "px")
option_spec["figclass"] = directives.class_option
option_spec["align"] = lambda argument: directives.choice(argument, EmbeddedFigure.align_values)
option_spec["caption"] = directives.unchanged
has_content = True
def run(self):
figwidth = self.options.pop("figwidth", None)
figclasses = self.options.pop("figclass", None)
align = self.options.pop("align", None)
caption = self.options.pop("caption", None)
(image_node,) = EmbeddedImage.run(self)
if isinstance(image_node, nodes.system_message):
return [image_node]
figure_node = nodes.figure("", image_node)
if figwidth is not None:
figure_node["width"] = figwidth
if figclasses:
figure_node["classes"] += figclasses
if align:
figure_node["align"] = align
if caption:
caption = nodes.caption("", text=caption)
figure_node += caption
return [figure_node]
......@@ -19,7 +19,7 @@ except:
from docutils.core import publish_cmdline, default_description
from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.body import ParsedLiteral
from ..directives import EmbeddedImage, Tikz, TikzFigure, set_output_mode
from ..directives import EmbeddedImage, EmbeddedFigure, Tikz, TikzFigure, set_output_mode
description = "Generates (X)HTML documents from standalone reStructuredText " "sources. " + default_description
......@@ -28,6 +28,7 @@ description = "Generates (X)HTML documents from standalone reStructuredText " "s
def main():
set_output_mode("html")
directives.register_directive("embedded-image", EmbeddedImage)
directives.register_directive("embedded-figure", EmbeddedFigure)
directives.register_directive("jupyter-outbox", ParsedLiteral)
directives.register_directive("tikz", Tikz)
directives.register_directive("tikz-figure", TikzFigure)
......
......@@ -19,7 +19,7 @@ except:
from docutils.core import publish_cmdline
from docutils.parsers.rst import directives
from ..directives import CodeBlock, EmbeddedImage, JupyterOutbox, Tikz, TikzFigure, set_output_mode
from ..directives import CodeBlock, EmbeddedImage, EmbeddedFigure, JupyterOutbox, Tikz, TikzFigure, set_output_mode
description = (
......@@ -37,6 +37,7 @@ def main():
for directive_name in ("code", "code-block"):
directives.register_directive(directive_name, CodeBlock)
directives.register_directive("embedded-image", EmbeddedImage)
directives.register_directive("embedded-figure", EmbeddedFigure)
directives.register_directive("jupyter-outbox", JupyterOutbox)
directives.register_directive("tikz", Tikz)
directives.register_directive("tikz-figure", TikzFigure)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment