mirror of
https://github.com/mailscope/kumomta.git
synced 2026-08-20 19:38:18 +00:00
831bfe9aab
Rather than edit SUMMARY.md, you edit docs/generate-toc.py and it will flesh out sections based on the list of files in a directory.
95 lines
2.7 KiB
Python
Executable File
95 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import base64
|
|
import glob
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
class Page(object):
|
|
""" A page in the TOC, and its optional children """
|
|
def __init__(self, title, filename, children=None):
|
|
self.title = title
|
|
self.filename = filename
|
|
self.children = children or []
|
|
|
|
def render(self, output, depth=0):
|
|
indent = " " * depth
|
|
bullet = "- " if depth > 0 else ""
|
|
output.write(f"{indent}{bullet}[{self.title}]({self.filename})\n")
|
|
for kid in self.children:
|
|
kid.render(output, depth + 1)
|
|
|
|
|
|
class Gen(object):
|
|
""" autogenerate an index page from the contents of a directory """
|
|
def __init__(self, title, dirname, index=None, extract_title=False):
|
|
self.title = title
|
|
self.dirname = dirname
|
|
self.index = index
|
|
self.extract_title = extract_title
|
|
|
|
def render(self, output, depth=0):
|
|
print(self.dirname)
|
|
names = sorted(glob.glob(f"{self.dirname}/*.md"))
|
|
children = []
|
|
for filename in names:
|
|
title = os.path.basename(filename).rsplit(".", 1)[0]
|
|
if title == "index":
|
|
continue
|
|
|
|
if self.extract_title:
|
|
with open(filename, "r") as f:
|
|
title = f.readline().strip('#').strip()
|
|
|
|
children.append(Page(title, filename))
|
|
|
|
index_filename = f"{self.dirname}/index.markdown"
|
|
index_page = Page(self.title, index_filename, children=children)
|
|
index_page.render(output, depth)
|
|
with open(index_filename, "w") as idx:
|
|
if self.index:
|
|
idx.write(self.index)
|
|
idx.write("\n\n")
|
|
else:
|
|
try:
|
|
with open(f"{self.dirname}/index.md", "r") as f:
|
|
idx.write(f.read())
|
|
idx.write("\n\n")
|
|
except FileNotFoundError:
|
|
pass
|
|
for page in children:
|
|
idx.write(f" - [{page.title}]({os.path.basename(page.filename)})\n")
|
|
|
|
|
|
TOC = [
|
|
Page(
|
|
"KumoMTA",
|
|
"index.md",
|
|
children=[
|
|
Page(
|
|
"Install",
|
|
"installation.md",
|
|
),
|
|
Page("Change Log", "changelog.md"),
|
|
Page(
|
|
"Lua Reference",
|
|
"reference/index.md",
|
|
children=[
|
|
Gen(
|
|
"module: kumo",
|
|
"reference/kumo",
|
|
),
|
|
],
|
|
),
|
|
],
|
|
)
|
|
]
|
|
|
|
os.chdir("docs")
|
|
with open("SUMMARY.md", "w") as f:
|
|
f.write("<!-- this is auto-generated by docs/generate-toc.py, do not edit -->\n")
|
|
for page in TOC:
|
|
page.render(f)
|