diff --git a/documentation-generator/filter.py b/documentation-generator/filter.py index b23b06b1..b5e0d97d 100755 --- a/documentation-generator/filter.py +++ b/documentation-generator/filter.py @@ -5,64 +5,27 @@ Filter program for transforming the Pandoc AST """ import json -import re import sys TITLE = "Documentation" -def text_content(content): - text = "" - for block in content: - if block["t"] == "Space": - text += " " - elif block["t"] == "Str": - text += block["c"] - return text - - -def convert_framgent(*titles): - titles = list(titles) - for i, title in enumerate(titles): - title = re.sub(r"\s", "-", title) - title = re.sub(r"[^\w-]", "", title) - titles[i] = title.lower() - return "/".join(titles) - - def main(): data = json.load(sys.stdin) - delete_block_indices = [] - level1_heading = None - # Use hierachical link fragments (e.g. #heading/subheading) - headings = [] - for i, block in enumerate(data["blocks"]): - if block["t"] != "Header": - continue - level, (attr_id, attr_class, attr_name), content = block["c"] - 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: - headings.append("") - 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) + level1_headings = [ + (i, content) for i, (level, _, content) + in ((i, b["c"]) for i, b in enumerate(data["blocks"]) + if b["t"] == "Header") + if level == 1] + if (len(level1_headings) != 1 + or level1_headings[0][1] != [{"t": "Str", "c": TITLE}]): + print(("ERROR: Document must contain single level 1 heading " + "with content %r") % TITLE, file=sys.stderr) exit(1) - data["meta"]["title"] = {"t": "MetaInlines", "c": level1_heading} - for i in reversed(delete_block_indices): + for i, _ in reversed(level1_headings): del data["blocks"][i] + data["meta"]["title"] = {"t": "MetaInlines", "c": level1_headings[0][1]} json.dump(data, sys.stdout)