From aa27baa06b3747f507003ea36b3392acc2a632c0 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Wed, 10 Jan 2024 05:17:19 -0500 Subject: [PATCH] feat(#14): include latest pending changes to mdit-py-plugins See PR: https://github.com/executablebooks/mdit-py-plugins/pull/94 --- .../_local/mdit_py_plugins/admon/index.py | 36 ++++++++++++++----- tests/fixtures.md | 24 +++++++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/mdformat_admon/_local/mdit_py_plugins/admon/index.py b/mdformat_admon/_local/mdit_py_plugins/admon/index.py index aa0e383..0309ed2 100644 --- a/mdformat_admon/_local/mdit_py_plugins/admon/index.py +++ b/mdformat_admon/_local/mdit_py_plugins/admon/index.py @@ -2,9 +2,12 @@ # https://github.com/executablebooks/mdit-py-plugins/blob/ba0c31e762d4961b6fde1b27541be92084af877b/mdit_py_plugins/admon/index.py # 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, Sequence, Tuple from markdown_it import MarkdownIt from markdown_it.rules_block import StateBlock @@ -16,20 +19,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[^"]+)\s+"(?P.*)"\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") + + +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: @@ -129,12 +146,13 @@ def admonition( # noqa: C901 # 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 diff --git a/tests/fixtures.md b/tests/fixtures.md index b93bcb3..8273e4d 100644 --- a/tests/fixtures.md +++ b/tests/fixtures.md @@ -50,6 +50,30 @@ Shows no title . +Removes extra quotes from the title +. +!!! danger "Don't try this at home" + ... + +. +!!! danger "Don't try this at home" + ... + +. + + +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"" + ... + +. +!!! a b c d inline-classes "Note: note about "foo"" + ... + +. + + Closes block after 2 empty lines . !!! note