Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed two more bugs with SVG inheritance-diagram links #11634

Merged
merged 12 commits into from Aug 30, 2023
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -16,6 +16,10 @@ Features added
Bugs fixed
----------

* #11634: Fixed two more bugs in inheritance diagrams that resulted in
broken links.
ayshih marked this conversation as resolved.
Show resolved Hide resolved
Patch by Albert Shih.

Testing
-------

Expand Down
23 changes: 12 additions & 11 deletions sphinx/ext/graphviz.py
Expand Up @@ -21,7 +21,7 @@
from sphinx.errors import SphinxError
from sphinx.locale import _, __
from sphinx.util import logging
from sphinx.util.docutils import SphinxDirective, SphinxTranslator
from sphinx.util.docutils import SphinxDirective
from sphinx.util.i18n import search_image_for_language
from sphinx.util.nodes import set_source_info
from sphinx.util.osutil import ensuredir
Expand Down Expand Up @@ -218,7 +218,8 @@ def run(self) -> list[Node]:
return [figure]


def fix_svg_relative_paths(self: SphinxTranslator, filepath: str) -> None:
def fix_svg_relative_paths(self: HTML5Translator | LaTeXTranslator | TexinfoTranslator,
filepath: str) -> None:
"""Change relative links in generated svg files to be relative to imgpath."""
tree = ET.parse(filepath) # NoQA: S314
root = tree.getroot()
Expand All @@ -230,16 +231,15 @@ def fix_svg_relative_paths(self: SphinxTranslator, filepath: str) -> None:
root.findall('.//svg:image[@xlink:href]', ns),
root.findall('.//svg:a[@xlink:href]', ns),
):
scheme, hostname, url, query, fragment = urlsplit(element.attrib[href_name])
scheme, hostname, rel_uri, query, fragment = urlsplit(element.attrib[href_name])
if hostname:
# not a relative link
continue

old_path = path.join(self.builder.outdir, url)
new_path = path.relpath(
old_path,
start=path.join(self.builder.outdir, self.builder.imgpath),
)
doc_dir = (self.builder.outdir / self.builder.current_docname).parent
old_path = doc_dir / rel_uri
img_path = doc_dir / self.builder.imgpath
new_path = path.relpath(old_path, start=img_path)
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved
modified_url = urlunsplit((scheme, hostname, new_path, query, fragment))

element.set(href_name, modified_url)
Expand All @@ -249,7 +249,8 @@ def fix_svg_relative_paths(self: SphinxTranslator, filepath: str) -> None:
tree.write(filepath)


def render_dot(self: SphinxTranslator, code: str, options: dict, format: str,
def render_dot(self: HTML5Translator | LaTeXTranslator | TexinfoTranslator,
code: str, options: dict, format: str,
prefix: str = 'graphviz', filename: str | None = None,
) -> tuple[str | None, str | None]:
"""Render graphviz code into a PNG or PDF output file."""
Expand Down Expand Up @@ -294,8 +295,8 @@ def render_dot(self: SphinxTranslator, code: str, options: dict, format: str,
logger.warning(__('dot command %r cannot be run (needed for graphviz '
'output), check the graphviz_dot setting'), graphviz_dot)
if not hasattr(self.builder, '_graphviz_warned_dot'):
self.builder._graphviz_warned_dot = {} # type: ignore[attr-defined]
self.builder._graphviz_warned_dot[graphviz_dot] = True # type: ignore[attr-defined]
self.builder._graphviz_warned_dot = {} # type: ignore[union-attr]
self.builder._graphviz_warned_dot[graphviz_dot] = True
return None, None
except CalledProcessError as exc:
raise GraphvizError(__('dot exited with error:\n[stderr]\n%r\n'
Expand Down
3 changes: 2 additions & 1 deletion sphinx/ext/inheritance_diagram.py
Expand Up @@ -36,6 +36,7 @@ class E(B): pass
import re
from collections.abc import Iterable, Sequence
from importlib import import_module
from os import path
from typing import TYPE_CHECKING, Any, cast

from docutils import nodes
Expand Down Expand Up @@ -417,7 +418,7 @@ def html_visit_inheritance_diagram(self: HTML5Translator, node: inheritance_diag

# Create a mapping from fully-qualified class names to URLs.
graphviz_output_format = self.builder.env.config.graphviz_output_format.upper()
current_filename = self.builder.current_docname + self.builder.out_suffix
current_filename = path.basename(self.builder.current_docname + self.builder.out_suffix)
urls = {}
pending_xrefs = cast(Iterable[addnodes.pending_xref], node)
for child in pending_xrefs:
Expand Down
2 changes: 2 additions & 0 deletions tests/roots/test-ext-inheritance_diagram/subdir/index.rst
Expand Up @@ -4,4 +4,6 @@ test-ext-inheritance_diagram subdirectory

.. inheritance-diagram:: test.DocMainLevel

.. inheritance-diagram:: test.DocLowerLevel

.. py:class:: test.DocLowerLevel