microblog.pub/scripts/build_docs.py

67 lines
1.9 KiB
Python
Raw Normal View History

2022-07-05 09:15:45 +02:00
import shutil
2022-07-04 19:02:06 +02:00
from pathlib import Path
from jinja2 import Environment
from jinja2 import FileSystemLoader
from jinja2 import select_autoescape
from markdown import markdown
2022-07-04 22:00:19 +02:00
from app.config import VERSION
from app.utils.datetime import now
2022-07-04 22:00:19 +02:00
2022-07-04 19:02:06 +02:00
def markdownify(content: str) -> str:
2022-07-05 21:36:20 +02:00
return markdown(
content, extensions=["mdx_linkify", "fenced_code", "codehilite", "toc"]
)
2022-07-04 19:02:06 +02:00
def main() -> None:
# Setup Jinja
loader = FileSystemLoader("docs/templates")
env = Environment(loader=loader, autoescape=select_autoescape())
template = env.get_template("layout.html")
2022-07-05 09:15:45 +02:00
shutil.rmtree("docs/dist", ignore_errors=True)
2022-07-04 19:02:06 +02:00
Path("docs/dist").mkdir(exist_ok=True)
2022-07-05 09:15:45 +02:00
shutil.rmtree("docs/dist/static", ignore_errors=True)
shutil.copytree("docs/static", "docs/dist/static")
2022-07-04 19:02:06 +02:00
2022-07-11 22:01:37 +02:00
last_updated = now().isoformat()
2022-07-04 19:02:06 +02:00
readme = Path("README.md")
template.stream(
2022-07-04 22:00:19 +02:00
content=markdownify(readme.read_text().removeprefix("# microblog.pub")),
version=VERSION,
2022-07-05 09:32:43 +02:00
path="/",
2022-07-11 22:04:54 +02:00
last_updated=last_updated,
2022-07-04 19:02:06 +02:00
).dump("docs/dist/index.html")
2022-07-05 09:15:45 +02:00
install = Path("docs/install.md")
template.stream(
2022-07-05 21:35:39 +02:00
content=markdownify(install.read_text()),
2022-07-05 09:15:45 +02:00
version=VERSION,
2022-07-05 09:32:43 +02:00
path="/installing.html",
2022-07-11 22:01:37 +02:00
last_updated=last_updated,
2022-07-05 09:15:45 +02:00
).dump("docs/dist/installing.html")
2022-07-05 21:35:39 +02:00
user_guide = Path("docs/user_guide.md")
template.stream(
content=markdownify(user_guide.read_text()),
version=VERSION,
path="/user_guide.html",
2022-07-11 22:01:37 +02:00
last_updated=last_updated,
2022-07-05 21:35:39 +02:00
).dump("docs/dist/user_guide.html")
2022-07-11 22:01:37 +02:00
developer_guide = Path("docs/developer_guide.md")
template.stream(
content=markdownify(developer_guide.read_text()),
version=VERSION,
path="/developer_guide.html",
last_updated=last_updated,
).dump("docs/dist/developer_guide.html")
2022-07-04 19:02:06 +02:00
if __name__ == "__main__":
main()