diff core/mdexts/ssl_images.py @ 883:f12751259f66

Add a Markdown extension to only allow https based <img> tags. This is not yet "turned on" in the site's markup system.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Feb 2015 19:51:12 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/mdexts/ssl_images.py	Tue Feb 03 19:51:12 2015 -0600
@@ -0,0 +1,28 @@
+"""
+A python-markdown extension to turn <img> tags with http: source attributes into
+<a> tags.
+"""
+from urlparse import urlparse
+
+import markdown
+
+
+class SslImagesTreeprocessor(markdown.treeprocessors.Treeprocessor):
+
+    def run(self, root):
+        for node in root.iter('img'):
+            src = node.get('src')
+            if src:
+                url = urlparse(src)
+                if url.scheme == 'http':
+                    node.clear()
+                    node.tag = 'a'
+                    node.text = 'Click for image'
+                    node.set('href', url.geturl())
+
+
+class SslImagesExtension(markdown.Extension):
+
+    def extendMarkdown(self, md, md_globals):
+        tree_proc = SslImagesTreeprocessor()
+        md.treeprocessors.add('ssl_images', tree_proc, '>inline')