# HG changeset patch # User Brian Neal # Date 1423014672 21600 # Node ID f12751259f661eba0726294cab41050e32bb26fe # Parent 9a3019f2c7dc697f6265ec72a9aa6e5a99d23b4b Add a Markdown extension to only allow https based tags. This is not yet "turned on" in the site's markup system. diff -r 9a3019f2c7dc -r f12751259f66 core/mdexts/ssl_images.py --- /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 tags with http: source attributes into + 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') diff -r 9a3019f2c7dc -r f12751259f66 core/tests/test_mdexts.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/tests/test_mdexts.py Tue Feb 03 19:51:12 2015 -0600 @@ -0,0 +1,27 @@ +"""Testing our custom Markdown extensions.""" + +import unittest + +import markdown + +from core.mdexts.ssl_images import SslImagesExtension + + +class SslImagesExtTestCase(unittest.TestCase): + """Tests for the SslImagesExtension.""" + + def setUp(self): + self.md = markdown.Markdown(extensions=[SslImagesExtension()]) + + def test_simple(self): + self.assertEqual(self.md.convert(''), '') + self.assertEqual(self.md.convert('1'), '

1

') + + def test_no_change(self): + self.assertEqual(self.md.convert('![image](https://example.com/1.jpg)'), + u'

image

') + + def test_change(self): + text = u'![image](http://example.com/1.jpg)' + html = u'

Click for image

' + self.assertEqual(self.md.convert(text), html)