Skip to content

Commit 82f9dad

Browse files
committed
feat: support python markdown < 3.6
1 parent 796ed98 commit 82f9dad

File tree

9 files changed

+71
-37
lines changed

9 files changed

+71
-37
lines changed

martor/extensions/del_ins.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#! /usr/bin/env python
2-
31
"""
42
Del/Ins Extension for Python-Markdown
53
=====================================
@@ -26,23 +24,21 @@
2624
See LICENSE.md for details.
2725
"""
2826

29-
3027
import markdown
3128
from markdown.inlinepatterns import SimpleTagPattern
3229

33-
3430
DEL_RE = r"(\~\~)(.+?)(\~\~)"
3531
INS_RE = r"(\+\+)(.+?)(\+\+)"
3632

3733

3834
class DelInsExtension(markdown.extensions.Extension):
3935
"""Adds del_ins extension to Markdown class."""
4036

41-
def extendMarkdown(self, md, md_globals):
37+
def extendMarkdown(self, md: markdown.core.Markdown, *args):
4238
del_tag = SimpleTagPattern(DEL_RE, "del")
4339
ins_tag = SimpleTagPattern(INS_RE, "ins")
44-
md.inlinePatterns.add("del", del_tag, "<not_strong")
45-
md.inlinePatterns.add("ins", ins_tag, "<not_strong")
40+
md.inlinePatterns.register(del_tag, "del", 10)
41+
md.inlinePatterns.register(ins_tag, "ins", 11)
4642

4743

4844
def makeExtension(*args, **kwargs):

martor/extensions/emoji.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from xml.etree import ElementTree
2+
13
import markdown
24

35
from ..settings import MARTOR_MARKDOWN_BASE_EMOJI_URL as EMOJI_URL
@@ -666,17 +668,17 @@ def handleMatch(self, m):
666668
return emoji
667669

668670
url = "{0}{1}.png".format(EMOJI_URL, emoji.replace(":", ""))
669-
el = markdown.util.etree.Element("img")
671+
el = ElementTree.Element("img")
670672
el.set("src", url)
671673
el.set("class", "marked-emoji")
672674
el.text = markdown.util.AtomicString(emoji)
673675
return el
674676

675677

676678
class EmojiExtension(markdown.Extension):
677-
def extendMarkdown(self, md, md_globals):
679+
def extendMarkdown(self, md: markdown.core.Markdown, *args):
678680
"""Setup `emoji_img` with EmojiPattern"""
679-
md.inlinePatterns["emoji_img"] = EmojiPattern(EMOJI_RE, md)
681+
md.inlinePatterns.register(EmojiPattern(EMOJI_RE, md), "emoji_img", 12)
680682

681683

682684
def makeExtension(*args, **kwargs):

martor/extensions/escape_html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class EscapeHtml(markdown.Extension):
5-
def extendMarkdown(self, md):
5+
def extendMarkdown(self, md: markdown.core.Markdown, *args):
66
md.preprocessors.deregister("html_block")
77
md.inlinePatterns.deregister("html")
88

martor/extensions/mdx_video.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#!/usr/bin/env python
1+
from xml.etree import ElementTree
22

33
import markdown
4-
from markdown.util import etree
54

65

76
class VideoExtension(markdown.Extension):
@@ -29,13 +28,13 @@ def __init__(self, **kwargs):
2928
for key, value in kwargs.items():
3029
self.setConfig(key, str(value))
3130

32-
def add_inline(self, md, name, klass, re):
31+
def add_inline(self, md: markdown.core.Markdown, name: str, klass: type, re: str):
3332
pattern = klass(re)
3433
pattern.md = md
3534
pattern.ext = self
36-
md.inlinePatterns.add(name, pattern, "<reference")
35+
md.inlinePatterns.register(pattern, name, 15)
3736

38-
def extendMarkdown(self, md, md_globals):
37+
def extendMarkdown(self, md, *args):
3938
self.add_inline(
4039
md,
4140
"dailymotion",
@@ -131,7 +130,7 @@ def handleMatch(self, m):
131130

132131

133132
def render_iframe(url, width, height):
134-
iframe = etree.Element("iframe")
133+
iframe = ElementTree.Element("iframe")
135134
iframe.set("width", width)
136135
iframe.set("height", height)
137136
iframe.set("src", url)
@@ -141,16 +140,16 @@ def render_iframe(url, width, height):
141140

142141

143142
def flash_object(url, width, height):
144-
obj = etree.Element("object")
143+
obj = ElementTree.Element("object")
145144
obj.set("type", "application/x-shockwave-flash")
146145
obj.set("width", width)
147146
obj.set("height", height)
148147
obj.set("data", url)
149-
param = etree.Element("param")
148+
param = ElementTree.Element("param")
150149
param.set("name", "movie")
151150
param.set("value", url)
152151
obj.append(param)
153-
param = etree.Element("param")
152+
param = ElementTree.Element("param")
154153
param.set("name", "allowFullScreen")
155154
param.set("value", "true")
156155
obj.append(param)

martor/extensions/mention.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from xml.etree import ElementTree
2+
13
import markdown
24
from django.contrib.auth import get_user_model
35

@@ -28,22 +30,21 @@ def handleMatch(self, m):
2830
) # noqa: E501
2931

