Skip to content

Commit 54f9928

Browse files
authored
Added Alerts Feature (#175)
* Added the alerts feature * Removed dead space and organized imports * Fixed version number
1 parent fc31d2d commit 54f9928

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

snakemd/templates.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import logging
1010
import os
1111
import re
12+
from typing import Iterable
13+
from enum import Enum, auto
1214

13-
from .elements import Element, Heading, Inline, MDList, Table
15+
from .elements import Block, Element, Heading, Inline, MDList, Quote, Table
1416

1517
logger = logging.getLogger(__name__)
1618

@@ -53,6 +55,45 @@ def load(self, elements: list[Element]) -> None:
5355
self._elements = elements
5456

5557

58+
class Alerts(Template):
59+
"""
60+
Alerts are a wrapper of the Quote object to provide
61+
support for the alerts Markdown extension. While
62+
quotes can be nested in each other, alerts cannot.
63+
64+
.. versionadded:: 2.4
65+
Included for user convenience
66+
67+
:param Kind kind:
68+
the kind of alert; limited to:
69+
70+
- NOTE
71+
- TIP
72+
- IMPORTANT
73+
- WARNING
74+
- CAUTION
75+
:param str | Iterable[str | Inline | Block] message:
76+
the message you would like to show with the alert
77+
"""
78+
79+
class Kind(Enum):
80+
NOTE = auto()
81+
TIP = auto()
82+
IMPORTANT = auto()
83+
WARNING = auto()
84+
CAUTION = auto()
85+
86+
def __init__(self, kind: Kind, message: str | Iterable[str | Inline | Block]) -> None:
87+
self._kind = kind
88+
self._message = message
89+
90+
def __str__(self) -> str:
91+
return str(Quote([f"[!{self._kind.name}]", self._message]))
92+
93+
def __repr__(self) -> str:
94+
return f"Alerts(kind={self._kind!r},message={self._message!r})"
95+
96+
5697
class CSVTable(Template):
5798
"""
5899
A CSV Table is a wrapper for the Table Block,
@@ -141,7 +182,8 @@ class TableOfContents(Template):
141182
def __init__(self, levels: range = range(2, 3)) -> None:
142183
super().__init__()
143184
self._levels: range = levels
144-
logger.debug("New table of contents initialized with levels in %s", levels)
185+
logger.debug(
186+
"New table of contents initialized with levels in %s", levels)
145187

146188
def __str__(self) -> str:
147189
"""

tests/templates/test_alerts.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from snakemd.elements import Inline
2+
from snakemd.templates import Alerts
3+
4+
def test_alerts_note():
5+
alert = Alerts(Alerts.Kind.NOTE, "Hello, World!")
6+
assert str(alert) == "> [!NOTE]\n> Hello, World!"
7+
8+
def test_alerts_tip():
9+
alert = Alerts(Alerts.Kind.TIP, "Hello, World!")
10+
assert str(alert) == "> [!TIP]\n> Hello, World!"
11+
12+
def test_alerts_important():
13+
alert = Alerts(Alerts.Kind.IMPORTANT, "Hello, World!")
14+
assert str(alert) == "> [!IMPORTANT]\n> Hello, World!"
15+
16+
def test_alerts_warning():
17+
alert = Alerts(Alerts.Kind.WARNING, "Hello, World!")
18+
assert str(alert) == "> [!WARNING]\n> Hello, World!"
19+
20+
def test_alerts_caution():
21+
alert = Alerts(Alerts.Kind.CAUTION, "Hello, World!")
22+
assert str(alert) == "> [!CAUTION]\n> Hello, World!"
23+
24+
def test_alerts_inline():
25+
alert = Alerts(Alerts.Kind.NOTE, Inline("Hello, World!", italics=True))
26+
assert str(alert) == "> [!NOTE]\n> _Hello, World!_"

0 commit comments

Comments
 (0)