Files
pybricks-api/doc/common/extensions/blockimg.py
T
Laurens Valk 2380be2814 extensions/blockimg: Embed svg.
This cuts the total build from 527 to 156 files,
which is considerably fewer requests when loading
this inside Pybricks Code.

See https://github.com/pybricks/support/issues/1559
2025-02-26 10:58:03 +01:00

42 lines
1.0 KiB
Python

import xml.etree.ElementTree as ET
from docutils.parsers.rst import Directive, directives
from docutils import nodes
from pathlib import Path
SPHINX_IMAGE_PATH = "blockimg"
def get_svg_content(file_path):
with open(file_path, "r", encoding="utf-8") as file:
return file.read()
# Global variable to store the app object
app = None
class BlockImageDirective(Directive):
has_content = False
required_arguments = 1
optional_arguments = 0
def run(self):
# Adjust the image path
file_name = self.arguments[0] + ".svg"
file_path = Path(app.srcdir) / SPHINX_IMAGE_PATH / file_name
# Read the SVG content
svg_content = get_svg_content(file_path)
# Create a raw HTML node with the SVG content
raw_html = f'<div class="svg-container">{svg_content}</div>'
raw_node = nodes.raw("", raw_html, format="html")
return [raw_node]
def setup(apparg):
global app
app = apparg
app.add_directive("blockimg", BlockImageDirective)