From 8fd9dab3670b66b68be1dfd1f6a7a60e6cb708be Mon Sep 17 00:00:00 2001 From: Kyle King Date: Wed, 21 Jun 2023 19:03:09 -0400 Subject: [PATCH 1/9] fix(#93): strip outer quotes from admonition titles --- mdit_py_plugins/admon/index.py | 4 ++-- tests/fixtures/admon.md | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mdit_py_plugins/admon/index.py b/mdit_py_plugins/admon/index.py index a46e8cf..4710ff5 100644 --- a/mdit_py_plugins/admon/index.py +++ b/mdit_py_plugins/admon/index.py @@ -25,9 +25,9 @@ def _get_tag(params: str) -> tuple[str, str]: 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.strip('"') def _validate(params: str) -> bool: diff --git a/tests/fixtures/admon.md b/tests/fixtures/admon.md index 083d0b9..5b6e20f 100644 --- a/tests/fixtures/admon.md +++ b/tests/fixtures/admon.md @@ -53,6 +53,19 @@ Shows no title . +Removes extra quotes from the title +. +!!! danger "Don't try this at home" + ... + +. +
+

Don't try this at home

+

...

+
+. + + Closes block after 2 empty lines . !!! note From b742c15b928f721275353bbfa16b2d1b59341232 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Wed, 21 Jun 2023 19:14:33 -0400 Subject: [PATCH 2/9] feat(#93): add HTML admonition roles --- mdit_py_plugins/admon/index.py | 7 +- tests/fixtures/admon.md | 90 +++++++++++------------ tests/test_admon/test_plugin_parse_0_.yml | 4 + tests/test_admon/test_plugin_parse_1_.yml | 4 + tests/test_admon/test_plugin_parse_2_.yml | 4 + 5 files changed, 62 insertions(+), 47 deletions(-) diff --git a/mdit_py_plugins/admon/index.py b/mdit_py_plugins/admon/index.py index 4710ff5..c908337 100644 --- a/mdit_py_plugins/admon/index.py +++ b/mdit_py_plugins/admon/index.py @@ -130,7 +130,10 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) -> 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", tag, *_extra_classes(markup)]), + "role": tag, + } token.meta = {"tag": tag} token.content = title token.info = params @@ -140,7 +143,7 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) -> title_markup = f"{markup} {tag}" token = state.push("admonition_title_open", "p", 1) token.markup = title_markup - token.attrs = {"class": "admonition-title"} + token.attrs = {"class": "admonition-title", "role": "heading"} token.map = [startLine, startLine + 1] token = state.push("inline", "", 0) diff --git a/tests/fixtures/admon.md b/tests/fixtures/admon.md index 5b6e20f..c80d524 100644 --- a/tests/fixtures/admon.md +++ b/tests/fixtures/admon.md @@ -4,8 +4,8 @@ Simple admonition !!! note *content* . -
-

Note

+
+

Note

content

. @@ -19,8 +19,8 @@ Could contain block elements too ----------- . -
-

Note

+
+

Note

heading


@@ -34,8 +34,8 @@ Shows custom title Some text . -
-

Custom title

+
+

Custom title

Some text

. @@ -47,7 +47,7 @@ Shows no title Some text . -
+

Some text

. @@ -59,8 +59,8 @@ Removes extra quotes from the title ... . -
-

Don't try this at home

+
+

Don't try this at home

...

. @@ -74,8 +74,8 @@ Closes block after 2 empty lines A code block . -
-

Note

+
+

Note

Some text

A code block
@@ -91,10 +91,10 @@ Nested blocks
             
             code block
 .
-
-

Note

-
-

Note

+
+

Note

+
+

Note

Some text

code block
 
@@ -109,11 +109,11 @@ Consecutive admonitions !!! warning . -
-

Note

+
+

Note

-
-

Warning

+
+

Warning

. @@ -123,8 +123,8 @@ Marker may be indented up to 3 chars !!! note content . -
-

Note

+
+

Note

content

. @@ -148,8 +148,8 @@ Some more indent checks code block . -
-

Note

+
+

Note

not a code block

code block
@@ -163,8 +163,8 @@ Type could be adjacent to marker
    xxx
 
 .
-
-

Note

+
+

Note

xxx

. @@ -176,8 +176,8 @@ Type could be adjacent to marker and content may be shifted up to 3 chars xxx . -
-

Note

+
+

Note

xxx

. @@ -188,8 +188,8 @@ Or several spaces apart !!! note xxx . -
-

Note

+
+

Note

xxx

. @@ -200,8 +200,8 @@ Admonitions self-close at the end of the document !!! note xxx . -
-

Note

+
+

Note

xxx

. @@ -218,8 +218,8 @@ They could be nested in lists .
  • -
    -

    Note

    +
    +

    Note

    • a
    • b
    • @@ -227,8 +227,8 @@ They could be nested in lists
  • -
    -

    Warning

    +
    +

    Warning

    • c
    • d
    • @@ -248,8 +248,8 @@ Or in blockquotes > .
      -
      -

      Note

      +
      +

      Note

      xxx

      yyy @@ -265,8 +265,8 @@ Renders unknown admonition type !!! unknown title content . -

      -

      title

      +
      +

      title

      content

      . @@ -288,8 +288,8 @@ MKdocs Closed Collapsible Sections ??? note content . -
      -

      Note

      +
      +

      Note

      content

      . @@ -300,8 +300,8 @@ MKdocs Open Collapsible Sections ???+ note content . -
      -

      Note

      +
      +

      Note

      content

      . @@ -321,8 +321,8 @@ Indented by 4 spaces, DISABLE-CODEBLOCKS ??? note content . -
      -

      Note

      +
      +

      Note

      content

      . diff --git a/tests/test_admon/test_plugin_parse_0_.yml b/tests/test_admon/test_plugin_parse_0_.yml index 3272842..dd4f177 100644 --- a/tests/test_admon/test_plugin_parse_0_.yml +++ b/tests/test_admon/test_plugin_parse_0_.yml @@ -1,6 +1,8 @@ - attrs: - - class - admonition note + - - role + - note block: true children: null content: Note @@ -19,6 +21,8 @@ - attrs: - - class - admonition-title + - - role + - heading block: true children: null content: '' diff --git a/tests/test_admon/test_plugin_parse_1_.yml b/tests/test_admon/test_plugin_parse_1_.yml index 3a8c206..6474190 100644 --- a/tests/test_admon/test_plugin_parse_1_.yml +++ b/tests/test_admon/test_plugin_parse_1_.yml @@ -1,6 +1,8 @@ - attrs: - - class - admonition note is-collapsible collapsible-closed + - - role + - note block: true children: null content: Note @@ -19,6 +21,8 @@ - attrs: - - class - admonition-title + - - role + - heading block: true children: null content: '' diff --git a/tests/test_admon/test_plugin_parse_2_.yml b/tests/test_admon/test_plugin_parse_2_.yml index bfada8e..9b53cf8 100644 --- a/tests/test_admon/test_plugin_parse_2_.yml +++ b/tests/test_admon/test_plugin_parse_2_.yml @@ -1,6 +1,8 @@ - attrs: - - class - admonition note is-collapsible collapsible-open + - - role + - note block: true children: null content: Note @@ -19,6 +21,8 @@ - attrs: - - class - admonition-title + - - role + - heading block: true children: null content: '' From 5549321a1f5fe259c3a1774cd2662aae13655c3f Mon Sep 17 00:00:00 2001 From: Kyle King Date: Wed, 21 Jun 2023 19:23:30 -0400 Subject: [PATCH 3/9] fix: always use `role=note` --- mdit_py_plugins/admon/index.py | 2 +- tests/fixtures/admon.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mdit_py_plugins/admon/index.py b/mdit_py_plugins/admon/index.py index c908337..9b8c4ed 100644 --- a/mdit_py_plugins/admon/index.py +++ b/mdit_py_plugins/admon/index.py @@ -132,7 +132,7 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) -> token.block = True token.attrs = { "class": " ".join(["admonition", tag, *_extra_classes(markup)]), - "role": tag, + "role": "note", } token.meta = {"tag": tag} token.content = title diff --git a/tests/fixtures/admon.md b/tests/fixtures/admon.md index c80d524..56def27 100644 --- a/tests/fixtures/admon.md +++ b/tests/fixtures/admon.md @@ -59,7 +59,7 @@ Removes extra quotes from the title ... . -
      +

      Don't try this at home

      ...

      From eeeb532afff72c1e66dff7bcdf8e5755b31a5f5b Mon Sep 17 00:00:00 2001 From: Kyle King Date: Wed, 21 Jun 2023 20:02:30 -0400 Subject: [PATCH 4/9] Revert "fix: always use `role=note`" This reverts commit 5549321a1f5fe259c3a1774cd2662aae13655c3f. --- mdit_py_plugins/admon/index.py | 2 +- tests/fixtures/admon.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mdit_py_plugins/admon/index.py b/mdit_py_plugins/admon/index.py index 9b8c4ed..c908337 100644 --- a/mdit_py_plugins/admon/index.py +++ b/mdit_py_plugins/admon/index.py @@ -132,7 +132,7 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) -> token.block = True token.attrs = { "class": " ".join(["admonition", tag, *_extra_classes(markup)]), - "role": "note", + "role": tag, } token.meta = {"tag": tag} token.content = title diff --git a/tests/fixtures/admon.md b/tests/fixtures/admon.md index 56def27..c80d524 100644 --- a/tests/fixtures/admon.md +++ b/tests/fixtures/admon.md @@ -59,7 +59,7 @@ Removes extra quotes from the title ... . -
      +

      Don't try this at home

      ...

      From ab9bb050dfa48ba2223dbd83513b1385f1e8e4c3 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Wed, 21 Jun 2023 20:02:31 -0400 Subject: [PATCH 5/9] Revert "feat(#93): add HTML admonition roles" This reverts commit b742c15b928f721275353bbfa16b2d1b59341232. --- mdit_py_plugins/admon/index.py | 7 +- tests/fixtures/admon.md | 90 +++++++++++------------ tests/test_admon/test_plugin_parse_0_.yml | 4 - tests/test_admon/test_plugin_parse_1_.yml | 4 - tests/test_admon/test_plugin_parse_2_.yml | 4 - 5 files changed, 47 insertions(+), 62 deletions(-) diff --git a/mdit_py_plugins/admon/index.py b/mdit_py_plugins/admon/index.py index c908337..4710ff5 100644 --- a/mdit_py_plugins/admon/index.py +++ b/mdit_py_plugins/admon/index.py @@ -130,10 +130,7 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) -> token = state.push("admonition_open", "div", 1) token.markup = markup token.block = True - token.attrs = { - "class": " ".join(["admonition", tag, *_extra_classes(markup)]), - "role": tag, - } + token.attrs = {"class": " ".join(["admonition", tag, *_extra_classes(markup)])} token.meta = {"tag": tag} token.content = title token.info = params @@ -143,7 +140,7 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) -> title_markup = f"{markup} {tag}" token = state.push("admonition_title_open", "p", 1) token.markup = title_markup - token.attrs = {"class": "admonition-title", "role": "heading"} + token.attrs = {"class": "admonition-title"} token.map = [startLine, startLine + 1] token = state.push("inline", "", 0) diff --git a/tests/fixtures/admon.md b/tests/fixtures/admon.md index c80d524..5b6e20f 100644 --- a/tests/fixtures/admon.md +++ b/tests/fixtures/admon.md @@ -4,8 +4,8 @@ Simple admonition !!! note *content* . -
      -

      Note

      +
      +

      Note

      content

      . @@ -19,8 +19,8 @@ Could contain block elements too ----------- . -
      -

      Note

      +
      +

      Note

      heading


      @@ -34,8 +34,8 @@ Shows custom title Some text . -
      -

      Custom title

      +
      +

      Custom title

      Some text

      . @@ -47,7 +47,7 @@ Shows no title Some text . -
      +

      Some text

      . @@ -59,8 +59,8 @@ Removes extra quotes from the title ... . -
      -

      Don't try this at home

      +
      +

      Don't try this at home

      ...

      . @@ -74,8 +74,8 @@ Closes block after 2 empty lines A code block . -
      -

      Note

      +
      +

      Note

      Some text

      A code block
      @@ -91,10 +91,10 @@ Nested blocks
                   
                   code block
       .
      -
      -

      Note

      -
      -

      Note

      +
      +

      Note

      +
      +

      Note

      Some text

      code block
       
      @@ -109,11 +109,11 @@ Consecutive admonitions !!! warning . -
      -

      Note

      +
      +

      Note

      -
      -

      Warning

      +
      +

      Warning

      . @@ -123,8 +123,8 @@ Marker may be indented up to 3 chars !!! note content . -
      -

      Note

      +
      +

      Note

      content

      . @@ -148,8 +148,8 @@ Some more indent checks code block . -
      -

      Note

      +
      +

      Note

      not a code block

      code block
      @@ -163,8 +163,8 @@ Type could be adjacent to marker
          xxx
       
       .
      -
      -

      Note

      +
      +

      Note

      xxx

      . @@ -176,8 +176,8 @@ Type could be adjacent to marker and content may be shifted up to 3 chars xxx . -
      -

      Note

      +
      +

      Note

      xxx

      . @@ -188,8 +188,8 @@ Or several spaces apart !!! note xxx . -
      -

      Note

      +
      +

      Note

      xxx

      . @@ -200,8 +200,8 @@ Admonitions self-close at the end of the document !!! note xxx . -
      -

      Note

      +
      +

      Note

      xxx

      . @@ -218,8 +218,8 @@ They could be nested in lists .
      • -
        -

        Note

        +
        +

        Note

        • a
        • b
        • @@ -227,8 +227,8 @@ They could be nested in lists
      • -
        -

        Warning

        +
        +

        Warning

        • c
        • d
        • @@ -248,8 +248,8 @@ Or in blockquotes > .
          -
          -

          Note

          +
          +

          Note

          xxx

          yyy @@ -265,8 +265,8 @@ Renders unknown admonition type !!! unknown title content . -

          -

          title

          +
          +

          title

          content

          . @@ -288,8 +288,8 @@ MKdocs Closed Collapsible Sections ??? note content . -
          -

          Note

          +
          +

          Note

          content

          . @@ -300,8 +300,8 @@ MKdocs Open Collapsible Sections ???+ note content . -
          -

          Note

          +
          +

          Note

          content

          . @@ -321,8 +321,8 @@ Indented by 4 spaces, DISABLE-CODEBLOCKS ??? note content . -
          -

          Note

          +
          +

          Note

          content

          . diff --git a/tests/test_admon/test_plugin_parse_0_.yml b/tests/test_admon/test_plugin_parse_0_.yml index dd4f177..3272842 100644 --- a/tests/test_admon/test_plugin_parse_0_.yml +++ b/tests/test_admon/test_plugin_parse_0_.yml @@ -1,8 +1,6 @@ - attrs: - - class - admonition note - - - role - - note block: true children: null content: Note @@ -21,8 +19,6 @@ - attrs: - - class - admonition-title - - - role - - heading block: true children: null content: '' diff --git a/tests/test_admon/test_plugin_parse_1_.yml b/tests/test_admon/test_plugin_parse_1_.yml index 6474190..3a8c206 100644 --- a/tests/test_admon/test_plugin_parse_1_.yml +++ b/tests/test_admon/test_plugin_parse_1_.yml @@ -1,8 +1,6 @@ - attrs: - - class - admonition note is-collapsible collapsible-closed - - - role - - note block: true children: null content: Note @@ -21,8 +19,6 @@ - attrs: - - class - admonition-title - - - role - - heading block: true children: null content: '' diff --git a/tests/test_admon/test_plugin_parse_2_.yml b/tests/test_admon/test_plugin_parse_2_.yml index 9b53cf8..bfada8e 100644 --- a/tests/test_admon/test_plugin_parse_2_.yml +++ b/tests/test_admon/test_plugin_parse_2_.yml @@ -1,8 +1,6 @@ - attrs: - - class - admonition note is-collapsible collapsible-open - - - role - - note block: true children: null content: Note @@ -21,8 +19,6 @@ - attrs: - - class - admonition-title - - - role - - heading block: true children: null content: '' From 0b95ef9f0f4850467a6f6daecf1005fa45b57495 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Wed, 21 Jun 2023 20:56:36 -0400 Subject: [PATCH 6/9] feat: handle a list of tags Based on: https://github.com/executablebooks/mdit-py-plugins/pull/95/files --- mdit_py_plugins/admon/index.py | 35 +++++++++++++++++++++++++--------- tests/fixtures/admon.md | 13 +++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/mdit_py_plugins/admon/index.py b/mdit_py_plugins/admon/index.py index 4710ff5..b75dcbf 100644 --- a/mdit_py_plugins/admon/index.py +++ b/mdit_py_plugins/admon/index.py @@ -1,8 +1,10 @@ # Process admonitions and pass to cb. -from __future__ import annotations -from typing import TYPE_CHECKING, Callable, Sequence +from __future__ import annotations +import re +from typing import TYPE_CHECKING, Callable, Sequence, Tuple, List, Optional +from contextlib import suppress from markdown_it import MarkdownIt from markdown_it.rules_block import StateBlock @@ -14,12 +16,26 @@ 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 = "" @@ -27,7 +43,7 @@ def _get_tag(params: str) -> tuple[str, str]: title = tag.title() elif joined != '""': # Specifically check for no title title = joined - return tag.lower(), title.strip('"') + return [tag.lower()], title def _validate(params: str) -> bool: @@ -125,12 +141,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 diff --git a/tests/fixtures/admon.md b/tests/fixtures/admon.md index 5b6e20f..ffd7d2f 100644 --- a/tests/fixtures/admon.md +++ b/tests/fixtures/admon.md @@ -66,6 +66,19 @@ Removes extra quotes from the title . +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 "foo"</p> +<p>...</p> +</div> +. + + Closes block after 2 empty lines . !!! note From 2daddecf8b021f5023bf5fb6a0745e288a947131 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 22 Jun 2023 00:56:48 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mdit_py_plugins/admon/index.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mdit_py_plugins/admon/index.py b/mdit_py_plugins/admon/index.py index b75dcbf..be8fea2 100644 --- a/mdit_py_plugins/admon/index.py +++ b/mdit_py_plugins/admon/index.py @@ -2,9 +2,10 @@ from __future__ import annotations -import re -from typing import TYPE_CHECKING, Callable, Sequence, Tuple, List, Optional 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 From 900193f560766abad6eb3b444aa2662dde11d59f Mon Sep 17 00:00:00 2001 From: Kyle King <KyleKing@users.noreply.github.com> Date: Wed, 21 Jun 2023 21:08:03 -0400 Subject: [PATCH 8/9] ci: exclude TYPE_CHECKING from text coverage --- .coveragerc | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..06e4886 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,4 @@ +[report] +exclude_lines = + pragma: no cover + if TYPE_CHECKING: From 95a04143c6a5c013156cde4f4b839ff753c62926 Mon Sep 17 00:00:00 2001 From: Kyle King <KyleKing@users.noreply.github.com> Date: Wed, 21 Jun 2023 21:08:12 -0400 Subject: [PATCH 9/9] ci: fix pre-commit failure --- mdit_py_plugins/admon/index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdit_py_plugins/admon/index.py b/mdit_py_plugins/admon/index.py index be8fea2..c39a4f7 100644 --- a/mdit_py_plugins/admon/index.py +++ b/mdit_py_plugins/admon/index.py @@ -4,7 +4,7 @@ from contextlib import suppress import re -from typing import TYPE_CHECKING, Callable, List, Optional, Sequence, Tuple +from typing import TYPE_CHECKING, Callable, List, Sequence, Tuple from markdown_it import MarkdownIt from markdown_it.rules_block import StateBlock