From 7f3ee3f52fc153d9b3c882070a3602dddd043bd4 Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Sun, 18 Oct 2020 02:56:43 +0900 Subject: [PATCH] Implement generate_toc.py --- Linux_ビルド.md | 0 Makefile | 6 +++++ U-Boot_ビルド_buildbrain.md | 0 U-Boot_ビルド_手動.md | 0 U-Boot_起動 (EBOOT).md | 0 U-Boot_起動 (USB Recovery).md | 0 _Sidebar.md | 10 ++++++++ generate_toc.py | 47 +++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 9 files changed, 64 insertions(+) create mode 100644 Linux_ビルド.md create mode 100644 U-Boot_ビルド_buildbrain.md create mode 100644 U-Boot_ビルド_手動.md create mode 100644 U-Boot_起動 (EBOOT).md create mode 100644 U-Boot_起動 (USB Recovery).md create mode 100644 _Sidebar.md create mode 100644 generate_toc.py create mode 100644 requirements.txt diff --git a/Linux_ビルド.md b/Linux_ビルド.md new file mode 100644 index 0000000..e69de29 diff --git a/Makefile b/Makefile index 517e352..7960983 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,13 @@ .PHONY: install-dependency: npm install -g doctoc + pip3 install -r requirements.txt .PHONY: doctoc: doctoc --title "### 目次" . + +.PHONY: +generate-sidebar: + python3 generate_toc.py + diff --git a/U-Boot_ビルド_buildbrain.md b/U-Boot_ビルド_buildbrain.md new file mode 100644 index 0000000..e69de29 diff --git a/U-Boot_ビルド_手動.md b/U-Boot_ビルド_手動.md new file mode 100644 index 0000000..e69de29 diff --git a/U-Boot_起動 (EBOOT).md b/U-Boot_起動 (EBOOT).md new file mode 100644 index 0000000..e69de29 diff --git a/U-Boot_起動 (USB Recovery).md b/U-Boot_起動 (USB Recovery).md new file mode 100644 index 0000000..e69de29 diff --git a/_Sidebar.md b/_Sidebar.md new file mode 100644 index 0000000..09f3e9f --- /dev/null +++ b/_Sidebar.md @@ -0,0 +1,10 @@ + + +# Linux + - [ビルド](Linux_%E3%83%93%E3%83%AB%E3%83%89.md) +# U-Boot + - [起動 (EBOOT)](U-Boot_%E8%B5%B7%E5%8B%95%20%28EBOOT%29.md) + - [起動 (USB Recovery)](U-Boot_%E8%B5%B7%E5%8B%95%20%28USB%20Recovery%29.md) +## ビルド + - [buildbrain](U-Boot_%E3%83%93%E3%83%AB%E3%83%89_buildbrain.md) + - [手動](U-Boot_%E3%83%93%E3%83%AB%E3%83%89_%E6%89%8B%E5%8B%95.md) diff --git a/generate_toc.py b/generate_toc.py new file mode 100644 index 0000000..1e0669d --- /dev/null +++ b/generate_toc.py @@ -0,0 +1,47 @@ +from pathlib import Path +from urllib.parse import quote +import markdown + + +def main(): + fn_tree = dict() + idfn = dict() + + for fn in sorted(Path('.').glob("*.md")): + if fn.name in {'Home.md', '_Sidebar.md', '_Footer.md'}: + continue + + fn_tokens = fn.name[:-3].split('_') + ptr = fn_tree + for t in fn_tokens: + ptr[t] = ptr.get(t, dict()) + ptr = ptr[t] + + idfn[id(ptr)] = quote(fn.name) + + md = markdown.Markdown(extensions=['toc']) + + with open(str(fn)) as f: + raw = f.read() + + md.convert(raw) + + with open('_Sidebar.md', 'w') as f: + f.write('\n\n') + recurse(f, fn_tree, idfn) + + +def recurse(f, d, idfn, level=1): + for k, child_dict in d.items(): + if child_dict: + continue + f.write(f' - [{k}]({idfn[id(child_dict)]})\n') + + for k, child_dict in d.items(): + if not child_dict: + continue + f.write(f'#'*level + f' {k}\n') + recurse(f, child_dict, idfn, level=level+1) + + +main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3cda9eb --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +markdown \ No newline at end of file