Skip to content

Commit

Permalink
Fix compatiblity of admonition with Python-Markdown executablebooks#93
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Corona committed Jun 21, 2023
1 parent ba0c31e commit c6ccace
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions mdit_py_plugins/admon/index.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Process admonitions and pass to cb.
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Callable, Sequence

from markdown_it import MarkdownIt
Expand All @@ -14,20 +15,20 @@
from markdown_it.utils import EnvType, OptionsDict


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

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

title = ""
if not joined:
title = tag.title()
elif joined != '""':
title = joined
return tag.lower(), title
return [""], ""

match = re.match('^\s*([^"]+)\s+"(.*)"\S*$', params)
if match:
tokens = match.group(1)
tags = tokens.strip().split(" ")
title = match.group(2)
return [tag.lower() for tag in tags], title
else:
tags = params.strip().split(" ")
return [tag.lower() for tag in tags], tags[0].title()


def _validate(params: str) -> bool:
Expand Down Expand Up @@ -125,12 +126,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

0 comments on commit c6ccace

Please sign in to comment.