#!/usr/bin/env python3
import json
import locale
import pickle
import time
from collections import defaultdict
from dataclasses import dataclass
from datetime import datetime, timedelta
from html import escape
from itertools import chain, groupby
from operator import attrgetter
from pathlib import Path
from string import Template
from textwrap import dedent
from time import perf_counter
import feedparser
import mistune
from jinja2 import Environment as Env
from jinja2 import FileSystemLoader
from jinja2.filters import do_striptags
from minicli import cli, run, wrap
from mistune.plugins.formatting import mark, strikethrough
from mistune.util import safe_entity
from PIL import Image
from slugify import slugify
from typography import typographie
from widont import widont
# Useful for dates rendering within Jinja2.
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
VERSION = "2023.09.10"
HERE = Path(".")
DAVID = HERE / "david"
STATIC = HERE / ".." / "larlet-fr-static"
DOMAIN = "https://larlet.fr"
LOCAL_DOMAIN = "http://larlet.test:3579"
# Hardcoding publication at 12 in Paris timezone.
NORMALIZED_STRFTIME = "%Y-%m-%dT12:00:00+01:00"
TODAY = datetime.today() + timedelta(hours=6)
PUBLICATION_BUFFER = TODAY - timedelta(days=0)
NB_ITEMS_IN_FEED = 30
SOURCES_PATH = DAVID / "2023" / "_sources"
all_tags = set()
pages_by_tags = defaultdict(list)
pages_by_url = {}
class TagsRenderer(mistune.HTMLRenderer):
"""Make the asumption each line starting with a `#` is a tag."""
def paragraph(self, text):
if text.startswith("#"):
tags = " ".join(
f'#{tag.strip()}'
for tag in text.split("#")
if tag.strip()
)
return f"\n"
return super().paragraph(text)
class FrenchTypographyRenderer(mistune.HTMLRenderer):
"""Apply French typographic rules to text."""
def text(self, text):
return typographie(super().text(text), html=True)
def block_html(self, html):
return typographie(super().block_html(html), html=True)
class InternalLinkTitleRenderer(mistune.HTMLRenderer):
"""Automatically generate the title for internal links."""
def link(self, text, url, title=None):
s = '" + text + ""
class CustomAndBlockquoteLanguageRenderer(
FrenchTypographyRenderer, InternalLinkTitleRenderer, TagsRenderer
):
"""Sets the English language attribute for blockquotes with `[en]` prefix."""
def _get_language(self, text):
if text.startswith("
[en] "):
return "en", text.replace("
[en] ", "
")
else:
return None, text
def block_quote(self, text):
language, text = self._get_language(text)
if language:
return f'\n
\n{text}
\n'
else:
return f"\n
\n{text}
\n"
class ImgsWithSizesRenderer(CustomAndBlockquoteLanguageRenderer):
"""Renders images as