From c417cba981e484c73636b648bc7db2f47b7c8c65 Mon Sep 17 00:00:00 2001 From: Cyrille Rossant Date: Fri, 17 Nov 2017 15:32:07 +0100 Subject: [PATCH] Improve build_toc --- README.md | 5 ++++- build_toc.py | 11 ++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8de45bc..2fe5480 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ A curated list of awesome mathematics resources. # Contents + + +* [Contents](#contents) * [General Resources](#general-resources) * [Learning Platforms](#learning-platforms) * [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) * [License](#license) - + # General Resources diff --git a/build_toc.py b/build_toc.py index 4aa4ad9..20486e6 100644 --- a/build_toc.py +++ b/build_toc.py @@ -7,7 +7,8 @@ import re _HEADER_REGEX = r'([#]+) ([^\n]+)' _PUNCTUATION_REGEX = r'[^\w\- ]' _HEADER_TEMPLATE = '{indent}* [{name}](#{anchor})' -_NEWLINES = '\n\n\n' +_START_TOC = '' +_END_TOC = '' def _anchor(name): @@ -50,15 +51,15 @@ def _read_md(filename): def gen_toc(filename): md = _read_md(filename) - i = md.index(_NEWLINES) - j = md.index('# General Resources', i) + i = md.index(_START_TOC) + len(_START_TOC) + 2 + j = md.index(_END_TOC) with open(filename, 'w') as f: - f.write(md[:i] + _NEWLINES) + f.write(md[:i]) for item in _gen_items(md): if 'Awesome Math' in item: continue f.write(item + '\n') - f.write(_NEWLINES + md[j:]) + f.write('\n' + md[j:]) if __name__ == '__main__':