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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. def __post_init__(self):
  75. suffix = len("/index.md")
  76. prefix = len("YYYY/MM/DD") + suffix
  77. date_str = self.file_path[-prefix:-suffix]
  78. self.url = f"/david/stream/{date_str}/"
  79. self.date = datetime.strptime(date_str, "%Y/%m/%d").date()
  80. super().__post_init__()
  81. self.extract = self.content.split("</p>", 1)[0] + "</p>"
  82. @staticmethod
  83. def all(source, only_published=True):
  84. """Retrieve all (published) notes sorted by date desc."""
  85. note_list = []
  86. for file_path in each_markdown_from(source):
  87. title, content, _ = parse_markdown(file_path)
  88. note = Note(title, content, file_path)
  89. if only_published and note.is_draft:
  90. continue
  91. note_list.append(note)
  92. return sorted(note_list, key=attrgetter("date"), reverse=True)
  93. @dataclass
  94. class Post(Item):
  95. date: str
  96. slug: str
  97. chapo: str
  98. lang: str
  99. def __post_init__(self):
  100. self.url = f"/david/blog/{self.date.year}/{self.slug}/"
  101. super().__post_init__()
  102. self.url_image = f"/static/david/blog/{self.date.year}/{self.slug}.jpg"
  103. self.url_image_thumbnail = (
  104. f"/static/david/blog/{self.date.year}/thumbnails/{self.slug}.jpg"
  105. )
  106. self.full_img_url = f"{DOMAIN}{self.url_image}"
  107. self.full_img_url_thumbnail = f"{DOMAIN}{self.url_image_thumbnail}"
  108. self.escaped_content = self.escaped_content + escape(
  109. f'<img src="{self.full_img_url_thumbnail}" width="500px" height="500px" />'
  110. )
  111. self.escaped_chapo = escape(self.chapo)
  112. @staticmethod
  113. def all(source, only_published=True):
  114. """Retrieve all (published) posts sorted by date desc."""
  115. post_list = []
  116. for file_path in each_markdown_from(source):
  117. title, content, metadata = parse_markdown(file_path)
  118. date = datetime.strptime(metadata["date"][0], "%Y-%m-%d").date()
  119. slug = metadata["slug"][0]
  120. chapo = metadata["chapo"][0]
  121. lang = metadata.get("lang", ["fr"])[0]
  122. post = Post(title, content, file_path, date, slug, chapo, lang)
  123. if only_published and post.is_draft:
  124. continue
  125. post_list.append(post)
  126. return sorted(post_list, key=attrgetter("date"), reverse=True)
  127. @cli
  128. def note(when=None):
  129. """Create a new note and open it in iA Writer.
  130. :when: Optional date in ISO format (YYYY-MM-DD)
  131. """
  132. when = datetime.strptime(when, "%Y-%m-%d") if when else date.today()
  133. note_path = DAVID / "stream" / str(when.year) / str(when.month) / str(when.day)
  134. os.makedirs(note_path)
  135. filename = note_path / "index.md"
  136. open(filename, "w+").write("title: ")
  137. os.popen(f'open -a "iA Writer" "{filename}"')
  138. @cli
  139. def stream():
  140. """Generate articles and archives for the stream."""
  141. template_article = environment.get_template("stream_2019_article.html")
  142. template_archives = environment.get_template("stream_2019_archives.html")
  143. # Default when you reach the last item.
  144. FakeNote = namedtuple("FakeNote", ["url", "title"])
  145. notes_2018 = FakeNote(url="/david/stream/2018/", title="Anciennes notes (2018)")
  146. note_base = DAVID / "stream" / "2019"
  147. unpublished = Note.all(source=note_base, only_published=False)
  148. published = [note for note in unpublished if not note.is_draft]
  149. for previous, note, next_ in neighborhood(unpublished, last=notes_2018):
  150. if note.is_draft:
  151. print(f"Soon: http://larlet.test:8001{note.url} ({note.title})")
  152. # Detect if there is code for syntax highlighting + monospaced font.
  153. has_code = "<code>" in note.content
  154. # Do not link to unpublished notes.
  155. previous = previous and not previous.is_draft and previous or None
  156. page_article = template_article.render(
  157. note=note,
  158. next=previous,
  159. prev=next_,
  160. has_code=has_code,
  161. note_list=published,
  162. )
  163. open(
  164. note_base / f"{note.date.month:02}" / f"{note.date.day:02}" / "index.html",
  165. "w",
  166. ).write(page_article)
  167. page_archive = template_archives.render(note_list=published)
  168. open(note_base / "index.html", "w").write(page_archive)
  169. print(f"Done: http://larlet.test:8001/{note_base}/")
  170. @cli
  171. def blog():
  172. """Generate articles and archives for the blog."""
  173. template_article = environment.get_template("blog_article.html")
  174. template_archives = environment.get_template("blog_archives.html")
  175. # Default when you reach the last item.
  176. FakePost = namedtuple("FakePost", ["url", "title"])
  177. posts_2012 = FakePost(
  178. url="/david/thoughts/", title="Pensées précédentes (en anglais)"
  179. )
  180. post_base = DAVID / "blog"
  181. unpublished = Post.all(source=post_base, only_published=False)
  182. published = [post for post in unpublished if not post.is_draft]
  183. published_en = [post for post in published if post.lang == "en"]
  184. note_list = Note.all(source=DAVID / "stream" / "2019")
  185. for previous, post, next_ in neighborhood(unpublished, last=posts_2012):
  186. if post.is_draft:
  187. print(f"Soon: http://larlet.test:8001{post.url} ({post.title})")
  188. # Detect if there is code for syntax highlighting + monospaced font.
  189. has_code = "<code>" in post.content
  190. # Do not link to unpublished posts.
  191. previous = previous and not previous.is_draft and previous or None
  192. page_article = template_article.render(
  193. post=post,
  194. next=previous,
  195. prev=next_,
  196. has_code=has_code,
  197. post_list=published,
  198. published_posts_en=published_en,
  199. note_list=note_list,
  200. )
  201. open(post_base / str(post.date.year) / post.slug / "index.html", "w",).write(
  202. page_article
  203. )
  204. page_archive = template_archives.render(posts=published, note_list=note_list)
  205. open(post_base / "index.html", "w").write(page_archive)
  206. print(f"Done: http://larlet.test:8001/{post_base}/")
  207. @cli
  208. def home():
  209. """Build the home page with last published items."""
  210. template = environment.get_template("profil.html")
  211. content = template.render(note_list=Note.all(source=DAVID / "stream" / "2019"),)
  212. open(DAVID / "index.html", "w").write(content)
  213. @cli
  214. def feed():
  215. """Generate a feed from last published items."""
  216. template = environment.get_template("feed.xml")
  217. content = template.render(
  218. note_list=Note.all(source=DAVID / "stream" / "2019")[:15],
  219. post_list=Post.all(source=DAVID / "blog")[:5],
  220. current_dt=datetime.now().strftime(NORMALIZED_STRFTIME),
  221. BASE_URL=f"{DOMAIN}/david/",
  222. )
  223. open(DAVID / "log" / "index.xml", "w").write(content)
  224. @wrap
  225. def perf_wrapper():
  226. start = perf_counter()
  227. yield
  228. elapsed = perf_counter() - start
  229. print(f"Done in {elapsed:.5f} seconds.")
  230. if __name__ == "__main__":
  231. run()