changeset 868:64a5acb83937

WIP on ssl_images command.
author Brian Neal <bgneal@gmail.com>
date Mon, 15 Dec 2014 19:51:12 -0600
parents 98adae6e13a1
children 63209e53f3aa
files core/management/commands/ssl_images.py core/tests/__init__.py core/tests/test_ssl_images.py
diffstat 2 files changed, 53 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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):
--- /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, '')