3032
"""Makesure `username` is registered and activated."""
31-
if MARTOR_ENABLE_CONFIGS["mention"] == "true":
32-
if users.exists():
33-
url = "{0}{1}/".format(
34-
MARTOR_MARKDOWN_BASE_MENTION_URL, username
35-
) # noqa: E501
36-
el = markdown.util.etree.Element("a")
37-
el.set("href", url)
38-
el.set("class", "direct-mention-link")
39-
el.text = markdown.util.AtomicString("@" + username)
40-
return el
33+
if MARTOR_ENABLE_CONFIGS["mention"] == "true" and users.exists():
34+
url = "{0}{1}/".format(
35+
MARTOR_MARKDOWN_BASE_MENTION_URL, username
36+
) # noqa: E501
37+
el = ElementTree.Element("a")
38+
el.set("href", url)
39+
el.set("class", "direct-mention-link")
40+
el.text = markdown.util.AtomicString("@" + username)
41+
return el
4142

4243

4344
class MentionExtension(markdown.Extension):
44-
def extendMarkdown(self, md, md_globals):
45+
def extendMarkdown(self, md: markdown.core.Markdown, *args):
4546
"""Setup `mention_link` with MentionPattern"""
46-
md.inlinePatterns["mention_link"] = MentionPattern(MENTION_RE, md)
47+
md.inlinePatterns.register(MentionPattern(MENTION_RE, md), "mention_link", 13)
4748

4849

4950
def makeExtension(*args, **kwargs):

martor/extensions/urlize.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
3838
"""
3939

40+
from xml.etree import ElementTree
41+
4042
import markdown
4143

4244
# Global Vars
@@ -61,13 +63,13 @@ def handleMatch(self, m):
6163

6264
text = url
6365

64-
if not url.split("://")[0] in ("http", "https", "ftp"):
66+
if url.split("://")[0] not in ("http", "https", "ftp"):
6567
if "@" in url and "/" not in url:
6668
url = "mailto:" + url
6769
else:
6870
url = "http://" + url
6971

70-
el = markdown.util.etree.Element("a")
72+
el = ElementTree.Element("a")
7173
el.set("href", url)
7274
el.text = markdown.util.AtomicString(text)
7375
return el
@@ -76,9 +78,9 @@ def handleMatch(self, m):
7678
class UrlizeExtension(markdown.Extension):
7779
"""Urlize Extension for Python-Markdown."""
7880

79-
def extendMarkdown(self, md, md_globals):
81+
def extendMarkdown(self, md: markdown.core.Markdown, *args):
8082
"""Replace autolink with UrlizePattern"""
81-
md.inlinePatterns["autolink"] = UrlizePattern(URLIZE_RE, md)
83+
md.inlinePatterns.register(UrlizePattern(URLIZE_RE, md), "autolink", 14)
8284

8385

8486
def makeExtension(*args, **kwargs):

requirements-dev.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-r requirements.txt
2+
3+
isort
4+
black
5+
flake8

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
urllib3<2 ; python_version < '3.7' # urllib3 2.0 dropped support for Python 3.6
22
zipp<3.7 ; python_version < '3.7' # zipp 3.7.0 dropped support for Python 3.6
33
Django>=3.2, <=5.0.3
4-
Markdown<3.4
4+
Markdown<3.6
55
requests
66
bleach
77
tzdata

requirements2.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
asgiref==3.8.1
2+
black==24.3.0
3+
bleach==6.1.0
4+
certifi==2024.2.2
5+
charset-normalizer==3.3.2
6+
click==8.1.7
7+
Django==4.2.11
8+
flake8==7.0.0
9+
idna==3.6
10+
importlib_metadata==7.1.0
11+
isort==5.13.2
12+
Markdown==3.5.2
13+
martor==1.6.42
14+
mccabe==0.7.0
15+
mypy-extensions==1.0.0
16+
packaging==24.0
17+
pathspec==0.12.1
18+
platformdirs==4.2.0
19+
pycodestyle==2.11.1
20+
pyflakes==3.2.0
21+
requests==2.31.0
22+
six==1.16.0
23+
sqlparse==0.4.4
24+
tomli==2.0.1
25+
typing_extensions==4.10.0
26+
tzdata==2024.1
27+
urllib3==2.2.1
28+
webencodings==0.5.1
29+
zipp==3.18.1

0 commit comments

Comments
 (0)