From 71311e778852c91d7770e878bc7dec8b8389f46f Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Tue, 19 Dec 2023 01:12:24 +0100 Subject: [PATCH] Restrict only to GitHub callout keywords --- markdown_callouts/github_callouts.py | 7 +++- tests/github/classes-nope.yml | 17 +++++++++ tests/github/classes.yml | 56 ++++++++++++++++++++++++++++ tests/github/varied-case.yml | 6 +-- 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 tests/github/classes-nope.yml create mode 100644 tests/github/classes.yml diff --git a/markdown_callouts/github_callouts.py b/markdown_callouts/github_callouts.py index 6f933a2..6dc23dd 100644 --- a/markdown_callouts/github_callouts.py +++ b/markdown_callouts/github_callouts.py @@ -11,7 +11,7 @@ # Based on https://github.com/Python-Markdown/markdown/blob/4acb949256adc535d6e6cd84c4fb47db8dda2f46/markdown/blockprocessors.py#L277 class _GitHubCalloutsBlockProcessor(BlockQuoteProcessor): REGEX = re.compile( - r"((?:^|\n) *(?:[^>].*)?(?:^|\n)) {0,3}> *\[!([A-Za-z]{3,})\] *\n(?: *> *\n)*() *(?:> *[^\s\n]|[^\s\n>])" + r"((?:^|\n) *(?:[^>].*)?(?:^|\n)) {0,3}> *\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\] *\n(?: *> *\n)*() *(?:> *[^\s\n]|[^\s\n>])",flags=re.IGNORECASE ) def test(self, parent, block): @@ -31,7 +31,10 @@ def run(self, parent: etree.Element, blocks: list[str]) -> None: self.parser.parseBlocks(parent, [before]) kind = m[2] - admon = etree.SubElement(parent, "div", {"class": "admonition " + kind.lower()}) + css_class = kind.lower() + if css_class == 'caution': + css_class = 'danger' + admon = etree.SubElement(parent, "div", {"class": "admonition " + css_class}) title = etree.SubElement(admon, "p", {"class": "admonition-title"}) title.text = kind.title() diff --git a/tests/github/classes-nope.yml b/tests/github/classes-nope.yml new file mode 100644 index 0000000..bdcad2c --- /dev/null +++ b/tests/github/classes-nope.yml @@ -0,0 +1,17 @@ +input: | + > [!INFO] + > foo + + > [!TIPSY] + > foo +output: |- +
+

+ [!INFO] + foo +

+

+ [!TIPSY] + foo +

+
diff --git a/tests/github/classes.yml b/tests/github/classes.yml new file mode 100644 index 0000000..5cd7b2a --- /dev/null +++ b/tests/github/classes.yml @@ -0,0 +1,56 @@ +input: | + > [!NOTE] + > foo + + > [!TIP] + > foo + + > [!IMPORTANT] + > foo + + > [!WARNING] + > foo + + > [!CAUTION] + > foo +output: |- +
+

+ Note +

+

+ foo +

+
+
+

+ Tip +

+

+ foo +

+
+
+

+ Important +

+

+ foo +

+
+
+

+ Warning +

+

+ foo +

+
+
+

+ Caution +

+

+ foo +

+
diff --git a/tests/github/varied-case.yml b/tests/github/varied-case.yml index 1a1dacc..8062df6 100644 --- a/tests/github/varied-case.yml +++ b/tests/github/varied-case.yml @@ -1,5 +1,5 @@ input: | - > [!info] + > [!important] > Foo > [!TIP] @@ -8,9 +8,9 @@ input: | > [!NoTe] > Foo output: |- -
+

- Info + Important

Foo