|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #!/usr/bin/env python3
-
- import codecs
- import os
- from pathlib import Path
- from time import perf_counter
-
- import markdown
- from jinja2 import Environment as Env
- from jinja2 import FileSystemLoader
- from minicli import cli, run, wrap
-
- HERE = Path(".")
- FAMILLE = HERE / "famille"
-
-
- environment = Env(loader=FileSystemLoader(str(FAMILLE / "templates")))
-
-
- def parse_markdown(file_path):
- """Extract content including footnotes from a markdown file."""
- parser = markdown.Markdown(extensions=["footnotes"])
- with codecs.open(file_path, "r") as source:
- return parser.convert(source.read())
-
-
- def collection():
- """Retrieve famille's chapters from markdown files."""
- SRC_PATH = FAMILLE / "sources"
- for chapter in sorted(os.listdir(SRC_PATH)):
- if chapter.endswith(".md"):
- content = parse_markdown(SRC_PATH / chapter)
- yield (
- """<article id="{id}">
- {content}
- </article>""".format(
- id=chapter[:-3], content=content.replace("'", "’")
- )
- )
-
-
- @cli
- def generate():
- template = environment.get_template("histoire.html")
- page = template.render(chapters=collection())
- open(FAMILLE / "histoire" / "index.html", "w").write(page)
-
-
- @wrap
- def perf_wrapper():
- start = perf_counter()
- yield
- elapsed = perf_counter() - start
- print(f"Done in {elapsed:.5f} seconds.")
-
-
- if __name__ == "__main__":
- run()
|