Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions dbuild/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ def _site_placeholders(text: str, env_file: bool = False) -> str:
MAX_COMBOS = 16


# GitHub's alert blockquote (> [!WARNING]) -> the site's admonition. The site
# renders the alert as a quote with "[!WARNING]" in it.
_GITHUB_ALERT_RE = re.compile(r"(?m)^> ?\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\][ \t]*\n((?:>.*(?:\n|$))*)")
_ADMONITION = {"NOTE": "note", "TIP": "tip", "IMPORTANT": 'info "Important"', "WARNING": "warning", "CAUTION": 'danger "Caution"'}


def _site_alerts(text: Any) -> Any:
"""Markdown with GitHub alerts as admonitions, for the site's render."""
if not isinstance(text, str):
return text

def admonition(m: re.Match) -> str:
body = [re.sub(r"^> ?", "", ln) for ln in m.group(2).splitlines()]
return f"!!! {_ADMONITION[m.group(1)]}\n\n" + "".join(f" {ln}\n" if ln.strip() else "\n" for ln in body)

return _GITHUB_ALERT_RE.sub(admonition, text)


def _combination_count(cfg: Config) -> int:
import math

Expand Down Expand Up @@ -679,6 +697,10 @@ def _enrich_metadata(cfg: Config, community_override: str | None = None) -> dict
"healthcheck": meta.healthcheck,
"notes": meta.notes,
"readme": meta.readme,
# The same for README.mkdocs.j2, which swaps them in.
"notes_site": _site_alerts(meta.notes),
"readme_site": dataclasses.replace(meta.readme, **{f.name: _site_alerts(getattr(meta.readme, f.name)) for f in dataclasses.fields(meta.readme)})
if dataclasses.is_dataclass(meta.readme) else meta.readme,
"community": community_raw,
"community_name": community_name,
"community_url": community_url,
Expand Down
2 changes: 2 additions & 0 deletions dbuild/templates/README.mkdocs.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% extends "README.j2" %}
{#- GitHub alerts in the author's text become admonitions here (_site_alerts). #}
{%- set readme, notes = readme_site, notes_site %}

{#- What an option runs, before any YAML: the images, then the host folders
the data lands in. Plain words; markdown, not a raw HTML block (a block
Expand Down
15 changes: 15 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,3 +1066,18 @@ def test_past_the_cap_only_the_defaults(self):
self.assertNotIn("Left out", readme.split("### Podman Compose", 1)[1].split("###", 1)[0],
"no heading per combination")
self.assertLess(len(readme), 50_000)


class TestSiteAlerts(unittest.TestCase):
def test_github_alert_becomes_an_admonition(self):
md = "intro\n\n> [!WARNING]\n> **Careful.** It breaks.\n>\n> Keep a backup.\n\nafter\n"
self.assertEqual(docs._site_alerts(md),
"intro\n\n!!! warning\n\n **Careful.** It breaks.\n\n Keep a backup.\n\nafter\n")

def test_a_plain_quote_is_left_alone(self):
md = "> Just a quote.\n"
self.assertEqual(docs._site_alerts(md), md)

def test_important_and_caution_keep_their_word(self):
self.assertTrue(docs._site_alerts("> [!IMPORTANT]\n> x\n").startswith('!!! info "Important"\n'))
self.assertTrue(docs._site_alerts("> [!CAUTION]\n> x\n").startswith('!!! danger "Caution"\n'))
Loading