#!/usr/bin/python3 import re import time # get list of chapters chapters = [] for line in open('index.html', encoding="utf-8"): if not line.count('', 1)[0]) # construct regexes used to fix internal xrefs chapter_href = re.compile("".format("|".join(chapters))) chapter_fragment_href = re.compile("".format("|".join(chapters))) same_chapter_fragment_href = re.compile("]") # munge and combine chapter-specific styles out = open('build/single.html', 'w', encoding="utf-8") out.write(open('util/single-header.html', encoding="utf-8").read().replace('%%DATE%%', time.strftime('%Y-%m-%d', time.localtime()))) out.write("'): include = False if include and not line.count('counter-reset'): line = "#{0} {1}".format(chapter_id, line) line = line.replace(",", ", #{0} ".format(chapter_id)) out.write(line) if line.count('\n") out.write(open('util/single-header2.html', encoding="utf-8").read()) # munge and combine chapters for filename in chapters: include = False id_prefix = filename.split(".", 1)[0] chapter_id = "chapter-" + id_prefix out.write("
\n".format(chapter_id)) for line in open(filename, encoding="utf-8"): if line.count('

'): include = True if line.count('

©'): include = False if line.count('

".format(id_prefix, x.group(1)), line) line = same_chapter_fragment_href.sub(lambda x: "", line) line = chapter_fragment_href.sub(lambda x: "".format(x.group(1).replace('.html', ''), x.group(2)) , line) out.write(line) out.write("

\n") out.write(open('util/single-footer.html', encoding="utf-8").read()) out.close()