from marko import inline from marko.helpers import MarkoExtension class ExtendedImage(inline.InlineElement): pattern = r'!\[(.*)\]\(((?:[^"\s\\]|\\.)+|"(?:[^"\\]|\\.)*")\s((?:[^"\s\\]|\\.)+|"(?:[^"\\]|\\.)*")\s((?:[^"\s\\]|\\.)+|"(?:[^"\\]|\\.)*")\s((?:[^"\s\\]|\\.)+|"(?:[^"\\]|\\.)*")\)' parse_children = True def __init__(self, match): self.alt = self.__strip_quotes(match.group(1)) self.src = self.__strip_quotes(match.group(2)) self.title = self.__strip_quotes(match.group(3)) self.href = self.__strip_quotes(match.group(4)) self.figcaption = self.__strip_quotes(match.group(5)) @staticmethod def __strip_quotes(text: str) -> str: if text.startswith('"'): return text[1:-1] return text class ExtendedImageRenderer: def render_extended_image(self, element): img = f'{element.alt}' if element.href != "": img = f'{img}' if element.figcaption != "": img = f"
{img}
{element.figcaption}
" return img ExtendedImage = MarkoExtension( elements=[ExtendedImage], renderer_mixins=[ExtendedImageRenderer] )