Files
bliven_sandSpencer Bliven f9c471ec6a
Build and deploy documentation / build-docs (push) Successful in 38s
Build and deploy documentation / deploy-docs (push) Successful in 38s
Add videos (#26)
This adds two videos:
1. Introduction to the PSI Data Catalog (/index.html)
2. Introduction to OpenEM (openem/user/introduction/)

Assisted by: Claude Opus 5.5

---------

Co-authored-by: Spencer Bliven <spencer.bliven@gmail.com>
Reviewed-on: #26
2026-10-13 14:24:24 +02:00

38 lines
1.2 KiB
Python

"""Render Markdown images that point at video files as HTML5 <video> elements.
Usage: ![Description of the video](../assets/videos/example.mp4)
Using image syntax (rather than a raw <video> tag) means MkDocs validates that
the file exists and rewrites the path relative to the page, like any other
image.
"""
import re
VIDEO_TYPES = {".mp4": "video/mp4", ".webm": "video/webm", ".ogv": "video/ogg"}
IMG_RE = re.compile(r"<img\b[^>]*>")
ATTR_RE = re.compile(r'(?<![\w-])(src|alt)="([^"]*)"')
def _replace(match: re.Match) -> str:
attrs = dict(ATTR_RE.findall(match.group(0)))
src = attrs.get("src", "")
path = src.split("#")[0].split("?")[0].lower()
ext = next((e for e in VIDEO_TYPES if path.endswith(e)), None)
if ext is None:
return match.group(0)
alt = attrs.get("alt")
label = f' aria-label="{alt}"' if alt else ""
link_text = f"Download: {alt}" if alt else "Download the video"
return (
f'<video controls preload="metadata" style="width: 100%"{label}>'
f'<source src="{src}" type="{VIDEO_TYPES[ext]}">'
f'<a href="{src}">{link_text}</a>'
"</video>"
)
def on_page_content(content: str, **kwargs) -> str:
return IMG_RE.sub(_replace, content)