Pārlūkot izejas kodu

Added python3 support to img and b64img tags

This is done using a new six import, however as `six` is used by pelican
itself, this do not add a requirement.
Benjamin ABEL 9 gadi atpakaļ
vecāks
revīzija
65329013b4
2 mainītis faili ar 9 papildinājumiem un 7 dzēšanām
  1. 5 4
      liquid_tags/b64img.py
  2. 4 3
      liquid_tags/img.py

+ 5 - 4
liquid_tags/b64img.py

@@ -26,6 +26,7 @@ import re
 import base64
 import urllib2
 from .mdx_liquid_tags import LiquidTags
+import six
 
 SYNTAX = '{% b64img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}'
 
@@ -45,7 +46,7 @@ def _get_file(src):
         else:
             with open(src, 'rb') as fh:
                 return fh.read()
-    except Exception, e:
+    except Exception as e:
         raise RuntimeError('Error generating base64image: {}'.format(e))
 
 
@@ -62,7 +63,7 @@ def b64img(preprocessor, tag, markup):
     match = ReImg.search(markup)
     if match:
         attrs = dict([(key, val.strip())
-                      for (key, val) in match.groupdict().iteritems() if val])
+                      for (key, val) in six.iteritems(match.groupdict()) if val])
     else:
         raise ValueError('Error processing input. '
                          'Expected syntax: {0}'.format(SYNTAX))
@@ -79,9 +80,9 @@ def b64img(preprocessor, tag, markup):
 
     # Return the formatted text
     return "<img {0}>".format(' '.join('{0}="{1}"'.format(key, val)
-                                       for (key, val) in attrs.iteritems()))
+                                       for (key, val) in six.iteritems(attrs)))
 
 
 #----------------------------------------------------------------------
 # This import allows image tag to be a Pelican plugin
-from liquid_tags import register
+from .liquid_tags import register

+ 4 - 3
liquid_tags/img.py

@@ -24,6 +24,7 @@ Output
 """
 import re
 from .mdx_liquid_tags import LiquidTags
+import six
 
 SYNTAX = '{% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}'
 
@@ -42,7 +43,7 @@ def img(preprocessor, tag, markup):
     match = ReImg.search(markup)
     if match:
         attrs = dict([(key, val.strip())
-                      for (key, val) in match.groupdict().iteritems() if val])
+                      for (key, val) in six.iteritems(match.groupdict()) if val])
     else:
         raise ValueError('Error processing input. '
                          'Expected syntax: {0}'.format(SYNTAX))
@@ -57,9 +58,9 @@ def img(preprocessor, tag, markup):
 
     # Return the formatted text
     return "<img {0}>".format(' '.join('{0}="{1}"'.format(key, val)
-                                       for (key, val) in attrs.iteritems()))
+                                       for (key, val) in six.iteritems(attrs)))
 
 #----------------------------------------------------------------------
 # This import allows image tag to be a Pelican plugin
-from liquid_tags import register
+from .liquid_tags import register