Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

site.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #!/usr/bin/env python3
  2. import fnmatch
  3. import locale
  4. import os
  5. from collections import defaultdict
  6. from dataclasses import dataclass
  7. from datetime import datetime, timedelta
  8. from html import escape
  9. from itertools import groupby
  10. from pathlib import Path
  11. from string import Template
  12. from textwrap import dedent
  13. from time import perf_counter
  14. import mistune
  15. from jinja2 import Environment as Env
  16. from jinja2 import FileSystemLoader
  17. from minicli import cli, run, wrap
  18. from mistune.directives import DirectiveInclude
  19. from PIL import Image
  20. from slugify import slugify
  21. from typography import typographie
  22. # Useful for dates rendering within Jinja2.
  23. locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
  24. HERE = Path(".")
  25. DAVID = HERE / "david"
  26. STATIC = HERE / ".." / "larlet-fr-static"
  27. DOMAIN = "https://larlet.fr"
  28. LOCAL_DOMAIN = "http://larlet.test:3579"
  29. # Hardcoding publication at 12 in Paris timezone.
  30. NORMALIZED_STRFTIME = "%Y-%m-%dT12:00:00+01:00"
  31. TODAY = datetime.today() + timedelta(hours=6)
  32. PUBLICATION_BUFFER = TODAY - timedelta(days=7)
  33. NB_ITEMS_IN_FEED = 30
  34. all_tags = set()
  35. pages_by_tags = defaultdict(list)
  36. pages_by_url = {}
  37. class MarkParser(mistune.InlineParser):
  38. """Parses `==foo==` as `<mark>foo</mark>`."""
  39. MARK = (
  40. r"(\={2})(?=[^\s*])("
  41. r"(?:\\[\\*]|[^*])*"
  42. r"(?:" + mistune.InlineParser.ESCAPE + r"|[^\s*]))\1"
  43. )
  44. RULE_NAMES = mistune.InlineParser.RULE_NAMES + ("mark",)
  45. def parse_mark(self, m, state):
  46. marker = m.group(1)
  47. text = m.group(2)
  48. return "mark", self.render(text, state)
  49. class MarkRenderer(mistune.HTMLRenderer):
  50. """To use in conjunction with `MarkParser`."""
  51. def mark(self, text):
  52. return "<mark>" + text + "</mark>"
  53. class TagsRenderer(mistune.HTMLRenderer):
  54. """Make the asumption each line starting with a `#` is a tag."""
  55. def paragraph(self, text):
  56. if text.startswith("#"):
  57. tags = " ".join(
  58. f'<a href="/david/2021/{slugify(tag.strip())}/">#{tag.strip()}</a>'
  59. for tag in text.split("#")
  60. if tag.strip()
  61. )
  62. return f"<nav><p>{tags}</p></nav>\n"
  63. return super().paragraph(text)
  64. class FrenchTypographyRenderer(mistune.HTMLRenderer):
  65. """Apply French typographic rules to text."""
  66. def text(self, text):
  67. return typographie(super().text(text))
  68. class InternalLinkTitleRenderer(mistune.HTMLRenderer):
  69. """Automatically generate the title for internal links."""
  70. def link(self, link, text=None, title=None):
  71. if text is None:
  72. text = link
  73. s = '<a href="' + self._safe_url(link) + '"'
  74. if not title and link.startswith("/david/2021/"):
  75. # It will not work for internal links referencing the future.
  76. page = pages_by_url.get(link)
  77. if page:
  78. title = page.title
  79. if title:
  80. s += ' title="' + mistune.escape_html(title) + '"'
  81. return s + ">" + (text or link) + "</a>"
  82. class CustomAndBlockquoteLanguageRenderer(
  83. FrenchTypographyRenderer, InternalLinkTitleRenderer, MarkRenderer, TagsRenderer
  84. ):
  85. """Sets the English language attribute for blockquotes with `[en]` prefix."""
  86. def _get_language(self, text):
  87. if text.startswith("<p>[en] "):
  88. return "en", text.replace("<p>[en] ", "<p>")
  89. else:
  90. return None, text
  91. def block_quote(self, text):
  92. language, text = self._get_language(text)
  93. if language:
  94. return f'\n<blockquote lang="{language}">\n{text}</blockquote>\n'
  95. else:
  96. return f"\n<blockquote>\n{text}</blockquote>\n"
  97. class ImgsWithSizesRenderer(CustomAndBlockquoteLanguageRenderer):
  98. """Renders images as <figure>s and add sizes."""
  99. def paragraph(self, text):
  100. # In case of a figure, we do not want the (non-standard) paragraph.
  101. if text.strip().startswith("<figure>"):
  102. return text
  103. return super().paragraph(text)
  104. def image(self, src, alt="", title=None):
  105. full_path = STATIC / Path(src[1:])
  106. image = Image.open(full_path)
  107. width, height = image.size
  108. return dedent(
  109. f"""\
  110. <figure>
  111. <a href="{src}"
  112. title="Cliquer pour une version haute résolution">
  113. <img src="{src}" alt="{alt}"
  114. width="{width}" height="{height}" />
  115. </a>
  116. <figcaption>{title}</figcaption>
  117. </figure>
  118. """
  119. )
  120. class ImgsWithSizesAndLightboxRenderer(ImgsWithSizesRenderer):
  121. """Renders images as <figure>s and add sizes + lazy attribute.
  122. Also, implement a lightbox markup. See:
  123. https://www.sylvaindurand.org/overlay-image-in-pure-css/
  124. """
  125. def image(self, src, alt="", title=None):
  126. file_name = Path(src).stem
  127. full_path = STATIC / Path(src[1:])
  128. image = Image.open(full_path)
  129. width, height = image.size
  130. return dedent(
  131. f"""\
  132. <figure>
  133. <a href="#{file_name}"
  134. title="Cliquer pour une version haute résolution">
  135. <img src="{src}" alt="{alt}"
  136. loading="lazy" width="{width}" height="{height}" />
  137. </a>
  138. <a href="#_" class="lightbox" id="{file_name}">
  139. <img src="{src}" alt="{alt}"
  140. loading="lazy" width="{width}" height="{height}" />
  141. </a>
  142. <figcaption>{title}</figcaption>
  143. </figure>
  144. """
  145. )
  146. class H2AnchorsRenderer(ImgsWithSizesAndLightboxRenderer):
  147. """Custom renderer for H2 titles with anchors."""
  148. def heading(self, text, level):
  149. if level == 2:
  150. slug = slugify(text)
  151. return (
  152. f'<h2 id="{slug}">'
  153. f"{text} "
  154. f'<a href="#{slug}" title="Ancre vers cette partie">#</a>'
  155. f"</h2>"
  156. )
  157. else:
  158. return super().heading(text, level)
  159. # We want a custom renderer to create a hash/link for each H2 headings.
  160. markdown_with_h2_anchors = mistune.Markdown(
  161. renderer=H2AnchorsRenderer(escape=False),
  162. inline=MarkParser(H2AnchorsRenderer(escape=False)),
  163. plugins=[DirectiveInclude()],
  164. )
  165. # The second markdown is pertinent to generate articles for the feed,
  166. # we do not need anchors in that case.
  167. markdown_with_img_sizes = mistune.Markdown(
  168. renderer=ImgsWithSizesRenderer(escape=False),
  169. inline=MarkParser(ImgsWithSizesRenderer(escape=False)),
  170. plugins=[DirectiveInclude()],
  171. )
  172. # This is the jinja2 configuration to locate templates.
  173. environment = Env(loader=FileSystemLoader(str(DAVID / "templates")))
  174. def neighborhood(iterable, first=None, last=None):
  175. """
  176. Yield the (previous, current, next) items given an iterable.
  177. You can specify a `first` and/or `last` item for bounds.
  178. """
  179. iterator = iter(iterable)
  180. previous = first
  181. current = next(iterator) # Throws StopIteration if empty.
  182. for next_ in iterator:
  183. yield (previous, current, next_)
  184. previous = current
  185. current = next_
  186. yield (previous, current, last)
  187. def each_markdown_from(source_dir, file_name="*.md"):
  188. """Walk across the `source_dir` and return the md file paths."""
  189. for filename in fnmatch.filter(os.listdir(source_dir), file_name):
  190. yield filename
  191. @dataclass
  192. class Page:
  193. title: str
  194. content: str
  195. tags: list
  196. file_path: str
  197. lang: str = "fr"
  198. def __post_init__(self):
  199. date_str, _ = self.file_path.split(" - ", 1)
  200. self.url = f"/david/{date_str.replace('-', '/')}/"
  201. self.date = datetime.strptime(date_str, "%Y-%m-%d").date()
  202. self.full_url = f"{DOMAIN}{self.url}"
  203. self.normalized_date = self.date.strftime(NORMALIZED_STRFTIME)
  204. self.escaped_title = escape(self.title)
  205. tag_template = Template(
  206. f'<a href="{DOMAIN}/david/2021/$tag_slug/">#$tag_name</a>'
  207. )
  208. tag_links = " ".join(
  209. tag_template.substitute(tag_slug=slugify(tag), tag_name=tag)
  210. for tag in self.tags
  211. )
  212. self.escaped_content = escape(
  213. self.content.replace('href="/', f'href="{DOMAIN}/')
  214. .replace('src="/', f'src="{DOMAIN}/')
  215. .replace('href="#', f'href="{self.full_url}#')
  216. + f"<nav><p>{tag_links}</p></nav>"
  217. + '<hr/><p><a href="mailto:david@larlet.fr">Réagir ?</a></p>'
  218. )
  219. # Extract first paragraph.
  220. self.extract = self.content.split("</p>", 1)[0] + "</p>"
  221. def __eq__(self, other):
  222. return self.url == other.url
  223. def __lt__(self, other: "Page"):
  224. if not isinstance(other, Page):
  225. return NotImplemented
  226. return self.date < other.date
  227. @staticmethod
  228. def all(source: Path, only_published=True, with_h2_anchors=True):
  229. """Retrieve all pages sorted by desc."""
  230. page_list = []
  231. md = markdown_with_h2_anchors if with_h2_anchors else markdown_with_img_sizes
  232. for file_name in sorted(each_markdown_from(source)):
  233. result = md.read(source / file_name)
  234. # Extract (and remove) the title from the generated page.
  235. title, content = result.split("</h1>", 1)
  236. h1_opening_size = len("<h1>")
  237. title = title[h1_opening_size:]
  238. tags = {}
  239. if "<nav><p>" in content:
  240. # Extract the tags from the generated page.
  241. content, tags_links = content.split("<nav><p>", 1)
  242. nav_closing_size = len("</p></nav>\n")
  243. tags_links = tags_links[:-nav_closing_size]
  244. tags = sorted(
  245. {
  246. tag.strip().split("#", 1)[1]
  247. for tag in tags_links.split("</a>")
  248. if tag.strip()
  249. },
  250. key=lambda tag: slugify(tag),
  251. )
  252. page = Page(title, content, tags, file_name)
  253. pages_by_url[page.url] = page
  254. if not page.is_draft:
  255. all_tags.update(tags)
  256. for tag in tags:
  257. if page not in pages_by_tags[tag]:
  258. pages_by_tags[tag].append(page)
  259. if only_published and page.is_draft:
  260. continue
  261. page_list.append(page)
  262. return sorted(page_list, reverse=True)
  263. @property
  264. def is_draft(self):
  265. return (
  266. datetime(year=self.date.year, month=self.date.month, day=self.date.day)
  267. > PUBLICATION_BUFFER
  268. )
  269. @cli
  270. def pages():
  271. """Build article pages."""
  272. root_path = DAVID / "2021"
  273. source_path = root_path / "sources"
  274. for previous, page, next_ in neighborhood(
  275. reversed(Page.all(source=source_path, only_published=False)),
  276. first={
  277. "url": "/david/2020/",
  278. "title": "Publications 2020",
  279. "is_draft": False,
  280. },
  281. ):
  282. template = environment.get_template("article_2020.html")
  283. content = template.render(page=page, prev=previous, next=next_, slugify=slugify)
  284. target_path = Path(page.url[1:])
  285. target_path.mkdir(parents=True, exist_ok=True)
  286. open(target_path / "index.html", "w").write(content)
  287. if page.is_draft:
  288. print(f"Draft: {LOCAL_DOMAIN}{page.url} ({page.title})")
  289. template = environment.get_template("archives_2020.html")
  290. content = template.render(page_list=reversed(Page.all(source=source_path)))
  291. open(root_path / "index.html", "w").write(content)
  292. @cli
  293. def tags():
  294. """Build tags pages."""
  295. root_path = DAVID / "2021"
  296. source_path = root_path / "sources"
  297. # Parse all pages to collect tags.
  298. Page.all(source=source_path, only_published=True)
  299. for tag in all_tags:
  300. template = environment.get_template("tag_2021.html")
  301. content = template.render(
  302. page_list=pages_by_tags[tag],
  303. tag_name=tag,
  304. )
  305. target_path = DAVID / "2021" / slugify(tag)
  306. target_path.mkdir(parents=True, exist_ok=True)
  307. open(target_path / "index.html", "w").write(content)
  308. @cli
  309. def home():
  310. """Build the home page with last published items."""
  311. def group_by_month_year(item):
  312. return item.date.strftime("%B %Y").title()
  313. template = environment.get_template("profil.html")
  314. page_list = Page.all(source=DAVID / "2021" / "sources")
  315. tags = sorted((slugify(tag), tag, len(pages_by_tags[tag])) for tag in all_tags)
  316. content = template.render(
  317. page_list=groupby(page_list, key=group_by_month_year), tags=tags
  318. )
  319. open(DAVID / "index.html", "w").write(content)
  320. @cli
  321. def feed():
  322. """Generate a feed from last published items."""
  323. template = environment.get_template("feed.xml")
  324. page_list = Page.all(source=DAVID / "2021" / "sources", with_h2_anchors=False)
  325. content = template.render(
  326. page_list=page_list[:NB_ITEMS_IN_FEED],
  327. current_dt=TODAY.strftime(NORMALIZED_STRFTIME),
  328. BASE_URL=f"{DOMAIN}/david/",
  329. )
  330. open(DAVID / "log" / "index.xml", "w").write(content)
  331. @wrap
  332. def perf_wrapper():
  333. start = perf_counter()
  334. yield
  335. elapsed = perf_counter() - start
  336. print(f"Done in {elapsed:.5f} seconds.")
  337. if __name__ == "__main__":
  338. run()