From 1f409c52fdc7be1ed8e6481fcab514dcb899abc5 Mon Sep 17 00:00:00 2001 From: jrg94 <11050412+jrg94@users.noreply.github.com> Date: Sun, 12 Oct 2025 22:53:43 -0400 Subject: [PATCH 1/3] Added the alerts feature --- snakemd/templates.py | 46 ++++++++++++++++++++++++++++++++-- tests/templates/test_alerts.py | 26 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/templates/test_alerts.py diff --git a/snakemd/templates.py b/snakemd/templates.py index 375d1d9..6939323 100644 --- a/snakemd/templates.py +++ b/snakemd/templates.py @@ -4,13 +4,15 @@ """ from __future__ import annotations +from enum import Enum, auto import csv import logging import os import re +from typing import Iterable -from .elements import Element, Heading, Inline, MDList, Table +from .elements import Block, Element, Heading, Inline, MDList, Quote, Table logger = logging.getLogger(__name__) @@ -53,6 +55,45 @@ def load(self, elements: list[Element]) -> None: self._elements = elements +class Alerts(Template): + """ + Alerts are a wrapper of the Quote object to provide + support for the alerts Markdown extension. While + quotes can be nested in each other, alerts cannot. + + .. versionadded:: 2.5 + Included for user convenience + + :param Kind kind: + the kind of alert; limited to: + + - NOTE + - TIP + - IMPORTANT + - WARNING + - CAUTION + :param str | Iterable[str | Inline | Block] message: + the message you would like to show with the alert + """ + + class Kind(Enum): + NOTE = auto() + TIP = auto() + IMPORTANT = auto() + WARNING = auto() + CAUTION = auto() + + def __init__(self, kind: Kind, message: str | Iterable[str | Inline | Block]) -> None: + self._kind = kind + self._message = message + + def __str__(self) -> str: + return str(Quote([f"[!{self._kind.name}]", self._message])) + + def __repr__(self) -> str: + return f"Alerts(kind={self._kind!r},message={self._message!r})" + + class CSVTable(Template): """ A CSV Table is a wrapper for the Table Block, @@ -141,7 +182,8 @@ class TableOfContents(Template): def __init__(self, levels: range = range(2, 3)) -> None: super().__init__() self._levels: range = levels - logger.debug("New table of contents initialized with levels in %s", levels) + logger.debug( + "New table of contents initialized with levels in %s", levels) def __str__(self) -> str: """ diff --git a/tests/templates/test_alerts.py b/tests/templates/test_alerts.py new file mode 100644 index 0000000..4679448 --- /dev/null +++ b/tests/templates/test_alerts.py @@ -0,0 +1,26 @@ +from snakemd.elements import Inline +from snakemd.templates import Alerts + +def test_alerts_note(): + alert = Alerts(Alerts.Kind.NOTE, "Hello, World!") + assert str(alert) == "> [!NOTE]\n> Hello, World!" + +def test_alerts_tip(): + alert = Alerts(Alerts.Kind.TIP, "Hello, World!") + assert str(alert) == "> [!TIP]\n> Hello, World!" + +def test_alerts_important(): + alert = Alerts(Alerts.Kind.IMPORTANT, "Hello, World!") + assert str(alert) == "> [!IMPORTANT]\n> Hello, World!" + +def test_alerts_warning(): + alert = Alerts(Alerts.Kind.WARNING, "Hello, World!") + assert str(alert) == "> [!WARNING]\n> Hello, World!" + +def test_alerts_caution(): + alert = Alerts(Alerts.Kind.CAUTION, "Hello, World!") + assert str(alert) == "> [!CAUTION]\n> Hello, World!" + +def test_alerts_inline(): + alert = Alerts(Alerts.Kind.NOTE, Inline("Hello, World!", italics=True)) + assert str(alert) == "> [!NOTE]\n> _Hello, World!_" From 505e6ecc6268d07c69545fa4f88081e2bda06f8e Mon Sep 17 00:00:00 2001 From: jrg94 <11050412+jrg94@users.noreply.github.com> Date: Sun, 12 Oct 2025 22:54:14 -0400 Subject: [PATCH 2/3] Removed dead space and organized imports --- snakemd/templates.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/snakemd/templates.py b/snakemd/templates.py index 6939323..5b574aa 100644 --- a/snakemd/templates.py +++ b/snakemd/templates.py @@ -4,13 +4,13 @@ """ from __future__ import annotations -from enum import Enum, auto import csv import logging import os import re from typing import Iterable +from enum import Enum, auto from .elements import Block, Element, Heading, Inline, MDList, Quote, Table @@ -60,13 +60,13 @@ class Alerts(Template): Alerts are a wrapper of the Quote object to provide support for the alerts Markdown extension. While quotes can be nested in each other, alerts cannot. - + .. versionadded:: 2.5 Included for user convenience - + :param Kind kind: the kind of alert; limited to: - + - NOTE - TIP - IMPORTANT @@ -75,21 +75,21 @@ class Alerts(Template): :param str | Iterable[str | Inline | Block] message: the message you would like to show with the alert """ - + class Kind(Enum): NOTE = auto() TIP = auto() IMPORTANT = auto() WARNING = auto() CAUTION = auto() - + def __init__(self, kind: Kind, message: str | Iterable[str | Inline | Block]) -> None: self._kind = kind self._message = message - + def __str__(self) -> str: return str(Quote([f"[!{self._kind.name}]", self._message])) - + def __repr__(self) -> str: return f"Alerts(kind={self._kind!r},message={self._message!r})" From 5929912a00ae73e87b3107f04854f71d784a140b Mon Sep 17 00:00:00 2001 From: jrg94 <11050412+jrg94@users.noreply.github.com> Date: Sun, 12 Oct 2025 22:56:27 -0400 Subject: [PATCH 3/3] Fixed version number --- snakemd/templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snakemd/templates.py b/snakemd/templates.py index 5b574aa..bef9736 100644 --- a/snakemd/templates.py +++ b/snakemd/templates.py @@ -61,7 +61,7 @@ class Alerts(Template): support for the alerts Markdown extension. While quotes can be nested in each other, alerts cannot. - .. versionadded:: 2.5 + .. versionadded:: 2.4 Included for user convenience :param Kind kind: