feat: Markdown emphasis inside columns blocks

Inside a columns fenced block (verbatim, so Pandoc doesn't process
Markdown), convert *italic* / **bold** / ***bold-italic*** to wikitext
emphasis. Asterisk style, single line; precedence bold-italic > bold >
italic. Raw inline HTML still works via the existing HTML-unescape path.

Docs + example updated to use ** ** rather than <b>.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-14 21:13:31 +09:00
parent 00527e46b1
commit 2072db4980
2 changed files with 23 additions and 7 deletions
+20 -4
View File
@@ -15,6 +15,20 @@ AUTO_BANNER_PREFIX = "{{Auto-generated"
_COLUMNS_BLOCK_RE = re.compile(r'<pre class="columns">(.*?)</pre>', re.DOTALL)
_COLUMN_SEP_RE = re.compile(r"^\s*===\s*$", re.MULTILINE)
# Minimal Markdown emphasis → wikitext, for use inside a verbatim columns block
# (Pandoc doesn't process Markdown there). Asterisk style only, single line.
# Order matters: bold-italic (***) before bold (**) before italic (*).
_BOLD_ITALIC_RE = re.compile(r"\*\*\*(.+?)\*\*\*")
_BOLD_RE = re.compile(r"\*\*(.+?)\*\*")
_ITALIC_RE = re.compile(r"\*(.+?)\*")
def _md_emphasis_to_wikitext(text: str) -> str:
text = _BOLD_ITALIC_RE.sub(r"'''''\1'''''", text)
text = _BOLD_RE.sub(r"'''\1'''", text)
text = _ITALIC_RE.sub(r"''\1''", text)
return text
def strip_first_h1(text: str) -> str:
"""Remove the first '# Heading' line and any immediately following blank line."""
@@ -35,13 +49,15 @@ def expand_columns(wikitext: str) -> str:
through verbatim as ``<pre class="columns">…</pre>`` (line breaks and blank
lines preserved, inline markup entity-escaped). Columns within the block are
separated by a line containing only ``===``. Each column is wrapped in
<poem> so its line breaks survive MediaWiki parsing, and content is
HTML-unescaped so inline markup written in the fence (e.g. <b>…</b>) renders
rather than appearing as literal text.
<poem> so its line breaks survive MediaWiki parsing. Content is
HTML-unescaped (so inline markup such as <b>…</b> renders rather than
appearing as literal text) and Markdown emphasis (``*italic*``, ``**bold**``,
``***bold-italic***``) is converted to wikitext, since Pandoc does not
process Markdown inside the verbatim block.
"""
def render(match: re.Match) -> str:
body = html.unescape(match.group(1))
body = _md_emphasis_to_wikitext(html.unescape(match.group(1)))
columns = _COLUMN_SEP_RE.split(body)
poems = "".join("<poem>\n" + col.strip("\n") + "\n</poem>" for col in columns)
return '<div style="display:flex; gap:3em; align-items:flex-start">' + poems + "</div>"