Use level 1 heading as title

This commit is contained in:
Unrud 2021-12-14 00:23:13 +01:00
parent 208c869b6b
commit ad604dcab7
2 changed files with 21 additions and 7 deletions

View file

@ -8,7 +8,7 @@ import json
import re
import sys
from run import SHIFT_HEADING
TITLE = "Documentation"
def text_content(content):
@ -33,13 +33,23 @@ def convert_framgent(*titles):
def main():
data = json.load(sys.stdin)
delete_block_indices = []
level1_heading = None
# Use hierachical link fragments (e.g. #heading/subheading)
headings = []
for block in data["blocks"]:
for i, block in enumerate(data["blocks"]):
if block["t"] != "Header":
continue
level, (attr_id, attr_class, attr_name), content = block["c"]
shifted_level = level - SHIFT_HEADING
if level == 1:
if level1_heading is not None:
print("ERROR: Mulitple level 1 headings found",
file=sys.stderr)
exit(1)
delete_block_indices.append(i)
level1_heading = content
continue
shifted_level = level - 1 # Ignore level 1 heading
title = text_content(content)
headings = headings[:shifted_level - 1]
while len(headings) < shifted_level - 1:
@ -47,6 +57,12 @@ def main():
headings.append(title)
full_attr_id = convert_framgent(*headings)
block["c"] = [level, [full_attr_id, attr_class, attr_name], content]
if level1_heading != [{'t': 'Str', 'c': TITLE}]:
print("ERROR: Level 1 heading must be %r" % TITLE, file=sys.stderr)
exit(1)
data["meta"]["title"] = {"t": "MetaInlines", "c": level1_heading}
for i in reversed(delete_block_indices):
del data["blocks"][i]
json.dump(data, sys.stdout)

View file

@ -25,8 +25,7 @@ GIT_CONFIG = {"protocol.version": "2",
COMMIT_MESSAGE = "Generate documentation"
DOCUMENTATION_SRC = "DOCUMENTATION.md"
REDIRECT_CONFIG_PATH = "redirect.json"
SHIFT_HEADING = 1
TOC_DEPTH = 3
TOC_DEPTH = 4
TOOLS_PATH = os.path.dirname(__file__)
REDIRECT_TEMPLATE_PATH = os.path.join(TOOLS_PATH, "template-redirect.html")
TEMPLATE_PATH = os.path.join(TOOLS_PATH, "template.html")
@ -64,8 +63,7 @@ def convert_doc(src_path, to_path, branch, branches):
"--template=%s" % os.path.basename(TEMPLATE_PATH),
"--metadata-file=%s" % os.path.abspath(metadata_file.name),
"--section-divs",
"--shift-heading-level-by=%d" % SHIFT_HEADING,
"--toc-depth=%d" % (TOC_DEPTH+SHIFT_HEADING),
"--toc-depth=%d" % TOC_DEPTH,
"--filter=%s" % os.path.abspath(FILTER_EXE)],
cwd=os.path.dirname(TEMPLATE_PATH),
stdout=subprocess.PIPE, check=True).stdout