Skip to content

Commit

Permalink
New ToC generator based on querying the docbook XML
Browse files Browse the repository at this point in the history
Production use
  • Loading branch information
shlomif committed Aug 24, 2024
1 parent 0a6e31f commit b171f53
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GEN_FAQ = FAQ_with_ToC__generated.md
MARKDOWN_FAQ_WITHOUT_TOC = FAQ.mdwn
GEN = topic.py
GEN = gen-toc-based-on-docbook5.py
TOC_DIR = github-markdown-toc
TOC_GEN = $(TOC_DIR)/gh-md-toc
DOCBOOK5 = FAQ.docbook5.xml
Expand All @@ -16,7 +16,7 @@ TARGETS += $(XHTML)
all: $(GEN_FAQ)

$(GEN_FAQ): $(MARKDOWN_FAQ_WITHOUT_TOC) $(GEN) $(TOC_GEN)
python3 $(GEN) --input $< --output $@
PYTHONPATH="$${PWD}/t/lib" python3 $(GEN) --input $< --output $@

$(TOC_GEN):
git clone https://github.com/ekalinin/github-markdown-toc
Expand Down
44 changes: 33 additions & 11 deletions gen-toc-based-on-docbook5.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#
# Distributed under terms of the MIT license.

# import re
import re

import argparse
import sys
import lxml.etree

import html_unit_test
Expand All @@ -22,7 +24,7 @@ def test_initial_docbook(self):
id_attr = '{xml}id'.format(xml=("{" + ns['xml'] + "}"))

def _process_sections():
output_text = ""
_toc_text = ""
while len(sections.xpath_results):
section = sections.xpath_results.pop(0)
id2 = section.get(id_attr)
Expand All @@ -31,6 +33,11 @@ def _process_sections():
title_xpath, namespaces=ns
)
docbook5_title = docbook5_title_list[0]
docbook5_title = re.sub('\\n', ' ', docbook5_title)
assert '\n' not in id2
if id2 == 'what-is-the-channel-for-topictechnology':
docbook5_title = \
"What is the channel for <em>TOPIC</em>/TECHNOLOGY?"
count_parent_section_elements = 0
parent = section
while parent is not None:
Expand All @@ -39,17 +46,32 @@ def _process_sections():
count_parent_section_elements += 1
parent = parent.getparent()
assert count_parent_section_elements > 0
output_text += "{}* [{}](#{})\n".format(
(" " * (4 * (count_parent_section_elements - 1))),
_toc_text += "{}* [{}](#{})\n".format(
(" " * (3 * (count_parent_section_elements - 1))),
docbook5_title, id2)
print(output_text)
return output_text
return _toc_text

_process_sections()
if 0:
for section in sections.xpath_results:
print(section.get(id_attr))
return _process_sections()


def main(argv):
parser = argparse.ArgumentParser(
prog='PROG',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--output', type=str, required=True,
help='output filename')
parser.add_argument('--input', type=str, required=True,
help='Input filename')
args = parser.parse_args(argv[1:])
out_s = ''
with open(args.input, "rt") as fh:
out_s += fh.read()
output_toc = MyTests().test_initial_docbook()
output_text = output_toc + out_s

with open(args.output, "wt") as ofh:
ofh.write(output_text)


if __name__ == '__main__':
MyTests().test_initial_docbook()
main(sys.argv)

0 comments on commit b171f53

Please sign in to comment.