-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import serializePages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
from .. import h, t | ||
from .. import messages as m | ||
|
||
|
||
def serializePages(doc: t.SpecT, mainFilename: str) -> dict[str, str]: | ||
main = h.find("main", doc) | ||
pages = collectIntoPages(list(h.childNodes(main, clear=True)), mainFilename) | ||
ret = {} | ||
serializer = h.Serializer(doc.md.opaqueElements, doc.md.blockElements) | ||
for filename, nodes in pages.items(): | ||
h.replaceContents(main, nodes) | ||
ret[filename] = serializer.serialize(doc.document) | ||
print(filename + " " + str(len(nodes)) + " nodes; " + str(len(ret[filename].split("\n"))) + " lines") | ||
#print(ret[filename][0:80]) | ||
return ret | ||
|
||
|
||
def collectIntoPages(nodes: list[t.NodeT], mainFilename: str) -> dict[str, list[t.NodeT]]: | ||
pages: dict[str, list[t.NodeT]] = {} | ||
filename = mainFilename | ||
pages[filename] = [] | ||
for node in nodes: | ||
if h.isElement(node) and meaningfulHeading(node) and headingLevel(node) == 2: | ||
id = node.get("id") | ||
if not id: | ||
m.die("Heading doesn't have a useful ID, can't split on it.", el=node) | ||
return {} | ||
filename = id + ".html" | ||
pages[filename] = [] | ||
#if h.isElement(node): | ||
# print(f"{filename} {h.tagName(node)} {node.get('bs-line-number')}") | ||
pages[filename].append(node) | ||
return pages | ||
|
||
|
||
def meaningfulHeading(el: t.ElementT) -> bool: | ||
if h.tagName(el) not in ("h1", "h2", "h3", "h4", "h5", "h6"): | ||
return False | ||
lineNum = el.get("bs-line-number", "") | ||
if parsesAsInt(lineNum): | ||
return True | ||
else: | ||
num, _, rest = lineNum.partition(":") | ||
return parsesAsInt(num) and parsesAsInt(rest) | ||
|
||
|
||
def parsesAsInt(s: str) -> bool: | ||
try: | ||
int(s) | ||
return True | ||
except: | ||
return False | ||
|
||
|
||
def headingLevel(el: t.ElementT) -> int: | ||
tag = h.tagName(el) | ||
if tag not in ("h1", "h2", "h3", "h4", "h5", "h6"): | ||
return 0 | ||
return int(tag[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters