Browse Source

Port pagure to markdown 3.0+ while remaining backward compatible

Lift the restriction in the requirements.txt

Fixes https://pagure.io/pagure/issue/3668

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
Pierre-Yves Chibon 5 years ago
parent
commit
8ed43b1f95
2 changed files with 23 additions and 5 deletions
  1. 22 4
      pagure/pfmarkdown.py
  2. 1 1
      requirements.txt

+ 22 - 4
pagure/pfmarkdown.py

@@ -34,6 +34,16 @@ import pagure.lib.query
 from pagure.config import config as pagure_config
 
 
+try:
+    from markdown.inlinepatterns import ImagePattern as ImagePattern
+
+    MK_VERSION = 2
+except ImportError:
+    from markdown.inlinepatterns import ImageInlineProcessor as ImagePattern
+
+    MK_VERSION = 3
+
+
 # the (?<!\w) (and variants) we use a lot in all these regexes is a
 # negative lookbehind assertion. It means 'match when the preceding
 # character is not in the \w class'. This stops us from starting a
@@ -325,11 +335,15 @@ class AutolinkPattern2(markdown.inlinepatterns.Pattern):
         return el
 
 
-class ImagePatternLazyLoad(markdown.inlinepatterns.ImagePattern):
+class ImagePatternLazyLoad(ImagePattern):
     """ Customize the image element matched for lazyloading. """
 
-    def handleMatch(self, m):
-        el = super(ImagePatternLazyLoad, self).handleMatch(m)
+    def handleMatch(self, m, *args):
+        out = super(ImagePatternLazyLoad, self).handleMatch(m, *args)
+        if MK_VERSION == 3:
+            el = out[0]
+        else:
+            el = out
 
         # Add a noscript tag with the untouched img tag
         noscript = markdown.util.etree.Element("noscript")
@@ -348,7 +362,11 @@ class ImagePatternLazyLoad(markdown.inlinepatterns.ImagePattern):
         outel.append(img)
         outel.append(noscript)
 
-        return outel
+        output = outel
+        if MK_VERSION == 3:
+            output = (outel, out[1], out[2])
+
+        return output
 
 
 class PagureExtension(markdown.extensions.Extension):

+ 1 - 1
requirements.txt

@@ -14,7 +14,7 @@ enum34;python_version<"3.4"
 flask
 flask-wtf
 kitchen
-markdown < 3.0
+markdown
 munch
 Pillow
 psutil