diff options
| author | Eddy Pedroni <epedroni@pm.me> | 2025-04-10 15:28:23 +0200 | 
|---|---|---|
| committer | Eddy Pedroni <epedroni@pm.me> | 2025-04-10 15:28:23 +0200 | 
| commit | 0d9452a064d33e826e876755b6f403617c36bc32 (patch) | |
| tree | d43de7c5f06f3cbe8dd49fe32d05fa629aa80c67 /godocs.py | |
| parent | 5198c7ee8d31959083937314200d577ff05c123a (diff) | |
Add audio and video preprocessing
Diffstat (limited to 'godocs.py')
| -rw-r--r-- | godocs.py | 44 | 
1 files changed, 25 insertions, 19 deletions
@@ -8,9 +8,12 @@ from string import capwords  import shutil  import tempfile  import re +from markdown2 import markdown -DRY = False  LOG_REGEX = re.compile("(20[0-9]{2})-([0-9]{2})-([0-9]{2})-([A-z\\-]+)\\.md") +VIDEO_REGEX = re.compile("^!v\((.+?)\)$", flags=re.M) +AUDIO_REGEX = re.compile("^!a\((.+?)\)$") +FILES_URL = "https://files.0xf7.com/api/public/dl/NR1j-os8"  @dataclass  class Page: @@ -38,10 +41,25 @@ class Page:          return sorted(normal_entries, key=lambda v: v.name) + sorted(log_entries, reverse=True, key=lambda v: v.name) +def renderEntries(page: Page) -> Iterator[str]: +    """ Preprocess and feed the page entries to the markdown engine """ +    for entry in page.entries: +        with open(entry, "r") as f: +            raw = f.read() + +        def videoTag(match: re.Match) -> str: +            return f"<video width=\"1280\" height=\"720\" controls><source src=\"{FILES_URL}/{match.group(1)}\">Your browser does not support the video element.</video>" + +        def audioTag(match: re.Match) -> str: +            return f"<audio controls><source src=\"{FILES_URL}/{match.group(1)}\">Your browser does not support the audio element.</audio>" + +        with_videos = re.sub(VIDEO_REGEX, videoTag, raw) +        #re.sub(AUDIO_REGEX, audioTag, raw) +        yield markdown(with_videos, extras=["fenced-code-blocks", "strike", "tables"]) +  def renderPageCallback(template_file: Path, stylesheet_url: str) -> Callable[[Page, str], None]:      """ Callback to process the provided page metadata and output the final page to the filesystem """      from jinja2 import Environment, FileSystemLoader, select_autoescape -    from markdown2 import markdown      env = Environment(              loader=FileSystemLoader(template_file.parent), @@ -49,21 +67,13 @@ def renderPageCallback(template_file: Path, stylesheet_url: str) -> Callable[[Pa      )      template = env.get_template(template_file.name) -    def entries(page: Page) -> Iterator[str]: -        for entry in page.entries: -            with open(entry, "r") as f: -                yield markdown(f.read(), extras=["fenced-code-blocks", "strike", "tables"]) -      def render(page: Page, site_title: str, navigation: list[dict[str, str]]) -> None:          print(f"Rendering {page.output}") -        args = {"site_title": site_title, "stylesheet_url": stylesheet_url, "navigation": navigation, "entries": entries(page)} +        args = {"site_title": site_title, "stylesheet_url": stylesheet_url, "navigation": navigation, "entries": renderEntries(page)}          content = template.render(args) -        if DRY: -            print(content) -        else: -            os.makedirs(page.output.parent, exist_ok=True) -            with open(page.output, "w") as f: -                f.write(content) +        os.makedirs(page.output.parent, exist_ok=True) +        with open(page.output, "w") as f: +            f.write(content)      return render @@ -121,11 +131,7 @@ def generateSites(config: dict[str, str]) -> None:  @click.command()  @click.argument("config_file", nargs=1, type=click.Path()) -@click.option("--dry/--no-dry", default=False, help="Instead of generating the sites, print every step to stdout") -def main(config_file, dry): -    global DRY -    DRY = dry - +def main(config_file):      with open(config_file, "rb") as f:          import tomllib          config = tomllib.load(f)  | 
