Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

site.py 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. #!/usr/bin/env python3
  2. import json
  3. import locale
  4. from collections import defaultdict
  5. from dataclasses import dataclass
  6. from datetime import datetime, timedelta
  7. from html import escape
  8. from itertools import chain, groupby
  9. from operator import attrgetter
  10. from pathlib import Path
  11. from string import Template
  12. from textwrap import dedent
  13. from time import perf_counter
  14. import feedparser
  15. import mistune
  16. from jinja2 import Environment as Env
  17. from jinja2 import FileSystemLoader
  18. from jinja2.filters import do_striptags
  19. from minicli import cli, run, wrap
  20. from mistune.plugins.formatting import mark, strikethrough
  21. from mistune.util import safe_entity
  22. from PIL import Image
  23. from slugify import slugify
  24. from typography import typographie
  25. from widont import widont
  26. # Useful for dates rendering within Jinja2.
  27. locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
  28. VERSION = "2023.09.10"
  29. HERE = Path(".")
  30. DAVID = HERE / "david"
  31. STATIC = HERE / ".." / "larlet-fr-static"
  32. DOMAIN = "https://larlet.fr"
  33. LOCAL_DOMAIN = "http://larlet.test:3579"
  34. # Hardcoding publication at 12 in Paris timezone.
  35. NORMALIZED_STRFTIME = "%Y-%m-%dT12:00:00+01:00"
  36. TODAY = datetime.today() + timedelta(hours=6)
  37. PUBLICATION_BUFFER = TODAY - timedelta(days=0)
  38. NB_ITEMS_IN_FEED = 30
  39. SOURCES_PATH = DAVID / "2023" / "_sources"
  40. all_tags = set()
  41. pages_by_tags = defaultdict(list)
  42. pages_by_url = {}
  43. class TagsRenderer(mistune.HTMLRenderer):
  44. """Make the asumption each line starting with a `#` is a tag."""
  45. def paragraph(self, text):
  46. if text.startswith("#"):
  47. tags = " ".join(
  48. f'<a href="/david/2023/{slugify(tag.strip())}/">#{tag.strip()}</a>'
  49. for tag in text.split("#")
  50. if tag.strip()
  51. )
  52. return f"<nav><p>{tags}</p></nav>\n"
  53. return super().paragraph(text)
  54. class FrenchTypographyRenderer(mistune.HTMLRenderer):
  55. """Apply French typographic rules to text."""
  56. def text(self, text):
  57. return typographie(super().text(text), html=True)
  58. def block_html(self, html):
  59. return typographie(super().block_html(html), html=True)
  60. class InternalLinkTitleRenderer(mistune.HTMLRenderer):
  61. """Automatically generate the title for internal links."""
  62. def link(self, text, url, title=None):
  63. s = '<a href="' + self.safe_url(url) + '"'
  64. if not title and url.startswith("/david/2023/"):
  65. # It will not work for internal urls referencing the future.
  66. page = pages_by_url.get(url)
  67. if page:
  68. title = page.title
  69. if title:
  70. s += ' title="' + safe_entity(title) + '"'
  71. return s + ">" + text + "</a>"
  72. class CustomAndBlockquoteLanguageRenderer(
  73. FrenchTypographyRenderer, InternalLinkTitleRenderer, TagsRenderer
  74. ):
  75. """Sets the English language attribute for blockquotes with `[en]` prefix."""
  76. def _get_language(self, text):
  77. if text.startswith("<p>[en] "):
  78. return "en", text.replace("<p>[en] ", "<p>")
  79. else:
  80. return None, text
  81. def block_quote(self, text):
  82. language, text = self._get_language(text)
  83. if language:
  84. return f'\n<blockquote lang="{language}">\n{text}</blockquote>\n'
  85. else:
  86. return f"\n<blockquote>\n{text}</blockquote>\n"
  87. class ImgsWithSizesRenderer(CustomAndBlockquoteLanguageRenderer):
  88. """Renders images as <figure>s and add sizes."""
  89. def paragraph(self, text):
  90. # In case of a figure, we do not want the (non-standard) paragraph.
  91. if text.strip().startswith("<figure>"):
  92. return text
  93. return super().paragraph(text)
  94. def _generate_size(self, src, width, height):
  95. src_size = src.replace(".jpg", f"_{width}x{height}.jpg")
  96. full_path = STATIC / Path(src[1:])
  97. full_path_size = STATIC / Path(src_size[1:])
  98. if full_path_size.exists() or "/2023/" not in src:
  99. return src_size
  100. image = Image.open(full_path)
  101. image.thumbnail((width, height), resample=Image.LANCZOS)
  102. image.save(full_path_size, icc_profile=image.info.get("icc_profile"))
  103. return src_size
  104. def _generate_webp(self, src):
  105. src_webp = src.replace(".jpg", ".webp")
  106. full_path = STATIC / Path(src[1:])
  107. full_path_webp = STATIC / Path(src_webp[1:])
  108. if full_path_webp.exists() or "/2023/" not in src:
  109. return src_webp
  110. image = Image.open(full_path)
  111. image.save(
  112. full_path_webp, format="webp", icc_profile=image.info.get("icc_profile")
  113. )
  114. # command = [
  115. # "cwebp",
  116. # "-q",
  117. # "80",
  118. # full_path,
  119. # "-o",
  120. # full_path_webp,
  121. # "-metadata",
  122. # "icc",
  123. # ]
  124. # subprocess.check_output(command, stderr=subprocess.STDOUT)
  125. return src_webp
  126. def image(self, alt, url, title=None):
  127. SIZES = [(660, 440), (990, 660), (1320, 880)]
  128. full_path = STATIC / Path(url[1:])
  129. image = Image.open(full_path)
  130. width, height = image.size
  131. jpg_srcs = [(url, width, height)]
  132. # src_webp = self._generate_webp(src)
  133. # webp_srcs = [(src_webp, width, height)]
  134. for size_width, size_height in SIZES:
  135. src_size = self._generate_size(url, size_width, size_height)
  136. jpg_srcs.append((src_size, size_width, size_height))
  137. # src_size_webp = self._generate_webp(src_size)
  138. # webp_srcs.append((src_size_webp, size_width, size_height))
  139. jpg_srcsets = ", ".join(
  140. f"{jpg_src} {jpg_width}w" for jpg_src, jpg_width, jpg_height in jpg_srcs
  141. )
  142. # webp_srcsets = ", ".join(
  143. # f"{webp_src} {webp_width}w"
  144. # for webp_src, webp_width, webp_height in webp_srcs
  145. # )
  146. return dedent(
  147. f"""\
  148. <figure>
  149. <a href="{url}"
  150. title="Cliquer pour une version haute résolution">
  151. <img
  152. src="{url}"
  153. width="{width}" height="{height}"
  154. srcset="{jpg_srcsets}"
  155. sizes="min(100vw, calc(100vh * {width} / {height}))"
  156. loading="lazy"
  157. decoding="async"
  158. alt="{alt}">
  159. </a>
  160. <figcaption>{title}</figcaption>
  161. </figure>
  162. """
  163. )
  164. class H2AnchorsRenderer(ImgsWithSizesRenderer):
  165. """Custom renderer for H2 titles with anchors."""
  166. def heading(self, text, level):
  167. if level == 2:
  168. slug = slugify(text)
  169. return (
  170. f'<h2 id="{slug}">'
  171. f"{text} "
  172. f'<a href="#{slug}" title="Ancre vers cette partie">#</a>'
  173. f"</h2>"
  174. )
  175. else:
  176. return super().heading(text, level)
  177. # We want a custom renderer to create a hash/link for each H2 headings.
  178. markdown_with_h2_anchors = mistune.Markdown(
  179. renderer=H2AnchorsRenderer(escape=False),
  180. plugins=[mark, strikethrough],
  181. )
  182. # The second markdown is pertinent to generate articles for the feed,
  183. # we do not need anchors in that case.
  184. markdown_with_img_sizes = mistune.Markdown(
  185. renderer=ImgsWithSizesRenderer(escape=False),
  186. plugins=[mark, strikethrough],
  187. )
  188. # This is the jinja2 configuration to locate templates.
  189. environment = Env(loader=FileSystemLoader(str(DAVID / "templates")))
  190. def neighborhood(iterable, first=None, last=None):
  191. """
  192. Yield the (previous, current, next) items given an iterable.
  193. You can specify a `first` and/or `last` item for bounds.
  194. """
  195. iterator = iter(iterable)
  196. previous = first
  197. current = next(iterator) # Throws StopIteration if empty.
  198. for next_ in iterator:
  199. yield (previous, current, next_)
  200. previous = current
  201. current = next_
  202. yield (previous, current, last)
  203. def each_file_from(source_dir, pattern="*", exclude=None):
  204. """Walk across the `source_dir` and return the `pattern` file paths."""
  205. for path in _each_path_from(source_dir, pattern=pattern, exclude=exclude):
  206. if path.is_file():
  207. yield path
  208. def each_folder_from(source_dir, exclude=None):
  209. """Walk across the `source_dir` and return the folder paths."""
  210. for path in _each_path_from(source_dir, exclude=exclude):
  211. if path.is_dir():
  212. yield path
  213. def _each_path_from(source_dir, pattern="*", exclude=None):
  214. for path in sorted(Path(source_dir).glob(pattern)):
  215. if exclude is not None and path.name in exclude:
  216. continue
  217. yield path
  218. @dataclass
  219. class Page:
  220. title: str
  221. content: str
  222. tags: list
  223. file_path: str
  224. lang: str = "fr"
  225. def __post_init__(self):
  226. try:
  227. date_str, _ = self.file_path.split(" - ", 1)
  228. except ValueError:
  229. # Fallback for 2020 contents (search index)
  230. suffix = len(".md")
  231. prefix = len("YYYY/MM-DD") + suffix
  232. date_str = "2020-" + self.file_path[-prefix:-suffix]
  233. self.url = f"/david/{date_str.replace('-', '/')}/"
  234. self.date = datetime.strptime(date_str, "%Y-%m-%d").date()
  235. self.full_url = f"{DOMAIN}{self.url}"
  236. self.normalized_date = self.date.strftime(NORMALIZED_STRFTIME)
  237. self.escaped_title = escape(self.title)
  238. tag_template = Template(
  239. f'<a href="{DOMAIN}/david/2023/$tag_slug/">#$tag_name</a>'
  240. )
  241. tag_links = " ".join(
  242. tag_template.substitute(tag_slug=slugify(tag), tag_name=tag)
  243. for tag in self.tags
  244. )
  245. self.escaped_content = escape(
  246. self.content.replace('href="/', f'href="{DOMAIN}/')
  247. .replace('src="/', f'src="{DOMAIN}/')
  248. .replace('href="#', f'href="{self.full_url}#')
  249. + f"<nav><p>{tag_links}</p></nav>"
  250. + '<hr/><p><a href="mailto:david@larlet.fr">Réagir ?</a></p>'
  251. )
  252. # Extract first paragraph.
  253. self.extract = self.content.split("</p>", 1)[0] + "</p>"
  254. # Create the index for the search.
  255. self.search_data = {
  256. "title": self.title,
  257. "url": self.url,
  258. "date": date_str,
  259. "content": do_striptags(self.content)
  260. .replace("\u00a0(cache)", " ")
  261. .replace("'", " "),
  262. }
  263. def __eq__(self, other):
  264. return self.url == other.url
  265. def __lt__(self, other: "Page"):
  266. if not isinstance(other, Page):
  267. return NotImplemented
  268. return self.date < other.date
  269. @staticmethod
  270. def all(source: Path, only_published=True, with_h2_anchors=True):
  271. """Retrieve all pages sorted by desc."""
  272. page_list = []
  273. md = markdown_with_h2_anchors if with_h2_anchors else markdown_with_img_sizes
  274. for file_path in sorted(each_file_from(source, pattern="*.md")):
  275. result, state = md.read(file_path)
  276. result = widont(result, html=True)
  277. # Extract (and remove) the title from the generated page.
  278. title, content = result.split("</h1>", 1)
  279. h1_opening_size = len("<h1>")
  280. title = title[h1_opening_size:]
  281. tags = {}
  282. if "<nav><p>" in content:
  283. # Extract the tags from the generated page.
  284. content, tags_links = content.split("<nav><p>", 1)
  285. nav_closing_size = len("</p></nav>\n")
  286. tags_links = tags_links[:-nav_closing_size]
  287. try:
  288. tags = sorted(
  289. {
  290. tag.strip().split("#", 1)[1]
  291. for tag in tags_links.split("</a>")
  292. if tag.strip()
  293. },
  294. key=lambda tag: slugify(tag),
  295. )
  296. except IndexError:
  297. # It happens for old contents, parsed for the search index.
  298. pass
  299. page = Page(title, content, tags, file_path.name)
  300. pages_by_url[page.url] = page
  301. if not page.is_draft:
  302. all_tags.update(tags)
  303. for tag in tags:
  304. if page not in pages_by_tags[tag]:
  305. pages_by_tags[tag].append(page)
  306. if only_published and page.is_draft:
  307. continue
  308. page_list.append(page)
  309. return sorted(page_list, reverse=True)
  310. @property
  311. def is_draft(self):
  312. return (
  313. datetime(year=self.date.year, month=self.date.month, day=self.date.day)
  314. > PUBLICATION_BUFFER
  315. )
  316. @cli
  317. def pages():
  318. """Build article pages."""
  319. root_path = DAVID / "2023"
  320. for previous, page, next_ in neighborhood(
  321. reversed(Page.all(source=SOURCES_PATH, only_published=False)),
  322. first={
  323. "url": "/david/2022/",
  324. "title": "Publications 2022",
  325. "is_draft": False,
  326. },
  327. ):
  328. template = environment.get_template("article_2020.html")
  329. content = template.render(page=page, prev=previous, next=next_, slugify=slugify)
  330. target_path = Path(page.url[1:])
  331. target_path.mkdir(parents=True, exist_ok=True)
  332. (target_path / "index.html").write_text(content)
  333. if page.is_draft:
  334. print(f"Draft: {LOCAL_DOMAIN}{page.url} ({page.title})")
  335. def group_by_month_year(item):
  336. return item.date.strftime("%B %Y").title()
  337. template = environment.get_template("archives_2020.html")
  338. page_list = reversed(Page.all(source=SOURCES_PATH))
  339. tags = sorted((slugify(tag), tag, len(pages_by_tags[tag])) for tag in all_tags)
  340. content = template.render(
  341. page_list=groupby(page_list, key=group_by_month_year), tags=tags
  342. )
  343. (root_path / "index.html").write_text(content)
  344. @cli
  345. def tags():
  346. """Build tags pages."""
  347. # Parse all pages to collect tags.
  348. Page.all(source=SOURCES_PATH, only_published=True)
  349. for tag in all_tags:
  350. template = environment.get_template("tag_2021.html")
  351. content = template.render(
  352. page_list=sorted(pages_by_tags[tag], reverse=True),
  353. tag_name=tag,
  354. )
  355. target_path = DAVID / "2023" / slugify(tag)
  356. target_path.mkdir(parents=True, exist_ok=True)
  357. (target_path / "index.html").write_text(content)
  358. @cli
  359. def home():
  360. """Build the home page with last published items."""
  361. template = environment.get_template("profil.html")
  362. page_list = Page.all(source=SOURCES_PATH, only_published=True)
  363. tags = sorted((slugify(tag), tag, len(pages_by_tags[tag])) for tag in all_tags)
  364. content = template.render(page_list=page_list, tags=tags)
  365. (DAVID / "index.html").write_text(content)
  366. @dataclass
  367. class Website:
  368. name: str
  369. url: str
  370. feed: str
  371. def __post_init__(self):
  372. content = feedparser.parse(self.feed)
  373. entries = content.get("entries")
  374. try:
  375. entries = sorted(entries, key=attrgetter("updated_parsed"), reverse=True)[
  376. :2
  377. ]
  378. except AttributeError:
  379. print("No `updated_parsed` in", entries)
  380. for entry in entries:
  381. entry.website = self
  382. self.entries = entries
  383. @cli
  384. def blogroll():
  385. """Build the blogroll with last published items from people."""
  386. template = environment.get_template("blogroll.html")
  387. website_list = [
  388. # Invalid feed, date not standard.
  389. # Website(
  390. # name="Alex Sirac",
  391. # url="https://alexsirac.com/",
  392. # feed="https://alexsirac.com/feed",
  393. # ),
  394. Website(
  395. name="Maïtané Lenoir",
  396. url="https://www.maiwann.net/",
  397. feed="https://www.maiwann.net/feed.xml",
  398. ),
  399. Website(
  400. name="Fanny Cheung",
  401. url="https://ynote.hk/",
  402. feed="https://ynote.hk/feed.xml",
  403. ),
  404. Website(
  405. name="La Lune Mauve",
  406. url="https://lalunemauve.fr/",
  407. feed="https://lalunemauve.fr/feed/",
  408. ),
  409. Website(
  410. name="Eliness",
  411. url="https://www.hypothermia.fr/",
  412. feed="https://www.hypothermia.fr/rss",
  413. ),
  414. Website(
  415. name="Luce Carević",
  416. url="https://luce.carevic.eu/fr",
  417. feed="https://luce.carevic.eu/fr/flux",
  418. ),
  419. Website(
  420. name="Emma",
  421. url="https://emmaclit.com/",
  422. feed="https://emmaclit.com/feed/",
  423. ),
  424. Website(
  425. name="Karl Dubost",
  426. url="https://www.la-grange.net/",
  427. feed="https://www.la-grange.net/feed.atom",
  428. ),
  429. Website(
  430. name="Thomas Parisot",
  431. url="https://thom4.net/",
  432. feed="https://thom4.net/feed/",
  433. ),
  434. Website(
  435. name="Arthur Perret",
  436. url="https://www.arthurperret.fr/",
  437. feed="https://www.arthurperret.fr/feed.xml",
  438. ),
  439. Website(
  440. name="Antoine Fauchié",
  441. url="https://www.quaternum.net/",
  442. feed="https://www.quaternum.net/atom.xml",
  443. ),
  444. Website(
  445. name="Éric D.",
  446. url="https://n.survol.fr/",
  447. feed="https://n.survol.fr/feed",
  448. ),
  449. Website(
  450. name="Winnie Lim (en)",
  451. url="https://winnielim.org/",
  452. feed="https://winnielim.org/feed/",
  453. ),
  454. ]
  455. entry_list = sorted(
  456. chain(*[website.entries for website in website_list]),
  457. key=attrgetter("updated_parsed"),
  458. reverse=True,
  459. )
  460. content = template.render(website_list=website_list, entry_list=entry_list)
  461. (DAVID / "blogroll" / "index.html").write_text(content)
  462. @cli
  463. def toot():
  464. """Pre-write the Mastodon message."""
  465. page_list = Page.all(source=SOURCES_PATH, only_published=True)
  466. last_published = page_list[0]
  467. print(f"✍️ QUOTE? — {last_published.title}, {last_published.full_url}")
  468. print()
  469. print("#blog #larletfr #rss")
  470. print(" ".join([f"#{tag}" for tag in last_published.tags]))
  471. @cli
  472. def search():
  473. """Build the static search page with custom index."""
  474. template = environment.get_template("recherche.html")
  475. page_list_2023 = Page.all(
  476. source=SOURCES_PATH, only_published=True, with_h2_anchors=False
  477. )
  478. page_list_2022 = Page.all(
  479. source=DAVID / "2022" / "_sources", only_published=True, with_h2_anchors=False
  480. )
  481. page_list_2021 = Page.all(
  482. source=DAVID / "2021" / "sources", only_published=True, with_h2_anchors=False
  483. )
  484. page_list_2020 = Page.all(
  485. source=DAVID / "2020", only_published=True, with_h2_anchors=False
  486. )
  487. blog_page_list_2019 = BlogPage.all(source=DAVID / "blog" / "2019")
  488. blog_page_list_2018 = BlogPage.all(source=DAVID / "blog" / "2018")
  489. blog_page_list_2017 = BlogPage.all(source=DAVID / "blog" / "2017")
  490. stream_page_list_2019 = StreamPage.all(source=DAVID / "stream" / "2019")
  491. stream_page_list_2018 = StreamPage.all(source=DAVID / "stream" / "2018")
  492. page_list = (
  493. page_list_2023
  494. + page_list_2022
  495. + page_list_2021
  496. + page_list_2020
  497. + blog_page_list_2019
  498. + blog_page_list_2018
  499. + blog_page_list_2017
  500. + stream_page_list_2019
  501. + stream_page_list_2018
  502. )
  503. search_index = json.dumps([page.search_data for page in page_list], indent=2)
  504. content = template.render(search_index=search_index)
  505. (DAVID / "recherche" / "index.html").write_text(content)
  506. @cli
  507. def feed():
  508. """Generate a feed from last published items."""
  509. template = environment.get_template("feed.xml")
  510. page_list = Page.all(source=SOURCES_PATH, with_h2_anchors=False)
  511. content = template.render(
  512. page_list=page_list[:NB_ITEMS_IN_FEED],
  513. current_dt=TODAY.strftime(NORMALIZED_STRFTIME),
  514. BASE_URL=f"{DOMAIN}/david/",
  515. )
  516. (DAVID / "log" / "index.xml").write_text(content)
  517. @wrap
  518. def perf_wrapper():
  519. start = perf_counter()
  520. yield
  521. elapsed = perf_counter() - start
  522. print(f"Done in {elapsed:.5f} seconds.")
  523. # Below are legacy blog contents, still useful for search indexation.
  524. @dataclass
  525. class BlogPage:
  526. title: str
  527. content: str
  528. file_path: str
  529. date_str: str
  530. def __post_init__(self):
  531. self.date = datetime.strptime(self.date_str, "%Y-%m-%d").date()
  532. self.url = f"/{self.file_path}/"
  533. # Create the index for the search.
  534. self.search_data = {
  535. "title": self.title,
  536. "url": self.url,
  537. "date": self.date_str,
  538. "content": do_striptags(self.content)
  539. .replace("\u00a0(cache)", " ")
  540. .replace("'", " ")
  541. .replace("<", "&lt;")
  542. .replace(">", "&gt;"),
  543. }
  544. def __eq__(self, other):
  545. return self.url == other.url
  546. def __lt__(self, other: "BlogPage"):
  547. if not isinstance(other, self.__class__):
  548. return NotImplemented
  549. return self.date < other.date
  550. @staticmethod
  551. def all(source: Path):
  552. """Retrieve all pages sorted by desc."""
  553. page_list = []
  554. for folder in each_folder_from(source):
  555. for path in each_file_from(folder, pattern="*.md"):
  556. metadata, content = path.read_text().split("\n\n", 1)
  557. if "lang:" in metadata:
  558. title, slug, date_, chapo, lang = metadata.split("\n")
  559. else:
  560. title, slug, date_, chapo = metadata.split("\n")
  561. title = title[len("title: ") :].strip()
  562. date_str = date_[len("date: ") :].strip()
  563. content = markdown_with_img_sizes(content)
  564. page = BlogPage(title, content, path.parent, date_str)
  565. page_list.append(page)
  566. return sorted(page_list, reverse=True)
  567. @dataclass
  568. class StreamPage:
  569. title: str
  570. content: str
  571. file_path: str
  572. date_str: str
  573. def __post_init__(self):
  574. self.date = datetime.strptime(self.date_str, "%Y/%m/%d").date()
  575. self.url = f"/{self.file_path}/"
  576. # Create the index for the search.
  577. self.search_data = {
  578. "title": self.title,
  579. "url": self.url,
  580. "date": self.date.isoformat(),
  581. "content": do_striptags(self.content)
  582. .replace("\u00a0(cache)", " ")
  583. .replace("'", " ")
  584. .replace("<", "&lt;")
  585. .replace(">", "&gt;"),
  586. }
  587. def __eq__(self, other):
  588. return self.url == other.url
  589. def __lt__(self, other: "StreamPage"):
  590. if not isinstance(other, self.__class__):
  591. return NotImplemented
  592. return self.date < other.date
  593. @staticmethod
  594. def all(source: Path):
  595. """Retrieve all pages sorted by desc."""
  596. page_list = []
  597. for folder in each_folder_from(source):
  598. for subfolder in each_folder_from(folder):
  599. for path in each_file_from(subfolder, pattern="*.md"):
  600. metadata, content = path.read_text().split("\n\n", 1)
  601. if "lang:" in metadata:
  602. title, lang = metadata.split("\n")
  603. else:
  604. title = metadata.strip()
  605. title = title[len("title: ") :].strip()
  606. date_str = str(path.parent)[-len("YYYY/MM/DD") :]
  607. content = markdown_with_img_sizes(content)
  608. page = StreamPage(title, content, path.parent, date_str)
  609. page_list.append(page)
  610. return sorted(page_list, reverse=True)
  611. if __name__ == "__main__":
  612. run()