| 1 |
# Python Markdown |
| 2 |
|
| 3 |
# A Python implementation of John Gruber's Markdown. |
| 4 |
|
| 5 |
# Documentation: https://python-markdown.github.io/ |
| 6 |
# GitHub: https://github.com/Python-Markdown/markdown/ |
| 7 |
# PyPI: https://pypi.org/project/Markdown/ |
| 8 |
|
| 9 |
# Started by Manfred Stienstra (http://www.dwerg.net/). |
| 10 |
# Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org). |
| 11 |
# Currently maintained by Waylan Limberg (https://github.com/waylan), |
| 12 |
# Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser). |
| 13 |
|
| 14 |
# Copyright 2007-2023 The Python Markdown Project (v. 1.7 and later) |
| 15 |
# Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) |
| 16 |
# Copyright 2004 Manfred Stienstra (the original version) |
| 17 |
|
| 18 |
# License: BSD (see LICENSE.md for details). |
| 19 |
|
| 20 |
""" |
| 21 |
An extension to Python Markdown which implements legacy attributes. |
| 22 |
|
| 23 |
Prior to Python-Markdown version 3.0, the Markdown class had an `enable_attributes` |
| 24 |
keyword which was on by default and provided for attributes to be defined for elements |
| 25 |
using the format `{@key=value}`. This extension is provided as a replacement for |
| 26 |
backward compatibility. New documents should be authored using `attr_lists`. However, |
| 27 |
numerous documents exist which have been using the old attribute format for many |
| 28 |
years. This extension can be used to continue to render those documents correctly. |
| 29 |
""" |
| 30 |
|
| 31 |
from __future__ import annotations |
| 32 |
|
| 33 |
import re |
| 34 |
from markdown.treeprocessors import Treeprocessor, isString |
| 35 |
from markdown.extensions import Extension |
| 36 |
from typing import TYPE_CHECKING |
| 37 |
|
| 38 |
if TYPE_CHECKING: # pragma: no cover |
| 39 |
import xml.etree.ElementTree as etree |
| 40 |
|
| 41 |
|
| 42 |
ATTR_RE = re.compile(r'\{@([^\}]*)=([^\}]*)}') # {@id=123} |
| 43 |
|
| 44 |
|
| 45 |
class LegacyAttrs(Treeprocessor): |
| 46 |
def run(self, doc: etree.Element) -> None: |
| 47 |
"""Find and set values of attributes ({@key=value}). """ |
| 48 |
for el in doc.iter(): |
| 49 |
alt = el.get('alt', None) |
| 50 |
if alt is not None: |
| 51 |
el.set('alt', self.handleAttributes(el, alt)) |
| 52 |
if el.text and isString(el.text): |
| 53 |
el.text = self.handleAttributes(el, el.text) |
| 54 |
if el.tail and isString(el.tail): |
| 55 |
el.tail = self.handleAttributes(el, el.tail) |
| 56 |
|
| 57 |
def handleAttributes(self, el: etree.Element, txt: str) -> str: |
| 58 |
""" Set attributes and return text without definitions. """ |
| 59 |
def attributeCallback(match: re.Match[str]): |
| 60 |
el.set(match.group(1), match.group(2).replace('\n', ' ')) |
| 61 |
return ATTR_RE.sub(attributeCallback, txt) |
| 62 |
|
| 63 |
|
| 64 |
class LegacyAttrExtension(Extension): |
| 65 |
def extendMarkdown(self, md): |
| 66 |
""" Register the processor. |
| 67 |
|
| 68 |
| Class Instance | Registry | Name | Priority | |
| 69 |
| ------------------------------------------------------------- | ---------------------------------------------------------------- | ------ | :------: | |
| 70 |
| [`LegacyAttrs`][markdown.extensions.legacy_attrs.LegacyAttrs] | [`treeprocessors`][markdown.treeprocessors.build_treeprocessors] | `legacyattrs` | `15` | |
| 71 |
|
| 72 |
""" |
| 73 |
# flake8: noqa: E501 68-70 |
| 74 |
md.treeprocessors.register(LegacyAttrs(md), 'legacyattrs', 15) |
| 75 |
|
| 76 |
|
| 77 |
def makeExtension(**kwargs): # pragma: no cover |
| 78 |
return LegacyAttrExtension(**kwargs) |