From 2cf206563553e5c6844aa65e9eee4d1d59fa4151 Mon Sep 17 00:00:00 2001 From: Trilarion Date: Fri, 1 Dec 2017 09:56:27 +0100 Subject: [PATCH] maintenance script that counts and orders alphabetically --- README.md | 12 ++++---- library/_toc.md | 2 +- maintenance.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ strategy/_toc.md | 4 +-- 4 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 maintenance.py diff --git a/README.md b/README.md index eb46faa2..84a93ef9 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ Help: [MarkDown Help](https://help.github.com/articles/github-flavored-markdown) ## Contents -- [Libraries](library/_toc.md) -- [Frameworks](framework/_toc.md) -- [Adventures](adventure/_toc.md) -- [RPGs](rpg/_toc.md) -- [Simulation](simulation/_toc.md) -- [Strategy](strategy/_toc.md) +- [Libraries](library/_toc.md) (2) +- [Frameworks](framework/_toc.md) (0) +- [Adventures](adventure/_toc.md) (0) +- [RPGs](rpg/_toc.md) (0) +- [Simulation](simulation/_toc.md) (1) +- [Strategy](strategy/_toc.md) (3) ## License diff --git a/library/_toc.md b/library/_toc.md index b53b0092..463a9c9d 100644 --- a/library/_toc.md +++ b/library/_toc.md @@ -1,4 +1,4 @@ # Popular required libraries +- [Boost](boost.md) - [Simple DirectMedia Layer (SDL 2)](sdl_2.md) -- [Boost](boost.md) \ No newline at end of file diff --git a/maintenance.py b/maintenance.py new file mode 100644 index 00000000..6dadd97a --- /dev/null +++ b/maintenance.py @@ -0,0 +1,78 @@ +""" + Counts the number of records each subfolder and updates the overview. Sorts the entries in the contents files of + each subfolder alphabetically. +""" + +import os +import re + +readme_regex = re.compile(r"- \[(.+)\]\((.+)\/_toc.md\)") +toc_regex = re.compile(r"- \[(.+)\]\((.+)\)") + +if __name__ == "__main__": + + # readme file location + base_path = os.path.abspath(os.path.dirname(__file__)) + readme_path = os.path.join(base_path, 'README.md') + + # read readme + with open(readme_path) as f: + readme_lines = f.readlines() + + # apply regex search on all lines + matched_lines = [readme_regex.findall(line) for line in readme_lines] + + # empty subfolder list + subfolders = [] + + # loop over the lines + for line, match in enumerate(matched_lines): + if match: + # get first group (should be only one) + match = match[0] + + # add to subfolders list + subfolders.append(match[1]) + + # subfolder path + subfolder_path = os.path.join(base_path, match[1]) + + # get number of files in that path (-1 for _toc.md) + n = len(os.listdir(subfolder_path)) - 1 + + # generate new line + readme_lines[line] = "- [{}]({}/_toc.md) ({})\n".format(match[0], match[1], n) + + # write readme again + with open(readme_path, "w") as f: + f.writelines(readme_lines) + + # loop over all subfolders + for subfolder in subfolders: + + # get contents file of that subfolder + toc_path = os.path.join(base_path, subfolder, '_toc.md') + + # read contents file + with open(toc_path) as f: + toc = f.readlines() + + # only if there are at least 4 lines (header, empty, two entries) + if len(toc) >= 4: + + # apply regex search on all entries (should work on all) + matched_entries = [toc_regex.findall(line)[0] for line in toc[2:]] + + # sort according to first entry + matched_entries.sort(key=lambda x: x[0]) + + # generate links again + lines = ["- [{}]({})\n".format(*match) for match in matched_entries] + + # reassemble toc + toc = toc[0:2] + toc.extend(lines) + + # write contents file again + with open(toc_path, "w") as f: + f.writelines(toc) diff --git a/strategy/_toc.md b/strategy/_toc.md index 6544bdec..72543146 100644 --- a/strategy/_toc.md +++ b/strategy/_toc.md @@ -1,5 +1,5 @@ # Strategy games -- [Freeciv WebGL](freeciv_web.md) +- [Battle for Wesnoth](wesnoth.md) - [Freeciv](freeciv.md) -- [Battle for Wesnoth](wesnoth.md) \ No newline at end of file +- [Freeciv WebGL](freeciv_web.md)