6
6
import sphinx .writers .latex
7
7
from docutils import nodes
8
8
from sphinx .application import Sphinx
9
+ from sphinx .config import Config
9
10
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 "
11
15
12
16
13
17
class LaTeXTranslator (sphinx .writers .latex .LaTeXTranslator ):
14
18
15
19
def generate_indices (self ) -> str :
16
20
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 (),
22
24
'' ,
23
- " \\ bookmarksetupnext{{level=part}} \n " ,
25
+ nest_bookmark_level_part ,
24
26
])
25
27
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
28
28
29
-
30
- class TocTreePlusDirective (sphinx .directives .other .TocTree ):
29
+ class LatexTocTreeDirective (sphinx .directives .other .TocTree ):
31
30
32
31
def run (self ) -> List [nodes .Node ]:
33
32
@@ -39,28 +38,43 @@ def run(self) -> List[nodes.Node]:
39
38
and self .env .docname == self .env .config .master_doc
40
39
):
41
40
42
- # TODO: \setcounter{section}{0}
43
- # https://tex.stackexchange.com/questions/271075/reset-counter-section-in-part
44
41
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}}" ,
47
43
format = "latex"
48
44
)
49
45
output .append (latex_part_node )
50
- # self.state.nested_parse(StringList(), self.content_offset, latex_part_node)
51
46
52
47
output .extend (super ().run ())
48
+
53
49
return output
54
50
55
51
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
+
56
69
def setup (app : Sphinx ) -> Dict [str , Any ]:
57
70
"""
58
- Setup Sphinx Extension .
71
+ Setup Sphinx extension .
59
72
60
73
:param app:
61
74
"""
62
75
63
- app .add_directive ("toctree" , TocTreePlusDirective , override = True )
76
+ app .connect ("config-inited" , configure )
77
+ app .add_directive ("toctree" , LatexTocTreeDirective , override = True )
64
78
app .set_translator ("latex" , LaTeXTranslator , override = True )
65
79
66
80
return {
0 commit comments