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