bazzite/docs/hooks/md_image_sizing.py
Zeglius b7ded48ce1
fix(docs): Use a working match substitution in md_image_sizing hook (#1584)
* fix(docs): Use a working match substitution in md_image_sizing hook

This fixes odd page segment duplicating inside themselves

* ci(docs): Avoid triggering deploy_docs on PR
2024-08-31 15:47:54 +00:00

42 lines
1.1 KiB
Python

import re
import string
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.pages import Page
from mkdocs.structure.files import Files
from mkdocs.plugins import event_priority
IMG_SIZED_RE = (
r"\!\[.*?\s*(?P<width>\d+)x(?P<height>\d+)(?:,\s*(?P<multi>\d+)%)?\]\(.*\)"
)
"""Used to get width, height from `text|WIDTHxHEIGHT`"""
def _add_attr_handler(match: re.Match[str]) -> str:
width = match.group("width")
height = match.group("height")
if multi := match.group("multi"):
multi = int(multi) / 100
width = str(int(width) * multi)
height = str(int(height) * multi)
res = match.group(0) + string.Template(
'{ style="width:${width}px; height:${height}px;" }'
).substitute(width=width, height=height)
return res
is_enabled: bool
def on_config(config: MkDocsConfig):
global is_enabled
is_enabled = "attr_list" in config.markdown_extensions
@event_priority(70)
def on_page_markdown(
markdown: str, page: Page, config: MkDocsConfig, files: Files, **kargs
):
if is_enabled:
return re.sub(IMG_SIZED_RE, _add_attr_handler, markdown)