|
| 1 | +"""A directive to generate an embedded svg image using Graphviz/Sketchviz |
| 2 | +backend parsers |
| 3 | +""" |
| 4 | +import os |
| 5 | +from tempfile import NamedTemporaryFile |
| 6 | +import subprocess |
| 7 | +import hashlib |
| 8 | + |
| 9 | +from docutils import nodes |
| 10 | +from ablog.commands import find_confdir, read_conf |
| 11 | +from docutils.parsers.rst import Directive |
| 12 | +from jinja2 import BaseLoader, Environment |
| 13 | +from sphinx.application import Sphinx |
| 14 | +from sphinx.util import logging |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | +TEMPLATE = """ |
| 19 | +<div class="sketchviz-image"> |
| 20 | + <object type="image/svg+xml" data="/{{ url }}"></object> |
| 21 | +</div> |
| 22 | +""" |
| 23 | +FILENAME_EXT = ".svg" |
| 24 | + |
| 25 | + |
| 26 | +class SketchvizError(Exception): |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +class Sketchviz(Directive): |
| 31 | + arguments = 1 |
| 32 | + has_content = True |
| 33 | + final_argument_whitespace = False |
| 34 | + option_spec = { |
| 35 | + "static-subdir": lambda a: a.strip(), |
| 36 | + } |
| 37 | + |
| 38 | + def run(self): |
| 39 | + static_subdir = self.options.get("static-subdir") |
| 40 | + confdir = find_confdir() |
| 41 | + conf = read_conf(confdir) |
| 42 | + html_static_path = getattr(conf, "html_static_path", []) |
| 43 | + |
| 44 | + if len(html_static_path) == 0: |
| 45 | + raise SketchvizError( |
| 46 | + "html_static_path should have at least one path configured" |
| 47 | + ) |
| 48 | + |
| 49 | + # We choose the first path as the default path for the image |
| 50 | + save_path = os.path.join(html_static_path[0], static_subdir) |
| 51 | + |
| 52 | + # Try to make the dir if it doesn't exist |
| 53 | + os.makedirs(save_path, exist_ok=True) |
| 54 | + |
| 55 | + temp_name = None |
| 56 | + h = hashlib.new("sha256") |
| 57 | + with NamedTemporaryFile( |
| 58 | + mode="w", |
| 59 | + encoding="utf-8", |
| 60 | + delete=False, |
| 61 | + delete_on_close=False) as fd: |
| 62 | + for content in self.content: |
| 63 | + line = f"{content}\n" |
| 64 | + h.update(line.encode()) |
| 65 | + fd.write(line) |
| 66 | + temp_name = fd.name |
| 67 | + |
| 68 | + filename = h.digest().hex() |
| 69 | + filename = f"{filename}{FILENAME_EXT}" |
| 70 | + save_path = os.path.join(save_path, filename) |
| 71 | + subprocess.run( |
| 72 | + ["sketchviz", temp_name, save_path], check=True) |
| 73 | + os.remove(temp_name) |
| 74 | + logger.info("Sketchviz: Created SVG image: {save_path}") |
| 75 | + |
| 76 | + template = Environment(loader=BaseLoader).from_string(TEMPLATE) |
| 77 | + |
| 78 | + out = template.render(url=save_path) |
| 79 | + # User a raw pass-through node |
| 80 | + para = nodes.raw("", out, format="html") |
| 81 | + return [para] |
| 82 | + |
| 83 | + |
| 84 | +def setup(app: Sphinx): |
| 85 | + app.add_directive("sketchviz", Sketchviz) |
| 86 | + |
| 87 | + return { |
| 88 | + "version": "0.1", |
| 89 | + "parallel_read_safe": True, |
| 90 | + "parallel_write_safe": True, |
| 91 | + } |
0 commit comments