From c86b7b3480fba1d7641581a205c2d55b860527ed Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Fri, 24 Sep 2021 16:59:58 +0800 Subject: [PATCH] feat: add notes share --- 筆記分享/README.md | 1 + 筆記分享/_sidebar.md | 1 + 筆記分享/index.html | 56 +++++++++++++++++++++++ 筆記分享/init.py | 105 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 筆記分享/README.md create mode 100644 筆記分享/_sidebar.md create mode 100644 筆記分享/index.html create mode 100644 筆記分享/init.py diff --git a/筆記分享/README.md b/筆記分享/README.md new file mode 100644 index 0000000..8e36fa2 --- /dev/null +++ b/筆記分享/README.md @@ -0,0 +1 @@ +# 筆記分享 \ No newline at end of file diff --git a/筆記分享/_sidebar.md b/筆記分享/_sidebar.md new file mode 100644 index 0000000..6739c43 --- /dev/null +++ b/筆記分享/_sidebar.md @@ -0,0 +1 @@ +* [Overview]() diff --git a/筆記分享/index.html b/筆記分享/index.html new file mode 100644 index 0000000..89614e8 --- /dev/null +++ b/筆記分享/index.html @@ -0,0 +1,56 @@ + + + + + Document + + + + + + +
+ + + + + + + + + + + + + + + + + + + diff --git a/筆記分享/init.py b/筆記分享/init.py new file mode 100644 index 0000000..d0202c1 --- /dev/null +++ b/筆記分享/init.py @@ -0,0 +1,105 @@ +import os +import glob +import sys +from urllib.parse import quote + +import platform + + +path_split = '/' +if platform.system() == 'Windows': + path_split = '\\' + + +IGNORE = [ + 'src', + '.git', + 'vendor', + 'docs', + 'assets', + "README.md", +] +HEADER = [ + "* [Overview]()" +] +FOOTER = [ + "", + "

", + "", + "* [Back](../)" +] + +def make_display_name(name): + name = name.split(".md")[0] # Remove .md extension + name = name.replace('-', ' ') # Add space instead of - + name = name.replace('_', ' ') # Add space instead of _ + # Capitalize all words + # (Exclude some words from capitalization) + forbidden = ['a', 'on', 'to', 'and', 'with', 'how', 'at', 'the'] + capitalized = '' + for word in name.split(' '): + if (word.lower() not in forbidden): + capitalized += word[0].upper() + word[1:] + # capitalized += word.capitalize() + else: + capitalized += word.lower() + capitalized += ' ' + name = capitalized.strip() + + return name + +def generate_sidebar(path, entries): + print(path) + sidebar_file = open(os.path.join(path,'_sidebar.md'), 'w', encoding="utf-8") + for entry in entries: + sidebar_file.write(entry+'\n') + sidebar_file.close() + +def acceptfile(name): + if os.path.splitext(name)[1] != ".md": return False + if name in IGNORE: return False + if name.startswith('.') or name.startswith('_'): return False + return True + +def acceptdir(root,d): + if d in IGNORE: return False + # if not acceptroot(root): return False + if os.path.exists(os.path.join(root,d,'README.md')): return True + return False + +def acceptroot(root): + parts = root.split(path_split) + if len(parts) > 1: + for part in parts: + if part in IGNORE: return False + return True + +def scan_dir(path="."): + for root, dirs, files in os.walk(path): + dirs = sorted(dirs) + files = sorted(files) + entries = [] + if len(root.split(path_split)) == 1: + entries += HEADER + elif not acceptroot(root): continue + else: + entries.append("* [{}]()".format(make_display_name(root.split(path_split)[-1])) ) + stop = [] + for d in dirs: + if not acceptdir(root,d): + stop.append(d) + continue + entries.append("* [{}](./{}/)".format(make_display_name(d), quote(d))) + for d in stop: + dirs.remove(d) + for f in files: + if not acceptfile(f): continue + entries.append("* [{}]({})".format(make_display_name(f), quote(f))) + if len(root.split(path_split)) > 1: + entries += FOOTER + generate_sidebar(root, entries) + +if __name__ == "__main__": + scan_dir() + +print('✅ All done!')