Skip to content

Commit b171f53

Browse files
committed
New ToC generator based on querying the docbook XML
Production use
1 parent 0a6e31f commit b171f53

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GEN_FAQ = FAQ_with_ToC__generated.md
22
MARKDOWN_FAQ_WITHOUT_TOC = FAQ.mdwn
3-
GEN = topic.py
3+
GEN = gen-toc-based-on-docbook5.py
44
TOC_DIR = github-markdown-toc
55
TOC_GEN = $(TOC_DIR)/gh-md-toc
66
DOCBOOK5 = FAQ.docbook5.xml
@@ -16,7 +16,7 @@ TARGETS += $(XHTML)
1616
all: $(GEN_FAQ)
1717

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

2121
$(TOC_GEN):
2222
git clone https://github.com/ekalinin/github-markdown-toc

gen-toc-based-on-docbook5.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
#
77
# Distributed under terms of the MIT license.
88

9-
# import re
9+
import re
1010

11+
import argparse
12+
import sys
1113
import lxml.etree
1214

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

2426
def _process_sections():
25-
output_text = ""
27+
_toc_text = ""
2628
while len(sections.xpath_results):
2729
section = sections.xpath_results.pop(0)
2830
id2 = section.get(id_attr)
@@ -31,6 +33,11 @@ def _process_sections():
3133
title_xpath, namespaces=ns
3234
)
3335
docbook5_title = docbook5_title_list[0]
36+
docbook5_title = re.sub('\\n', ' ', docbook5_title)
37+
assert '\n' not in id2
38+
if id2 == 'what-is-the-channel-for-topictechnology':
39+
docbook5_title = \
40+
"What is the channel for <em>TOPIC</em>/TECHNOLOGY?"
3441
count_parent_section_elements = 0
3542
parent = section
3643
while parent is not None:
@@ -39,17 +46,32 @@ def _process_sections():
3946
count_parent_section_elements += 1
4047
parent = parent.getparent()
4148
assert count_parent_section_elements > 0
42-
output_text += "{}* [{}](#{})\n".format(
43-
(" " * (4 * (count_parent_section_elements - 1))),
49+
_toc_text += "{}* [{}](#{})\n".format(
50+
(" " * (3 * (count_parent_section_elements - 1))),
4451
docbook5_title, id2)
45-
print(output_text)
46-
return output_text
52+
return _toc_text
4753

48-
_process_sections()
49-
if 0:
50-
for section in sections.xpath_results:
51-
print(section.get(id_attr))
54+
return _process_sections()
55+
56+
57+
def main(argv):
58+
parser = argparse.ArgumentParser(
59+
prog='PROG',
60+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
61+
parser.add_argument('--output', type=str, required=True,
62+
help='output filename')
63+
parser.add_argument('--input', type=str, required=True,
64+
help='Input filename')
65+
args = parser.parse_args(argv[1:])
66+
out_s = ''
67+
with open(args.input, "rt") as fh:
68+
out_s += fh.read()
69+
output_toc = MyTests().test_initial_docbook()
70+
output_text = output_toc + out_s
71+
72+
with open(args.output, "wt") as ofh:
73+
ofh.write(output_text)
5274

5375

5476
if __name__ == '__main__':
55-
MyTests().test_initial_docbook()
77+
main(sys.argv)

0 commit comments

Comments
 (0)