Skip to content

Commit 6364871

Browse files
committed
Tidy up latex_toctree_sections.py and automatically configure the required LaTeX packages.
1 parent 6c4392c commit 6364871

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

doc-source/conf.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,3 @@
145145

146146
manpages_url = "https://manpages.debian.org/{path}"
147147
toctree_plus_types.add("fixture")
148-
149-
latex_elements = {
150-
"preamble": r"\usepackage{bookmark}",
151-
}

doc-source/latex_toctree_sections.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@
66
import sphinx.writers.latex
77
from docutils import nodes
88
from sphinx.application import Sphinx
9+
from sphinx.config import Config
910

10-
__all__ = ["TocTreePlusDirective", "setup"]
11+
__all__ = ["LaTeXTranslator", "LatexTocTreeDirective", "setup"]
12+
13+
use_bookmark = r"\usepackage{bookmark}"
14+
nest_bookmark_level_part = "\\bookmarksetupnext{{level=part}}\n"
1115

1216

1317
class LaTeXTranslator(sphinx.writers.latex.LaTeXTranslator):
1418

1519
def generate_indices(self) -> str:
1620

17-
lines = super().generate_indices().splitlines()
18-
19-
return "\n".join([
20-
"\\bookmarksetupnext{{level=part}}\n",
21-
*lines,
21+
return '\n'.join([
22+
nest_bookmark_level_part,
23+
*super().generate_indices().splitlines(),
2224
'',
23-
"\\bookmarksetupnext{{level=part}}\n",
25+
nest_bookmark_level_part,
2426
])
2527

26-
# TODO: The first section in a part has all sub sections nested under it in the sidebar,
27-
# The numbering is correct, and its correct in the contents
2828

29-
30-
class TocTreePlusDirective(sphinx.directives.other.TocTree):
29+
class LatexTocTreeDirective(sphinx.directives.other.TocTree):
3130

3231
def run(self) -> List[nodes.Node]:
3332

@@ -39,28 +38,43 @@ def run(self) -> List[nodes.Node]:
3938
and self.env.docname == self.env.config.master_doc
4039
):
4140

42-
# TODO: \setcounter{section}{0}
43-
# https://tex.stackexchange.com/questions/271075/reset-counter-section-in-part
4441
latex_part_node = nodes.raw(
45-
text=
46-
f"\\setcounter{{section}}{{0}}\n\\part{{{caption}}}\n\\setcounter{{chapter}}{{1}}",
42+
text=f"\\setcounter{{section}}{{0}}\n\\part{{{caption}}}\n\\setcounter{{chapter}}{{1}}",
4743
format="latex"
4844
)
4945
output.append(latex_part_node)
50-
# self.state.nested_parse(StringList(), self.content_offset, latex_part_node)
5146

5247
output.extend(super().run())
48+
5349
return output
5450

5551

52+
def configure(app: Sphinx, config: Config):
53+
"""
54+
Configure the Sphinx extension.
55+
56+
:param app:
57+
:param config:
58+
"""
59+
60+
if not hasattr(config, "latex_elements"):
61+
config.latex_elements = {}
62+
63+
latex_preamble = (config.latex_elements or {}).get("preamble", '')
64+
65+
if use_bookmark not in latex_preamble:
66+
config.latex_elements["preamble"] = f"{latex_preamble}\n{use_bookmark}"
67+
68+
5669
def setup(app: Sphinx) -> Dict[str, Any]:
5770
"""
58-
Setup Sphinx Extension.
71+
Setup Sphinx extension.
5972
6073
:param app:
6174
"""
6275

63-
app.add_directive("toctree", TocTreePlusDirective, override=True)
76+
app.connect("config-inited", configure)
77+
app.add_directive("toctree", LatexTocTreeDirective, override=True)
6478
app.set_translator("latex", LaTeXTranslator, override=True)
6579

6680
return {

0 commit comments

Comments
 (0)