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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/usr/bin/env python3
  2. import codecs
  3. import fnmatch
  4. import locale
  5. import os
  6. from collections import namedtuple
  7. from dataclasses import dataclass
  8. from datetime import date, datetime
  9. from html import escape
  10. from operator import attrgetter
  11. from pathlib import Path
  12. from time import perf_counter
  13. import markdown
  14. from jinja2 import Environment as Env
  15. from jinja2 import FileSystemLoader
  16. from minicli import cli, run, wrap
  17. # Useful for dates rendering within Jinja2.
  18. locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
  19. HERE = Path(".")
  20. DAVID = HERE / "david"
  21. DOMAIN = "https://larlet.fr"
  22. # Hardcoding publication at 12 in Paris timezone.
  23. NORMALIZED_STRFTIME = "%Y-%m-%dT12:00:00+01:00"
  24. environment = Env(loader=FileSystemLoader(str(DAVID / "templates")))
  25. def neighborhood(iterable, first=None, last=None):
  26. """
  27. Yield the (previous, current, next) items given an iterable.
  28. You can specify a `first` and/or `last` item for bounds.
  29. """
  30. iterator = iter(iterable)
  31. previous = first
  32. current = next(iterator) # Throws StopIteration if empty.
  33. for next_ in iterator:
  34. yield (previous, current, next_)
  35. previous = current
  36. current = next_
  37. yield (previous, current, last)
  38. def parse_markdown(file_path):
  39. """Extract title, (HTML) content and metadata from a markdown file."""
  40. parser = markdown.Markdown(extensions=["meta"])
  41. with codecs.open(file_path, "r") as source:
  42. source = source.read()
  43. # Avoid replacing quotes from code #PoorManParsing.
  44. if ":::" not in source and "`" not in source:
  45. source = source.replace("'", "’")
  46. content = parser.convert(source)
  47. metadata = parser.Meta if hasattr(parser, "Meta") else None
  48. title = metadata["title"][0] if metadata is not None else ""
  49. return title, content, metadata
  50. def each_markdown_from(source_dir, file_name="index.md"):
  51. """Walk across the `source_dir` and return the md file paths."""
  52. for root, dirnames, filenames in os.walk(source_dir):
  53. for filename in fnmatch.filter(filenames, file_name):
  54. yield os.path.join(root, filename)
  55. @dataclass
  56. class Item:
  57. title: str
  58. content: str
  59. file_path: str
  60. def __post_init__(self):
  61. self.full_url = f"{DOMAIN}{self.url}"
  62. self.normalized_date = self.date.strftime(NORMALIZED_STRFTIME)
  63. self.escaped_title = escape(self.title)
  64. self.escaped_content = escape(
  65. self.content.replace('href="/', f'href="{DOMAIN}/').replace(
  66. 'src="/', f'src="{DOMAIN}/'
  67. )
  68. )
  69. @property
  70. def is_draft(self):
  71. return self.date > date.today()
  72. @dataclass
  73. class Note(Item):
  74. lang: str = "fr"
  75. def __post_init__(self):
  76. suffix = len("/index.md")
  77. prefix = len("YYYY/MM/DD") + suffix
  78. date_str = self.file_path[-prefix:-suffix]
  79. self.url = f"/david/stream/{date_str}/"
  80. self.date = datetime.strptime(date_str, "%Y/%m/%d").date()
  81. super().__post_init__()
  82. self.extract = self.content.split("</p>", 1)[0] + "</p>"
  83. @staticmethod
  84. def all(source, only_published=True):
  85. """Retrieve all (published) notes sorted by date desc."""
  86. note_list = []
  87. for file_path in each_markdown_from(source):
  88. title, content, _ = parse_markdown(file_path)
  89. note = Note(title, content, file_path)
  90. if only_published and note.is_draft:
  91. continue
  92. note_list.append(note)
  93. return sorted(note_list, key=attrgetter("date"), reverse=True)
  94. @dataclass
  95. class Post(Item):
  96. date: str
  97. slug: str
  98. chapo: str
  99. lang: str
  100. def __post_init__(self):
  101. self.url = f"/david/blog/{self.date.year}/{self.slug}/"
  102. super().__post_init__()
  103. self.url_image = f"/static/david/blog/{self.date.year}/{self.slug}.jpg"
  104. self.url_image_thumbnail = (
  105. f"/static/david/blog/{self.date.year}/thumbnails/{self.slug}.jpg"
  106. )
  107. self.full_img_url = f"{DOMAIN}{self.url_image}"
  108. self.full_img_url_thumbnail = f"{DOMAIN}{self.url_image_thumbnail}"
  109. self.escaped_content = self.escaped_content + escape(
  110. f'<img src="{self.full_img_url_thumbnail}" width="500px" height="500px" />'
  111. )
  112. self.escaped_chapo = escape(self.chapo)
  113. @staticmethod
  114. def all(source, only_published=True):
  115. """Retrieve all (published) posts sorted by date desc."""
  116. post_list = []
  117. for file_path in each_markdown_from(source):
  118. title, content, metadata = parse_markdown(file_path)
  119. date = datetime.strptime(metadata["date"][0], "%Y-%m-%d").date()
  120. slug = metadata["slug"][0]
  121. chapo = metadata["chapo"][0]
  122. lang = metadata.get("lang", ["fr"])[0]
  123. post = Post(title, content, file_path, date, slug, chapo, lang)
  124. if only_published and post.is_draft:
  125. continue
  126. post_list.append(post)
  127. return sorted(post_list, key=attrgetter("date"), reverse=True)
  128. @cli
  129. def note(when=None):
  130. """Create a new note and open it in iA Writer.
  131. :when: Optional date in ISO format (YYYY-MM-DD)
  132. """
  133. when = datetime.strptime(when, "%Y-%m-%d") if when else date.today()
  134. note_path = DAVID / "stream" / str(when.year) / str(when.month) / str(when.day)
  135. os.makedirs(note_path)
  136. filename = note_path / "index.md"
  137. open(filename, "w+").write("title: ")
  138. os.popen(f'open -a "iA Writer" "{filename}"')
  139. @cli
  140. def stream():
  141. """Generate articles and archives for the stream."""
  142. template_article = environment.get_template("stream_2019_article.html")
  143. template_archives = environment.get_template("stream_2019_archives.html")
  144. # Default when you reach the last item.
  145. FakeNote = namedtuple("FakeNote", ["url", "title"])
  146. notes_2018 = FakeNote(url="/david/stream/2018/", title="Anciennes notes (2018)")
  147. note_base = DAVID / "stream" / "2019"
  148. unpublished = Note.all(source=note_base, only_published=False)
  149. published = [note for note in unpublished if not note.is_draft]
  150. for previous, note, next_ in neighborhood(unpublished, last=notes_2018):
  151. if note.is_draft:
  152. print(f"Soon: http://larlet.test:8001{note.url} ({note.title})")
  153. # Detect if there is code for syntax highlighting + monospaced font.
  154. has_code = "<code>" in note.content
  155. # Do not link to unpublished notes.
  156. previous = previous and not previous.is_draft and previous or None
  157. page_article = template_article.render(
  158. note=note,
  159. next=previous,
  160. prev=next_,
  161. has_code=has_code,
  162. note_list=published,
  163. )
  164. open(
  165. note_base / f"{note.date.month:02}" / f"{note.date.day:02}" / "index.html",
  166. "w",
  167. ).write(page_article)
  168. page_archive = template_archives.render(note_list=published)
  169. open(note_base / "index.html", "w").write(page_archive)
  170. print(f"Done: http://larlet.test:8001/{note_base}/")
  171. @cli
  172. def blog():
  173. """Generate articles and archives for the blog."""
  174. template_article = environment.get_template("blog_article.html")
  175. template_archives = environment.get_template("blog_archives.html")
  176. # Default when you reach the last item.
  177. FakePost = namedtuple("FakePost", ["url", "title"])
  178. posts_2012 = FakePost(
  179. url="/david/thoughts/", title="Pensées précédentes (en anglais)"
  180. )
  181. post_base = DAVID / "blog"
  182. unpublished = Post.all(source=post_base, only_published=False)
  183. published = [post for post in unpublished if not post.is_draft]
  184. published_en = [post for post in published if post.lang == "en"]
  185. note_list = Note.all(source=DAVID / "stream" / "2019")
  186. for previous, post, next_ in neighborhood(unpublished, last=posts_2012):
  187. if post.date.year < 2018:
  188. continue # Speed up + do not overwrite old comments.
  189. if post.is_draft:
  190. print(f"Soon: http://larlet.test:8001{post.url} ({post.title})")
  191. # Detect if there is code for syntax highlighting + monospaced font.
  192. has_code = "<code>" in post.content
  193. # Do not link to unpublished posts.
  194. previous = previous and not previous.is_draft and previous or None
  195. page_article = template_article.render(
  196. post=post,
  197. next=previous,
  198. prev=next_,
  199. has_code=has_code,
  200. post_list=published,
  201. published_posts_en=published_en,
  202. note_list=note_list,
  203. )
  204. open(post_base / str(post.date.year) / post.slug / "index.html", "w",).write(
  205. page_article
  206. )
  207. page_archive = template_archives.render(posts=published, note_list=note_list)
  208. open(post_base / "index.html", "w").write(page_archive)
  209. print(f"Done: http://larlet.test:8001/{post_base}/")
  210. @cli
  211. def home():
  212. """Build the home page with last published items."""
  213. template = environment.get_template("profil.html")
  214. content = template.render(note_list=Note.all(source=DAVID / "stream" / "2019"),)
  215. open(DAVID / "index.html", "w").write(content)
  216. @cli
  217. def feed():
  218. """Generate a feed from last published items."""
  219. template = environment.get_template("feed.xml")
  220. content = template.render(
  221. note_list=Note.all(source=DAVID / "stream" / "2019")[:15],
  222. post_list=Post.all(source=DAVID / "blog")[:5],
  223. current_dt=datetime.now().strftime(NORMALIZED_STRFTIME),
  224. BASE_URL=f"{DOMAIN}/david/",
  225. )
  226. open(DAVID / "log" / "index.xml", "w").write(content)
  227. @wrap
  228. def perf_wrapper():
  229. start = perf_counter()
  230. yield
  231. elapsed = perf_counter() - start
  232. print(f"Done in {elapsed:.5f} seconds.")
  233. if __name__ == "__main__":
  234. run()