2020-02-27 02:34:49 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
Postprocessor program for the HTML output of Pandoc
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
|
|
def add_class(element, class_):
|
|
|
|
element["class"] = element.get("class", []) + [class_]
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
soup = BeautifulSoup(sys.stdin.buffer, "html.parser")
|
|
|
|
|
|
|
|
# Mark the hierachical levels in the navigation
|
2020-04-18 13:01:05 +02:00
|
|
|
nav = soup.select("main nav")[0]
|
|
|
|
for section in soup.select("main section"):
|
|
|
|
link = nav.find("a", href="#" + section["id"])
|
2020-02-27 02:34:49 +01:00
|
|
|
if link is None:
|
|
|
|
continue
|
|
|
|
add_class(link.parent, section["class"][0])
|
|
|
|
|
|
|
|
# Mark last section
|
2020-04-18 13:01:05 +02:00
|
|
|
add_class(soup.select("main section")[-1], "last")
|
2020-02-27 02:34:49 +01:00
|
|
|
|
|
|
|
# Wrap tables in a div container (for scrolling)
|
|
|
|
for table in soup.select("main table"):
|
|
|
|
container = soup.new_tag("div")
|
|
|
|
add_class(container, "tableContainer")
|
|
|
|
table.wrap(container)
|
|
|
|
|
|
|
|
# Add a link with the fragment to every header
|
2020-04-18 13:01:05 +02:00
|
|
|
for header in soup.select("main section > *:first-child"):
|
2020-02-27 02:34:49 +01:00
|
|
|
section = header.parent
|
|
|
|
link = soup.new_tag("a")
|
|
|
|
add_class(link, "headerlink")
|
|
|
|
link["href"] = "#" + section["id"]
|
|
|
|
link.string = "¶"
|
|
|
|
header.append(" ")
|
|
|
|
header.append(link)
|
|
|
|
|
|
|
|
sys.stdout.buffer.write(soup.encode(formatter="html5") + b"\n")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|