Skip to content

Commit 4c7ccff

Browse files
authored
👌 IMPROVE: Handling relative path of master_doc to populate frontmatter (#46)
* amended filename function * minor changes
1 parent 6cc4e9e commit 4c7ccff

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

‎jupyterbook_latex/transforms.py

Lines changed: 9 additions & 4 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 getFilename, removeExtension
11+
from .utils import getFilenameWithSubpath, removeExtension
1212
from .nodes import HiddenCellNode, H2Node, H3Node
1313

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

105105
# check if the document is the masterdoc
106-
if getFilename(self.document["source"]) == self.app.config.master_doc:
106+
numbSlashes = self.app.config.master_doc.count("/")
107+
108+
if (
109+
getFilenameWithSubpath(self.document["source"], numbSlashes)
110+
== self.app.config.master_doc
111+
):
107112
# pull the toctree-wrapper and append it later to the topmost document level
108113
for node in self.document.traverse(docutils.nodes.compound):
109114
if "toctree-wrapper" in node["classes"]:
@@ -122,7 +127,7 @@ class handleSubSections(SphinxPostTransform):
122127

123128
def apply(self, **kwargs: Any) -> None:
124129
docname = self.document["source"]
125-
if getFilename(docname) == self.app.config.master_doc:
130+
if getFilenameWithSubpath(docname, 0) == self.app.config.master_doc:
126131
for compound in self.document.traverse(docutils.nodes.compound):
127132
if "toctree-wrapper" in compound["classes"]:
128133
nodecopy = compound
@@ -149,7 +154,7 @@ def checkNodeIsInPart(part, node):
149154
return False
150155

151156
docname = self.document["source"]
152-
if getFilename(docname) == self.app.config.master_doc:
157+
if getFilenameWithSubpath(docname, 0) == self.app.config.master_doc:
153158
TOC_PATH = Path(self.app.confdir).joinpath("_toc.yml")
154159
tocfile = yaml.safe_load(TOC_PATH.read_text("utf8"))
155160

‎jupyterbook_latex/utils.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
from pathlib import Path
2-
3-
4-
def getFilename(source):
5-
name = Path(source).stem
6-
return name
7-
8-
91
def removeExtension(filename):
102
index = filename.find(".")
113
if index > 0:
124
filename = filename[0:index]
135
return filename
146

157

8+
def getFilenameWithSubpath(source, slashes):
9+
"""Gets the filename along with the relative path upto the number of slashes.
10+
:param source: full source path
11+
:param slashes: denotes the level of subpath
12+
"""
13+
filename = ""
14+
original = str(source)
15+
16+
while slashes >= 0:
17+
index = original.rfind("/")
18+
if index == -1:
19+
return filename
20+
filename = original[index:] + filename
21+
original = original[:index]
22+
slashes -= 1
23+
24+
if filename[0] == "/":
25+
filename = filename[1:]
26+
27+
return removeExtension(filename)
28+
29+
1630
def sphinxEncode(string):
1731
return (
1832
string.replace("~", "\\textasciitilde{}")

0 commit comments

Comments
 (0)