#!/usr/bin/env python3
import fnmatch
import locale
import os
from dataclasses import dataclass
from datetime import datetime, timedelta
from html import escape
from pathlib import Path
from textwrap import dedent
from time import perf_counter
import mistune
from jinja2 import Environment as Env
from jinja2 import FileSystemLoader
from minicli import cli, run, wrap
from mistune.directives import DirectiveInclude
from PIL import Image
from slugify import slugify
from typography import typographie
# Useful for dates rendering within Jinja2.
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
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=7)
class MarkParser(mistune.InlineParser):
"""Parses `==foo==` as `foo`."""
MARK = (
r"(\={2})(?=[^\s*])("
r"(?:\\[\\*]|[^*])*"
r"(?:" + mistune.InlineParser.ESCAPE + r"|[^\s*]))\1"
)
RULE_NAMES = mistune.InlineParser.RULE_NAMES + ("mark",)
def parse_mark(self, m, state):
marker = m.group(1)
text = m.group(2)
return "mark", self.render(text, state)
class MarkRenderer(mistune.HTMLRenderer):
"""To use in conjunction with `MarkParser`."""
def mark(self, text):
return "" + text + ""
class FrenchTypographyRenderer(mistune.HTMLRenderer):
"""Apply French typographic rules to text."""
def text(self, text):
return typographie(super().text(text))
class TypographyAndBlockquoteLanguageRenderer(FrenchTypographyRenderer, MarkRenderer):
"""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(TypographyAndBlockquoteLanguageRenderer):
"""Renders images as