From 225bd2696fa228d4ce01ab5a7695ac8afa2d9a20 Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Mon, 21 Jan 2019 16:34:16 +0200 Subject: [PATCH] Add `tools/update_toc.py` to automatically replace TOC --- tools/update_toc.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tools/update_toc.py diff --git a/tools/update_toc.py b/tools/update_toc.py new file mode 100644 index 0000000..47db34b --- /dev/null +++ b/tools/update_toc.py @@ -0,0 +1,44 @@ +import re +from subprocess import check_output + + +def get_new_toc(): + new_toc = check_output( + 'markdown-toc README.md', + shell=True + ).decode('utf-8') + + pattern = ['', '', new_toc, '', ''] + + return '\n'.join(pattern) + + +def get_readme(): + with open('README.md', 'r') as f: + return f.read() + + +def save_readme(readme): + with open('README.md', 'w') as f: + return f.write(readme) + + +def replace_toc(): + readme = get_readme() + new_toc = get_new_toc() + + regex = '(.|\n)+' + + new_readme = re.sub(regex, new_toc, readme) + + save_readme(new_readme) + + print('TOC updated ...') + + +def main(): + return replace_toc() + + +if __name__ == '__main__': + main()