Generate a nested nav if parts exist and the nested flag is on
This commit is contained in:
@@ -103,10 +103,7 @@ class ToEPub(ToHtml):
|
||||
##
|
||||
|
||||
def setReplaceUnicode(self, doReplace: bool) -> None:
|
||||
"""Set translation map.
|
||||
|
||||
Replace Unicode is ignored for EPub.
|
||||
"""
|
||||
"""Set translation map. Replace Unicode is ignored for EPub."""
|
||||
self._trMap = str.maketrans({"&": "&"})
|
||||
|
||||
##
|
||||
@@ -382,15 +379,42 @@ class ToEPub(ToHtml):
|
||||
xHtml.append("</header>")
|
||||
xHtml.append('<nav epub:type="toc" role="doc-toc" aria-labelledby="toc">')
|
||||
xHtml.append('<ol epub:type="list">')
|
||||
xHtml.extend(
|
||||
f'<li><a href="xhtml/{section.name}.xhtml#{section.sectionID}">{title}</a></li>'
|
||||
for section in self._sections
|
||||
if (title := section.title) and section.epubType != EPubType.COVER
|
||||
)
|
||||
|
||||
nested = self._nestedToc and any(s.epubType == EPubType.PART for s in self._sections)
|
||||
inPart = False
|
||||
inList = False
|
||||
for section in self._sections:
|
||||
if not (title := section.title) or section.epubType == EPubType.COVER:
|
||||
continue
|
||||
link = f'<a href="xhtml/{section.name}.xhtml#{section.sectionID}">{title}</a>'
|
||||
if inPart and section.epubType == EPubType.CHAPTER:
|
||||
if not inList:
|
||||
# A nested list must not be empty, so open it on first child
|
||||
xHtml.append("<ol>")
|
||||
inList = True
|
||||
xHtml.append(f"<li>{link}</li>")
|
||||
continue
|
||||
if inList:
|
||||
xHtml.append("</ol>")
|
||||
inList = False
|
||||
if inPart:
|
||||
xHtml.append("</li>")
|
||||
inPart = False
|
||||
if nested and section.epubType == EPubType.PART:
|
||||
xHtml.append(f"<li>{link}")
|
||||
inPart = True
|
||||
else:
|
||||
xHtml.append(f"<li>{link}</li>")
|
||||
if inList:
|
||||
xHtml.append("</ol>")
|
||||
if inPart:
|
||||
xHtml.append("</li>")
|
||||
|
||||
xHtml.append("</ol>")
|
||||
xHtml.append("</nav>")
|
||||
xHtml.append("</body>")
|
||||
xHtml.append("</html>")
|
||||
|
||||
return "\n".join(xHtml)
|
||||
|
||||
def _generateStyleSheet(self) -> str:
|
||||
|
||||
@@ -81,12 +81,13 @@ class ToHtml(Tokenizer):
|
||||
Extend the Tokenizer class to write HTML output.
|
||||
"""
|
||||
|
||||
__slots__ = ("_brTag", "_cssStyles", "_trMap", "_usedFields", "_usedNotes")
|
||||
__slots__ = ("_brTag", "_cssStyles", "_nestedToc", "_trMap", "_usedFields", "_usedNotes")
|
||||
|
||||
def __init__(self, project: NWProject) -> None:
|
||||
super().__init__(project)
|
||||
self._trMap = {}
|
||||
self._cssStyles = True
|
||||
self._nestedToc = False
|
||||
self._usedNotes: dict[str, int] = {}
|
||||
self._usedFields: list[tuple[int, str]] = []
|
||||
self._brTag = "<br>"
|
||||
@@ -114,6 +115,10 @@ class ToHtml(Tokenizer):
|
||||
# Extend to all relevant Unicode characters
|
||||
self._trMap.update(str.maketrans(nwHtmlUnicode.U_TO_H))
|
||||
|
||||
def setNestedToC(self, value: bool) -> None:
|
||||
"""Set a nested table of contents."""
|
||||
self._nestedToc = value
|
||||
|
||||
##
|
||||
# Class Methods
|
||||
##
|
||||
|
||||
@@ -348,6 +348,9 @@ class DocumentBuilder:
|
||||
bldObj.setCommentType(nwComment.STORY, self._build.getBool("text.includeStory"))
|
||||
bldObj.setCommentType(nwComment.NOTE, self._build.getBool("text.includeNotes"))
|
||||
|
||||
if isinstance(bldObj, ToEPub):
|
||||
bldObj.setNestedToC(self._build.getBool("epub.nestedToc"))
|
||||
|
||||
if isinstance(bldObj, ToHtml):
|
||||
bldObj.setStyles(self._build.getBool("html.addStyles"))
|
||||
bldObj.setReplaceUnicode(self._build.getBool("format.stripUnicode"))
|
||||
|
||||
Reference in New Issue
Block a user