Implement generate_toc.py

Takumi Sueda 2020-10-18 02:56:43 +09:00
parent 456d08f9b2
commit 7f3ee3f52f
9 changed files with 64 additions and 0 deletions

0
Linux_ビルド.md Normal file

@ -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

0
U-Boot_起動 (EBOOT).md Normal file

10
_Sidebar.md Normal file

@ -0,0 +1,10 @@
<!-- This file is generated by generate_toc.py. DO NOT EDIT MANUALLY! -->
# 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)

47
generate_toc.py Normal file

@ -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('<!-- This file is generated by generate_toc.py. DO NOT EDIT MANUALLY! -->\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()

1
requirements.txt Normal file

@ -0,0 +1 @@
markdown