bgneal@883: """
bgneal@883: A python-markdown extension to turn tags with http: source attributes into
bgneal@883: tags.
bgneal@883: """
bgneal@883: from urlparse import urlparse
bgneal@883:
bgneal@883: import markdown
bgneal@883:
bgneal@883:
bgneal@883: class SslImagesTreeprocessor(markdown.treeprocessors.Treeprocessor):
bgneal@883:
bgneal@883: def run(self, root):
bgneal@883: for node in root.iter('img'):
bgneal@883: src = node.get('src')
bgneal@883: if src:
bgneal@883: url = urlparse(src)
bgneal@883: if url.scheme == 'http':
bgneal@883: node.clear()
bgneal@883: node.tag = 'a'
bgneal@883: node.text = 'Click for image'
bgneal@883: node.set('href', url.geturl())
bgneal@883:
bgneal@883:
bgneal@883: class SslImagesExtension(markdown.Extension):
bgneal@883:
bgneal@883: def extendMarkdown(self, md, md_globals):
bgneal@883: tree_proc = SslImagesTreeprocessor()
bgneal@883: md.treeprocessors.add('ssl_images', tree_proc, '>inline')