microblog.pub/scripts/build_docs.py

167 lines
5.1 KiB
Python
Raw Permalink Normal View History

2022-10-19 18:46:01 +00:00
import re
2022-07-05 07:15:45 +00:00
import shutil
2022-10-19 18:46:01 +00:00
import typing
2022-07-04 17:02:06 +00:00
from pathlib import Path
2022-10-19 18:46:01 +00:00
from typing import Any
2022-07-04 17:02:06 +00:00
from jinja2 import Environment
from jinja2 import FileSystemLoader
from jinja2 import select_autoescape
2022-10-19 18:46:01 +00:00
from mistletoe import Document # type: ignore
from mistletoe import HTMLRenderer # type: ignore
from mistletoe import block_token # type: ignore
from pygments import highlight # type: ignore
from pygments.formatters import HtmlFormatter # type: ignore
from pygments.lexers import get_lexer_by_name as get_lexer # type: ignore
from pygments.lexers import guess_lexer # type: ignore
2022-07-04 17:02:06 +00:00
2022-07-04 20:00:19 +00:00
from app.config import VERSION
2022-10-19 18:46:01 +00:00
from app.source import CustomRenderer
from app.utils.datetime import now
2022-07-04 20:00:19 +00:00
2022-10-19 18:46:01 +00:00
_FORMATTER = HtmlFormatter()
_FORMATTER.noclasses = True
2022-07-04 17:02:06 +00:00
2022-10-19 18:46:01 +00:00
class DocRenderer(CustomRenderer):
def __init__(
self,
depth=5,
omit_title=True,
filter_conds=[],
) -> None:
super().__init__(
enable_mentionify=False,
enable_hashtagify=False,
)
self._headings: list[tuple[int, str, str]] = []
self._ids: set[str] = set()
self.depth = depth
self.omit_title = omit_title
self.filter_conds = filter_conds
@property
def toc(self):
"""
Returns table of contents as a block_token.List instance.
"""
def get_indent(level):
if self.omit_title:
level -= 1
return " " * 4 * (level - 1)
def build_list_item(heading):
level, content, title_id = heading
template = '{indent}- <a href="#{id}" rel="nofollow">{content}</a>\n'
return template.format(
indent=get_indent(level), content=content, id=title_id
)
lines = [build_list_item(heading) for heading in self._headings]
items = block_token.tokenize(lines)
return items[0]
def render_heading(self, token):
"""
Overrides super().render_heading; stores rendered heading first,
then returns it.
"""
template = '<h{level} id="{id}">{inner}</h{level}>'
inner = self.render_inner(token)
title_id = inner.lower().replace(" ", "-")
if title_id in self._ids:
i = 1
while 1:
title_id = f"{title_id}_{i}"
if title_id not in self._ids:
break
self._ids.add(title_id)
rendered = template.format(level=token.level, inner=inner, id=title_id)
content = self.parse_rendered_heading(rendered)
if not (
self.omit_title
and token.level == 1
or token.level > self.depth
or any(cond(content) for cond in self.filter_conds)
):
self._headings.append((token.level, content, title_id))
return rendered
@staticmethod
def parse_rendered_heading(rendered):
"""
Helper method; converts rendered heading to plain text.
"""
return re.sub(r"<.+?>", "", rendered)
def render_block_code(self, token: typing.Any) -> str:
code = token.children[0].content
lexer = get_lexer(token.language) if token.language else guess_lexer(code)
return highlight(code, lexer, _FORMATTER)
def markdownify(content: str) -> tuple[str, Any]:
with DocRenderer() as renderer:
rendered_content = renderer.render(Document(content))
with HTMLRenderer() as html_renderer:
toc = html_renderer.render(renderer.toc)
return rendered_content, toc
2022-07-04 17:02:06 +00: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 07:15:45 +00:00
shutil.rmtree("docs/dist", ignore_errors=True)
2022-07-04 17:02:06 +00:00
Path("docs/dist").mkdir(exist_ok=True)
2022-07-05 07:15:45 +00:00
shutil.rmtree("docs/dist/static", ignore_errors=True)
shutil.copytree("docs/static", "docs/dist/static")
2022-07-04 17:02:06 +00:00
2022-07-30 07:43:36 +00:00
last_updated = now().replace(second=0, microsecond=0).isoformat()
2022-07-11 20:01:37 +00:00
2022-07-04 17:02:06 +00:00
readme = Path("README.md")
2022-10-19 18:46:01 +00:00
content, toc = markdownify(readme.read_text().removeprefix("# microblog.pub"))
2022-07-04 17:02:06 +00:00
template.stream(
2022-10-19 18:46:01 +00:00
content=content,
2022-07-04 20:00:19 +00:00
version=VERSION,
2022-07-05 07:32:43 +00:00
path="/",
2022-07-11 20:04:54 +00:00
last_updated=last_updated,
2022-07-04 17:02:06 +00:00
).dump("docs/dist/index.html")
2022-07-05 07:15:45 +00:00
install = Path("docs/install.md")
2022-10-19 18:46:01 +00:00
content, toc = markdownify(install.read_text())
2022-07-05 07:15:45 +00:00
template.stream(
2022-10-19 18:46:01 +00:00
content=content.replace("[TOC]", toc),
2022-07-05 07:15:45 +00:00
version=VERSION,
2022-07-05 07:32:43 +00:00
path="/installing.html",
2022-07-11 20:01:37 +00:00
last_updated=last_updated,
2022-07-05 07:15:45 +00:00
).dump("docs/dist/installing.html")
2022-07-05 19:35:39 +00:00
user_guide = Path("docs/user_guide.md")
2022-10-19 18:46:01 +00:00
content, toc = markdownify(user_guide.read_text())
2022-07-05 19:35:39 +00:00
template.stream(
2022-10-19 18:46:01 +00:00
content=content.replace("[TOC]", toc),
2022-07-05 19:35:39 +00:00
version=VERSION,
path="/user_guide.html",
2022-07-11 20:01:37 +00:00
last_updated=last_updated,
2022-07-05 19:35:39 +00:00
).dump("docs/dist/user_guide.html")
2022-07-11 20:01:37 +00:00
developer_guide = Path("docs/developer_guide.md")
2022-10-19 18:46:01 +00:00
content, toc = markdownify(developer_guide.read_text())
2022-07-11 20:01:37 +00:00
template.stream(
2022-10-19 18:46:01 +00:00
content=content.replace("[TOC]", toc),
2022-07-11 20:01:37 +00:00
version=VERSION,
path="/developer_guide.html",
last_updated=last_updated,
).dump("docs/dist/developer_guide.html")
2022-07-04 17:02:06 +00:00
if __name__ == "__main__":
main()