# HG changeset patch # User Brian Neal # Date 1418694672 21600 # Node ID 64a5acb83937c63d5cdd30835eb7b2b44441f78d # Parent 98adae6e13a1ef33735e00c03a693dbd15ea582a WIP on ssl_images command. diff -r 98adae6e13a1 -r 64a5acb83937 core/management/commands/ssl_images.py --- a/core/management/commands/ssl_images.py Thu Dec 04 20:32:43 2014 -0600 +++ b/core/management/commands/ssl_images.py Mon Dec 15 19:51:12 2014 -0600 @@ -11,6 +11,7 @@ import os.path import re import signal +import urlparse from django.core.management.base import NoArgsCommand, CommandError from django.conf import settings @@ -26,6 +27,7 @@ IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE) IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE) +SG101_HOSTS = set(['www.surfguitar101.com', 'surfguitar101.com']) MODEL_CHOICES = ['comments', 'posts'] quit_flag = False @@ -46,8 +48,46 @@ logger.addHandler(handler) +def save_image_to_cloud(src): + # TODO + return src + + def replace_image_markup(match): - return match.group(0) + src_parts = match.group(9).split() + if src_parts: + src = src_parts[0] + if src[0] == "<" and src[-1] == ">": + src = src[1:-1] + else: + src = '' + + title = '' + if len(src_parts) > 1: + title = " ".join(src_parts[1:]) + alt = match.group(2) + + new_src = '' + if src: + r = urlparse.urlparse(src) + if r.scheme == 'http': + if r.hostname in SG101_HOSTS: + new_src = r.path # convert to relative path + else: + new_src = save_image_to_cloud(src) + elif r.scheme == 'https': + new_src = src # already https, accept it as-is + + if new_src: + if title: + s = u'![{alt}]({src} "{title}")'.format(alt=alt, src=new_src, title=title) + else: + s = u'![{alt}]({src})'.format(alt=alt, src=new_src) + else: + # something's messed up, convert to a link using original src + s = u'[{alt}]({src})'.format(alt=alt, src=src) + + return s def process_post(text): diff -r 98adae6e13a1 -r 64a5acb83937 core/tests/test_ssl_images.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/tests/test_ssl_images.py Mon Dec 15 19:51:12 2014 -0600 @@ -0,0 +1,12 @@ +"""Unit tests for the ssl_images management command.""" + +import unittest + +from core.management.commands.ssl_images import process_post + + +class ProcessPostTestCase(unittest.TestCase): + + def test_empty_string(self): + s = process_post('') + self.assertEqual(s, '')