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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/env python3
  2. import fnmatch
  3. import locale
  4. import os
  5. from dataclasses import dataclass
  6. from datetime import date, datetime, timedelta
  7. from html import escape
  8. from pathlib import Path
  9. from time import perf_counter
  10. import mistune
  11. from jinja2 import Environment as Env
  12. from jinja2 import FileSystemLoader
  13. from minicli import cli, run, wrap
  14. from mistune.directives import DirectiveInclude
  15. from slugify import slugify
  16. # Useful for dates rendering within Jinja2.
  17. locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
  18. HERE = Path(".")
  19. DAVID = HERE / "david"
  20. DOMAIN = "https://larlet.fr"
  21. LOCAL_DOMAIN = "http://larlet.test:3579"
  22. # Hardcoding publication at 12 in Paris timezone.
  23. NORMALIZED_STRFTIME = "%Y-%m-%dT12:00:00+01:00"
  24. TODAY = datetime.today() + timedelta(hours=6)
  25. class CustomHTMLRenderer(mistune.HTMLRenderer):
  26. def heading(self, text, level):
  27. # Set an anchor to h2 headings.
  28. if level == 2:
  29. slug = slugify(text)
  30. return (
  31. f'<h2 id="{slug}">'
  32. f"{text} "
  33. f'<a href="#{slug}" title="Ancre vers cette partie" '
  34. f'aria-hidden="true">#</a>'
  35. f"</h2>"
  36. )
  37. else:
  38. return super().heading(text, level)
  39. markdown = mistune.create_markdown(
  40. renderer=CustomHTMLRenderer(escape=False), plugins=[DirectiveInclude()]
  41. )
  42. environment = Env(loader=FileSystemLoader(str(DAVID / "templates")))
  43. def neighborhood(iterable, first=None, last=None):
  44. """
  45. Yield the (previous, current, next) items given an iterable.
  46. You can specify a `first` and/or `last` item for bounds.
  47. """
  48. iterator = iter(iterable)
  49. previous = first
  50. current = next(iterator) # Throws StopIteration if empty.
  51. for next_ in iterator:
  52. yield (previous, current, next_)
  53. previous = current
  54. current = next_
  55. yield (previous, current, last)
  56. def each_markdown_from(source_dir, file_name="*.md"):
  57. """Walk across the `source_dir` and return the md file paths."""
  58. for filename in fnmatch.filter(os.listdir(source_dir), file_name):
  59. yield os.path.join(source_dir, filename)
  60. @dataclass
  61. class Page:
  62. title: str
  63. content: str
  64. file_path: str
  65. lang: str = "fr"
  66. def __post_init__(self):
  67. suffix = len(".md")
  68. prefix = len("YYYY/MM-DD") + suffix
  69. date_str = self.file_path[-prefix:-suffix].replace("-", "/")
  70. self.url = f"/david/{date_str}/"
  71. self.date = datetime.strptime(date_str, "%Y/%m/%d").date()
  72. self.full_url = f"{DOMAIN}{self.url}"
  73. self.normalized_date = self.date.strftime(NORMALIZED_STRFTIME)
  74. self.escaped_title = escape(self.title)
  75. self.escaped_content = escape(
  76. self.content.replace('href="/', f'href="{DOMAIN}/')
  77. .replace('src="/', f'src="{DOMAIN}/')
  78. .replace('href="#', f'href="{self.full_url}#')
  79. )
  80. # Extract first paragraph.
  81. self.extract = self.content.split("</p>", 1)[0] + "</p>"
  82. def __lt__(self, other: "Page"):
  83. if not isinstance(other, Page):
  84. return NotImplemented
  85. return self.date < other.date
  86. @staticmethod
  87. def all(source: Path, only_published=True):
  88. """Retrieve all pages sorted by desc."""
  89. page_list = []
  90. for file_path in each_markdown_from(source):
  91. result = markdown.read(file_path)
  92. title, content = result.split("</h1>", 1)
  93. h1_opening_size = len("<h1>")
  94. title = title[h1_opening_size:]
  95. page = Page(title, content, file_path)
  96. if only_published and page.is_draft:
  97. continue
  98. page_list.append(page)
  99. return sorted(page_list, reverse=True)
  100. @property
  101. def is_draft(self):
  102. return (
  103. datetime(year=self.date.year, month=self.date.month, day=self.date.day)
  104. > TODAY
  105. )
  106. @cli
  107. def orphans():
  108. """Print out fragments not linked to any page."""
  109. linked_fragments = []
  110. for file_path in each_markdown_from(DAVID / "2020"):
  111. for line in open(file_path).readlines():
  112. if line.startswith(".. include:: fragments/"):
  113. linked_fragments.append(line[len(".. include:: fragments/") : -1])
  114. all_fragments = []
  115. for file_path in each_markdown_from(DAVID / "2020" / "fragments"):
  116. all_fragments.append(file_path[len("david/2020/fragments/") :])
  117. for fragment_filename in set(all_fragments) - set(linked_fragments):
  118. # Prepending path for easy command+click from fish.
  119. print(f"Orphan: {DAVID / '2020' / 'fragments' / fragment_filename}")
  120. @cli
  121. def pages():
  122. """Build the agregations from fragments."""
  123. root_path = DAVID / "2020"
  124. for previous, page, next_ in neighborhood(
  125. reversed(Page.all(source=root_path, only_published=False)),
  126. first={
  127. "url": "/david/stream/",
  128. "title": "Streams 2009-2019",
  129. "is_draft": False,
  130. },
  131. ):
  132. template = environment.get_template("article_2020.html")
  133. content = template.render(page=page, prev=previous, next=next_,)
  134. target_path = Path(page.url[1:])
  135. target_path.mkdir(parents=True, exist_ok=True)
  136. open(target_path / "index.html", "w").write(content)
  137. if page.is_draft:
  138. print(f"Draft: {LOCAL_DOMAIN}{page.url} ({page.title})")
  139. template = environment.get_template("archives_2020.html")
  140. content = template.render(page_list=Page.all(source=root_path))
  141. open(root_path / "index.html", "w").write(content)
  142. @cli
  143. def home():
  144. """Build the home page with last published items."""
  145. template = environment.get_template("profil.html")
  146. content = template.render(page_list=Page.all(source=DAVID / "2020"),)
  147. open(DAVID / "index.html", "w").write(content)
  148. @cli
  149. def feed():
  150. """Generate a feed from last published items."""
  151. template = environment.get_template("feed.xml")
  152. content = template.render(
  153. page_list=Page.all(source=DAVID / "2020"),
  154. current_dt=TODAY.strftime(NORMALIZED_STRFTIME),
  155. BASE_URL=f"{DOMAIN}/david/",
  156. )
  157. open(DAVID / "log" / "index.xml", "w").write(content)
  158. @wrap
  159. def perf_wrapper():
  160. start = perf_counter()
  161. yield
  162. elapsed = perf_counter() - start
  163. print(f"Done in {elapsed:.5f} seconds.")
  164. if __name__ == "__main__":
  165. run()