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 10KB

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