ref: 3e5b6322e1167758fedaec5b3930eadaeaa8ec33
parent: 00f28bb8f4ff81b51aaf7eab7ae413218bda7843
author: Remy Oukaour <[email protected]>
date: Wed Dec 13 11:57:29 EST 2017
Don't bother supporting numbered lists
--- a/tools/toc.py
+++ b/tools/toc.py
@@ -5,7 +5,6 @@
Usage: python3 toc.py [-n] files.md...
Replace a "## TOC" heading in a Markdown file with a table of contents,
generated from the other headings in the file. Supports multiple files.
-Use "-n" for numbered list items.
Headings must start with "##" signs to be detected.
"""
@@ -46,7 +45,7 @@
anchor = name_to_anchor(name)
yield TocItem(name, anchor, level)
-def toc_string(toc_items, numeric):
+def toc_string(toc_items):
lines = ['## %s' % toc_name, '']
for name, anchor, level in toc_items:
padding = ' ' * level
@@ -54,7 +53,7 @@
lines.append(line)
return '\n'.join(lines) + '\n'
-def add_toc(filename, numeric):
+def add_toc(filename):
with open(filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
toc_index = get_toc_index(lines)
@@ -66,7 +65,7 @@
with open(filename, 'w', encoding='utf-8') as f:
for i, line in enumerate(lines):
if i == toc_index:
- f.write(toc_string(toc_items, numeric))
+ f.write(toc_string(toc_items))
else:
f.write(line)
return True # OK
@@ -73,20 +72,12 @@
def main():
if len(sys.argv) < 2:
- print('*** ERROR: Not enough arguments')
+ print('*** ERROR: No filenames specified')
print(__doc__)
exit(1)
- del sys.argv[0]
- numeric = False
- if sys.argv[0] == '-n':
- numeric = True
- del sys.argv[0]
- if not sys.argv:
- print('*** ERROR: No filenames specified')
- exit(1)
- for filename in sys.argv:
+ for filename in sys.argv[1:]:
print(filename)
- result = add_toc(filename, numeric)
+ result = add_toc(filename)
if result is None:
print('*** WARNING: No "## TOC" heading found')
elif result is False: