annotate 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
rev   line source
bgneal@883 1 """
bgneal@883 2 A python-markdown extension to turn <img> tags with http: source attributes into
bgneal@883 3 <a> tags.
bgneal@883 4 """
bgneal@883 5 from urlparse import urlparse
bgneal@883 6
bgneal@883 7 import markdown
bgneal@883 8
bgneal@883 9
bgneal@883 10 class SslImagesTreeprocessor(markdown.treeprocessors.Treeprocessor):
bgneal@883 11
bgneal@883 12 def run(self, root):
bgneal@883 13 for node in root.iter('img'):
bgneal@883 14 src = node.get('src')
bgneal@883 15 if src:
bgneal@883 16 url = urlparse(src)
bgneal@883 17 if url.scheme == 'http':
bgneal@883 18 node.clear()
bgneal@883 19 node.tag = 'a'
bgneal@883 20 node.text = 'Click for image'
bgneal@883 21 node.set('href', url.geturl())
bgneal@883 22
bgneal@883 23
bgneal@883 24 class SslImagesExtension(markdown.Extension):
bgneal@883 25
bgneal@883 26 def extendMarkdown(self, md, md_globals):
bgneal@883 27 tree_proc = SslImagesTreeprocessor()
bgneal@883 28 md.treeprocessors.add('ssl_images', tree_proc, '>inline')