dabla commented on code in PR #77257:
URL: https://github.com/apache/airflow/pull/77257#discussion_r4726757009


##########
devel-common/src/sphinx_exts/mermaid_svg.py:
##########
@@ -22,33 +22,73 @@
 Content-Security-Policy (https://infra.apache.org/csp.html), whose 
``script-src`` does not allow
 that host, so the import is blocked and readers see the diagram source as 
text. Rendered at build
 time by mermaid-cli (``mmdc``), each diagram is an SVG file served from the 
site itself, with no
-script involved.
+script involved. The extension replaces sphinxcontrib-mermaid's HTML visitor 
so that each diagram
+also links to its SVG file, which a reader opens to see a large diagram at 
full size.
 
 ``breeze build-docs`` installs ``mmdc`` and the Chromium it drives into its 
container when a package
-it builds has a diagram (``scripts/in_container/install_mermaid_cli.sh``). 
When ``mmdc`` is missing or
-fails, sphinxcontrib-mermaid logs a warning naming the diagram, and the 
warning fails the docs build.
+it builds has a diagram (``scripts/in_container/install_mermaid_cli.sh``). 
When ``mmdc`` is missing,
+sphinxcontrib-mermaid logs a warning; when it fails, this extension logs one 
naming the diagram.
+Either warning fails the docs build.
 """
 
 from __future__ import annotations
 
 from pathlib import Path
 from typing import TYPE_CHECKING
 
+from docutils import nodes
+from sphinx.util import logging
+from sphinxcontrib.mermaid import MermaidError, mermaid, render_mm
+
 if TYPE_CHECKING:
     from sphinx.application import Sphinx
     from sphinx.config import Config
+    from sphinx.writers.html5 import HTML5Translator
+
+logger = logging.getLogger(__name__)
 
 # mermaid sizes each label's box from the font it measures in the build 
container, and the reader's
 # browser draws the text in whatever font it has. With mermaid's default 
"trebuchet ms" first in the
 # list, the two differ and labels are clipped. Arial is on macOS and Windows, 
and the
 # fonts-liberation package installed with mermaid-cli gives the container 
Liberation Sans, which has
 # the same metrics as Arial, so the boxes fit the text the reader sees.
+# The config also turns off useMaxWidth, so each SVG gets its size in pixels 
instead of width="100%"
+# (see DIAGRAM_HTML). mermaid has no global switch for it: add a diagram type 
when the docs first use it.
 MERMAID_CONFIG_PATH = Path(__file__).parent / "mermaid_config.json"
 
 # The docs build runs as root inside breeze, and Chromium refuses to start as 
root with its sandbox
 # on. The container is the isolation boundary, and the diagrams come from this 
repository.
 PUPPETEER_CONFIG_PATH = Path(__file__).parent / "mermaid_puppeteer_config.json"
 
+# Scaled to the text width, a large diagram such as the database ERD is too 
small to read, and the
+# <object> offers no way to open the SVG on its own. So the diagram links to 
its SVG file, which the
+# browser shows at full size, to scroll and zoom. The <object> ignores the 
pointer so that a click
+# reaches the link. The styles are inline so that no docs package using this 
extension needs a
+# stylesheet for it.
+DIAGRAM_HTML = (
+    '<a class="mermaid-diagram" href="{src}" title="Open the diagram at full 
size"'
+    ' style="display: block; cursor: zoom-in">'
+    '<object data="{src}" type="image/svg+xml"'
+    ' style="display: block; max-width: 100%; height: auto; pointer-events: 
none">'
+    '<p class="warning">{alt}</p></object></a>\n'
+)
+
+
+def _visit_mermaid_html(translator: HTML5Translator, node: mermaid) -> None:
+    code = node["code"]
+    try:
+        src, _ = render_mm(translator, code, node["options"], "svg")
+    except MermaidError as exc:
+        logger.warning("mermaid code %r: %s", code, exc)

Review Comment:
   **[nit]** The warning prints the whole diagram source instead of where the 
diagram is.
   
   This line is carried over from sphinxcontrib-mermaid, but now that the 
visitor lives here it can do better. For the generated ERD, `code` is the full 
`airflow_erd.mmd`, so one `mmdc` failure puts the entire schema in the build 
log ahead of the actual error. Sphinx's logger takes a `location`, which 
prefixes the message with the document and line of the directive ([Sphinx 
logging API](https://www.sphinx-doc.org/en/master/extdev/logging.html)):
   
   ```python
   logger.warning("mermaid diagram could not be drawn: %s", exc, location=node)
   ```
   
   The test's `"Mermaid exited with error"` and `"Parse error on line 2"` 
assertions keep passing, since both come from `exc`. Fine to leave for a 
follow-up.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to