nvchecker/scripts/gen_contents
2017-02-28 15:27:24 +08:00

43 lines
763 B
Python
Executable File

#!/usr/bin/env python3
# MIT licensed
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import sys
import re
sections = re.compile(r'[-=]{3,}')
levels = {
'=': 1,
'-': 2,
}
def to_id(t):
return re.sub(r'[`(),]', '', t).lower().replace(' ', '-')
def to_text(t):
return t.replace('`', '')
def main(f):
L = []
last = None
for l in f:
l = l[:-1]
if sections.fullmatch(l):
L.append((last, levels[l[0]]))
last = l
last_lvl = 1
for title, lvl in L:
indent = ' ' * (lvl - 1)
if last_lvl != lvl:
print()
print('{indent}* `{text} <#{id}>`_'.format(
indent = indent,
text = to_text(title),
id = to_id(title),
))
last_lvl = lvl
if __name__ == '__main__':
main(sys.stdin)