Improve build_toc

This commit is contained in:
Cyrille Rossant 2017-11-17 15:32:07 +01:00
parent d36a935936
commit c417cba981
2 changed files with 10 additions and 6 deletions

View File

@ -4,6 +4,9 @@ A curated list of awesome mathematics resources.
# Contents # Contents
<!-- START_TOC -->
* [Contents](#contents)
* [General Resources](#general-resources) * [General Resources](#general-resources)
* [Learning Platforms](#learning-platforms) * [Learning Platforms](#learning-platforms)
* [Learn to Learn](#learn-to-learn) * [Learn to Learn](#learn-to-learn)
@ -51,7 +54,7 @@ A curated list of awesome mathematics resources.
* [Mathematics for Computer Science](#mathematics-for-computer-science) * [Mathematics for Computer Science](#mathematics-for-computer-science)
* [License](#license) * [License](#license)
<!-- END_TOC -->
# General Resources # General Resources

View File

@ -7,7 +7,8 @@ import re
_HEADER_REGEX = r'([#]+) ([^\n]+)' _HEADER_REGEX = r'([#]+) ([^\n]+)'
_PUNCTUATION_REGEX = r'[^\w\- ]' _PUNCTUATION_REGEX = r'[^\w\- ]'
_HEADER_TEMPLATE = '{indent}* [{name}](#{anchor})' _HEADER_TEMPLATE = '{indent}* [{name}](#{anchor})'
_NEWLINES = '\n\n\n' _START_TOC = '<!-- START_TOC -->'
_END_TOC = '<!-- END_TOC -->'
def _anchor(name): def _anchor(name):
@ -50,15 +51,15 @@ def _read_md(filename):
def gen_toc(filename): def gen_toc(filename):
md = _read_md(filename) md = _read_md(filename)
i = md.index(_NEWLINES) i = md.index(_START_TOC) + len(_START_TOC) + 2
j = md.index('# General Resources', i) j = md.index(_END_TOC)
with open(filename, 'w') as f: with open(filename, 'w') as f:
f.write(md[:i] + _NEWLINES) f.write(md[:i])
for item in _gen_items(md): for item in _gen_items(md):
if 'Awesome Math' in item: if 'Awesome Math' in item:
continue continue
f.write(item + '\n') f.write(item + '\n')
f.write(_NEWLINES + md[j:]) f.write('\n' + md[j:])
if __name__ == '__main__': if __name__ == '__main__':