Files
md_to_gost/md2gost/extended_markdown/caption.py
T
Igor20264 516abe7b83
Python application / build (push) Has been cancelled
BigUpdate
2026-09-03 10:44:08 +03:00

34 lines
900 B
Python

from marko.block import BlockElement
from marko.source import Source
from re import Match, compile as re_compile
_LISTING_FLAG_RE = re_compile(r"(?i)(?:^|\s)\+?listing\b")
class Caption(BlockElement):
"""Represents caption element
Syntax: %label Caption text [+listing]
"""
pattern = r"\%(\w+)( (.+))?"
def __init__(self, match: Match[str]):
self.unique_name = match.group(1)
raw = (match.group(3) or "").strip()
self.with_listing = bool(_LISTING_FLAG_RE.search(raw))
if self.with_listing:
raw = _LISTING_FLAG_RE.sub(" ", raw).strip()
self.text = raw or None
@classmethod
def match(cls, source: Source) -> Match[str] | None:
return source.expect_re(cls.pattern)
@classmethod
def parse(cls, source: Source) -> Match[str] | None:
m = source.match
source.consume()
return m