mirror of
https://github.com/Kozea/Radicale.git
synced 2025-06-26 16:45:52 +00:00
Use regular ids
This commit is contained in:
parent
df8cc6f190
commit
3d3e6b4528
1 changed files with 11 additions and 48 deletions
|
@ -5,64 +5,27 @@ Filter program for transforming the Pandoc AST
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import re
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
TITLE = "Documentation"
|
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():
|
def main():
|
||||||
data = json.load(sys.stdin)
|
data = json.load(sys.stdin)
|
||||||
|
|
||||||
delete_block_indices = []
|
level1_headings = [
|
||||||
level1_heading = None
|
(i, content) for i, (level, _, content)
|
||||||
# Use hierachical link fragments (e.g. #heading/subheading)
|
in ((i, b["c"]) for i, b in enumerate(data["blocks"])
|
||||||
headings = []
|
if b["t"] == "Header")
|
||||||
for i, block in enumerate(data["blocks"]):
|
if level == 1]
|
||||||
if block["t"] != "Header":
|
if (len(level1_headings) != 1
|
||||||
continue
|
or level1_headings[0][1] != [{"t": "Str", "c": TITLE}]):
|
||||||
level, (attr_id, attr_class, attr_name), content = block["c"]
|
print(("ERROR: Document must contain single level 1 heading "
|
||||||
if level == 1:
|
"with content %r") % TITLE, file=sys.stderr)
|
||||||
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)
|
|
||||||
exit(1)
|
exit(1)
|
||||||
data["meta"]["title"] = {"t": "MetaInlines", "c": level1_heading}
|
for i, _ in reversed(level1_headings):
|
||||||
for i in reversed(delete_block_indices):
|
|
||||||
del data["blocks"][i]
|
del data["blocks"][i]
|
||||||
|
data["meta"]["title"] = {"t": "MetaInlines", "c": level1_headings[0][1]}
|
||||||
|
|
||||||
json.dump(data, sys.stdout)
|
json.dump(data, sys.stdout)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue