Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👌 Expand support for Python-Markdown in the admon plugin #94

Merged
merged 10 commits into from
Jan 11, 2024
36 changes: 27 additions & 9 deletions mdit_py_plugins/admon/index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Process admonitions and pass to cb.

from __future__ import annotations

from typing import TYPE_CHECKING, Callable, Sequence
from contextlib import suppress
import re
from typing import TYPE_CHECKING, Callable, List, Optional, Sequence, Tuple

from markdown_it import MarkdownIt
from markdown_it.rules_block import StateBlock
Expand All @@ -14,20 +17,34 @@
from markdown_it.utils import EnvType, OptionsDict


def _get_tag(params: str) -> tuple[str, str]:
def _get_multiple_tags(params: str) -> Tuple[List[str], str]:
"""Check for multiple tags when the title is double quoted."""
re_tags = re.compile(r'^\s*(?P<tokens>[^"]+)\s+"(?P<title>.*)"\S*$')
match = re_tags.match(params)
if match:
tags = match["tokens"].strip().split(" ")
return [tag.lower() for tag in tags], match["title"]
raise ValueError("No match found for parameters")
KyleKing marked this conversation as resolved.
Show resolved Hide resolved


def _get_tag(_params: str) -> Tuple[List[str], str]:
"""Separate the tag name from the admonition title."""
if not params.strip():
return "", ""
params = _params.strip()
if not params:
return [""], ""

with suppress(ValueError):
return _get_multiple_tags(params)

tag, *_title = params.strip().split(" ")
tag, *_title = params.split(" ")
joined = " ".join(_title)

title = ""
if not joined:
title = tag.title()
elif joined != '""':
elif joined != '""': # Specifically check for no title
title = joined
return tag.lower(), title
return [tag.lower()], title


def _validate(params: str) -> bool:
Expand Down Expand Up @@ -125,12 +142,13 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
# this will prevent lazy continuations from ever going past our end marker
state.lineMax = next_line

tag, title = _get_tag(params)
tags, title = _get_tag(params)
tag = tags[0]

token = state.push("admonition_open", "div", 1)
token.markup = markup
token.block = True
token.attrs = {"class": " ".join(["admonition", tag, *_extra_classes(markup)])}
token.attrs = {"class": " ".join(["admonition", *tags, *_extra_classes(markup)])}
token.meta = {"tag": tag}
token.content = title
token.info = params
Expand Down
26 changes: 26 additions & 0 deletions tests/fixtures/admon.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ Shows no title
.


Removes extra quotes from the title
.
!!! danger "Don't try this at home"
...

.
<div class="admonition danger">
<p class="admonition-title">Don't try this at home</p>
<p>...</p>
</div>
.


Parse additional classes to support Python markdown (https://github.com/executablebooks/mdit-py-plugins/issues/93#issuecomment-1601822723)
.
!!! a b c d inline-classes "Note: note about "foo""
...

.
<div class="admonition a b c d inline-classes">
<p class="admonition-title">Note: note about &quot;foo&quot;</p>
<p>...</p>
</div>
KyleKing marked this conversation as resolved.
Show resolved Hide resolved
.


Closes block after 2 empty lines
.
!!! note
Expand Down