Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions snakemd/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import logging
import os
import re
from typing import Iterable
from enum import Enum, auto

from .elements import Element, Heading, Inline, MDList, Table
from .elements import Block, Element, Heading, Inline, MDList, Quote, Table

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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.4
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,
Expand Down Expand Up @@ -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:
"""
Expand Down
26 changes: 26 additions & 0 deletions tests/templates/test_alerts.py
Original file line number Diff line number Diff line change
@@ -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!_"