Skip to content

Commit 6cc4e9e

Browse files
authored
🐛 FIX: file extension handling in _toc.yml (#45)
1 parent 4e994d9 commit 6cc4e9e

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

jupyterbook_latex/transforms.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sphinx import addnodes
99
from sphinx.builders.latex.nodes import thebibliography
1010

11-
from .utils import get_filename
11+
from .utils import getFilename, removeExtension
1212
from .nodes import HiddenCellNode, H2Node, H3Node
1313

1414
# Utility functions
@@ -103,7 +103,7 @@ def alterNodes(sectLevelsDict, parentSect):
103103
replaceWithNode(sect, HiddenCellNode, False)
104104

105105
# check if the document is the masterdoc
106-
if get_filename(self.document["source"]) == self.app.config.master_doc:
106+
if getFilename(self.document["source"]) == self.app.config.master_doc:
107107
# pull the toctree-wrapper and append it later to the topmost document level
108108
for node in self.document.traverse(docutils.nodes.compound):
109109
if "toctree-wrapper" in node["classes"]:
@@ -122,7 +122,7 @@ class handleSubSections(SphinxPostTransform):
122122

123123
def apply(self, **kwargs: Any) -> None:
124124
docname = self.document["source"]
125-
if get_filename(docname) == self.app.config.master_doc:
125+
if getFilename(docname) == self.app.config.master_doc:
126126
for compound in self.document.traverse(docutils.nodes.compound):
127127
if "toctree-wrapper" in compound["classes"]:
128128
nodecopy = compound
@@ -141,12 +141,15 @@ def checkNodeIsInPart(part, node):
141141
nodefile = node.children[0].attributes["docname"]
142142
chapfiles = part["chapters"]
143143
for chap in chapfiles:
144-
if nodefile in chap.values():
144+
chapname = removeExtension(
145+
list(chap.values())[0]
146+
) # get filename without extension
147+
if nodefile in chapname:
145148
return True
146149
return False
147150

148151
docname = self.document["source"]
149-
if get_filename(docname) == self.app.config.master_doc:
152+
if getFilename(docname) == self.app.config.master_doc:
150153
TOC_PATH = Path(self.app.confdir).joinpath("_toc.yml")
151154
tocfile = yaml.safe_load(TOC_PATH.read_text("utf8"))
152155

@@ -178,6 +181,7 @@ def checkNodeIsInPart(part, node):
178181
replaceWithNode(node, HiddenCellNode, False)
179182
sectionName.append(nodecopy)
180183
self.document.append(compoundParent)
184+
181185
# append bib at the end
182186
if len(bibNodes):
183187
self.document.extend(bibNodes)

jupyterbook_latex/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from pathlib import Path
22

33

4-
def get_filename(source):
4+
def getFilename(source):
55
name = Path(source).stem
66
return name
77

88

9+
def removeExtension(filename):
10+
index = filename.find(".")
11+
if index > 0:
12+
filename = filename[0:index]
13+
return filename
14+
15+
916
def sphinxEncode(string):
1017
return (
1118
string.replace("~", "\\textasciitilde{}")

tests/roots/test-partsToc/_toc.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
- part: part1
44
chapters:
5-
- file: part1/chap1
5+
- file: part1/chap1.md
66
- file: part1/chap2
77
sections:
88
- file: part1/sec1
9-
- file: part1/sec2
9+
- file: part1/sec2.md
1010

1111
- part: part2
1212
chapters:

0 commit comments

Comments
 (0)