Created
March 6, 2022 15:38
-
-
Save 1f604/2785c6e07609894742c80935c07d17d0 to your computer and use it in GitHub Desktop.
hoedown toc rewriting script
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 script was tested to work with Hoedown 3.0.7. | |
from collections import defaultdict | |
import subprocess | |
import re | |
p = re.compile(r'^(<a href="#toc_[0-9]+">)(.+)(<\/a>\n)$') | |
# takes a bunch of html lines starting and ending with ul tags | |
# return a single html string starting and ending with div tags | |
def rewrite_toc(lines): | |
first_line = """<div id="toc" class="toc" ><div class="toctitle" ><h2>Contents</h2></div>""" | |
result = [first_line] | |
last_line = "</div>" | |
level = 0 | |
count = defaultdict(int) | |
for line in lines: | |
if line == "<ul>\n": | |
level += 1 | |
count[level] = 0 | |
if line == "</ul>\n": | |
level -= 1 | |
if line == "<li>\n": | |
count[level] += 1 | |
numbering = str(count[1]) | |
for i in range(2, level + 1): | |
numbering += "." + str(count[i]) | |
if line.startswith("<a "): | |
match_groups = p.match(line).groups() | |
if len(match_groups) != 3: | |
print("ERROR: Not expected number of match groups") | |
exit(1) | |
span_tocnumber_open = '<span class="tocnumber">' | |
span_toctext_open = '<span class="toctext">' | |
span_close = "</span>" | |
numbering_html = span_tocnumber_open + numbering + span_close | |
toctext_html = span_toctext_open + match_groups[1] + span_close | |
toprint = match_groups[0] + numbering_html + " " + toctext_html + match_groups[2] | |
result.append(toprint) | |
else: | |
result.append(line) | |
result.append(last_line) | |
result = ''.join(result) | |
return result | |
cmd = ["/path/to/hoedown", "--html-toc", "--toc-level", "500", "input_file.md"] | |
result = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
lines = [line.decode("utf-8") for line in result.stdout.readlines()] | |
print("===========") | |
print(rewrite_toc(lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment