from __future__ import annotations from pathlib import Path from docutils import nodes from sphinx.application import Sphinx from sphinx.util.docutils import SphinxDirective from sphinx.util.typing import ExtensionMetadata class DrawioDirective(SphinxDirective): """A directive to show a drawio diagram! Usage: .. drawio:: example-diagram.drawio.html example-diagram.drawio.png """ has_content = False required_arguments = 2 # html and png def run(self) -> list[nodes.Node]: html_path = self.arguments[0] png_path = self.arguments[1] env = self.state.document.settings.env docdir = Path(env.doc2path(env.docname)).parent html_rel = Path(self.arguments[0]) png_rel = Path(self.arguments[1]) html_path = (docdir / html_rel).resolve() png_path = (docdir / png_rel).resolve() container = nodes.container() # HTML output -> raw HTML node if self.builder.format == "html": # Embed the HTML file contents directly raw_html_node = nodes.raw( "", f'