On 4/16/26 12:23, 'unman' via qubes-devel wrote:
> In any case, a more immediate issue is that currently many images are
> rendered as links: the link text is taken from the alt text, and the
> target is the source file. So readers generate "link", not "image" 0
> definitely sub-optimal.
Thanks A LOT for pointing this issue.
The way Sphinx and Docutils treat images and links has many flaws. When
an image is inside a link without any text, the alternative text should
describe the link function, not the image. So even with a non-empty alt,
that's not a good situation. It is of course worse with empty alts.
One solution could be to edit the build process with a custom extension:
* remove the link that Sphinx adds on top of the image
* add a new link **after** the image node
The following is a snippet to show how it would be possible. I would
like to know if it would be an acceptable solution before finishing it.
Copy-pasting it at the end of conf.py is a simple way to test the code:
from sphinx.application import Sphinx
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.util.typing import ExtensionMetadata
from docutils import nodes
import posixpath
class FixStandaloneHTMLBuilder(StandaloneHTMLBuilder):
name = "html"
html_scaled_image_link = False
def post_process_images(self, doctree: Node) -> None:
super().post_process_images(doctree)
for node in doctree.findall(nodes.image):
if not any((key in node) for key in ("scale", "width",
"height")):
continue
if isinstance(node.parent, nodes.reference):
continue
if "no-scaled-link" in node["classes"]:
continue
uri = node["uri"]
parent = nodes.paragraph("")
reference = nodes.reference("", f"Link to {node['uri']}",
internal=True)
parent.append(reference)
if uri in self.images:
reference["refuri"] = posixpath.join(self.imgpath,
self.images[uri])
else:
reference["refuri"] = uri
node.parent.append(parent)
def setup(app: Sphinx) -> ExtensionMetadata:
app.add_builder(FixStandaloneHTMLBuilder, override=True)
return {
"version": "0.1",
"env_version": 1,
"parallel_read_safe": True,
"parallel_write_safe": True,
}