Debian Codegraph @

devel-docs/_extensions/source.py

raw
1 """Custom reStructuredText entities/helpers for referencing entries in the
2 librsvg source tree.
3
4 For any changes made in this module, please ensure
5 ``devel-docs/devel_docs_mod_guide.rst`` is updated accordingly, if necessary.
6
7 For help/reference on modifying these, see:
8
9 - https://www.sphinx-doc.org/en/master/development/tutorials/extending_syntax.html
10 - https://docutils.sourceforge.io/docs/howto/rst-roles.html
11 - https://protips.readthedocs.io/link-roles.html
12 - https://www.sphinx-doc.org/en/master/extdev/utils.html#sphinx.util.docutils.SphinxRole
13 - https://www.sphinx-doc.org/en/master/extdev/utils.html#sphinx.util.docutils.ReferenceRole
14 - https://github.com/sphinx-doc/sphinx/blob/master/sphinx/roles.py
15 """
16
17 from __future__ import annotations
18
19 from docutils.nodes import reference
20 from sphinx.util.docutils import ReferenceRole
21
22 BASE_URL = "https://gitlab.gnome.org/GNOME/librsvg/-/tree"
23
24
25 class SourceRole(ReferenceRole):
26 """A custom Sphinx role for referencing entries in the librsvg source tree.
27 """
28
29 def run(self):
30 ref, _, path = self.target.rpartition(":")
31
32 if path.startswith("/"):
33 msg = self.inliner.reporter.error(
34 f"Invalid source tree entry path: {path!r}", line=self.lineno
35 )
36 prb = self.inliner.problematic(self.rawtext, self.rawtext, msg)
37
38 return [prb], [msg]
39
40 if ref:
41 # 12 <= 9 characters + elipsis (3 periods);
42 # 12, to allow `librsvg-X.YZ`.
43 short_ref = f"{ref[:9]}..." if len(ref) > 12 else ref
44 else:
45 short_ref = ref = "main"
46
47 node = reference(
48 self.rawtext,
49 (
50 self.title if self.has_explicit_title
51 else path or 'the source tree' if ref == "main"
52 else f"{path or 'the source tree'} (@ {short_ref})"
53 ),
54 refuri=f"{BASE_URL}/{ref}/{path}",
55 **self.options,
56 )
57
58 return [node], []
59
60
61 def setup(app):
62 app.add_role("source", SourceRole())