comparison core/management/commands/ssl_images.py @ 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 ee56c8c8cf98
comparison
equal deleted inserted replaced
866:98adae6e13a1 868:64a5acb83937
9 import logging 9 import logging
10 from optparse import make_option 10 from optparse import make_option
11 import os.path 11 import os.path
12 import re 12 import re
13 import signal 13 import signal
14 import urlparse
14 15
15 from django.core.management.base import NoArgsCommand, CommandError 16 from django.core.management.base import NoArgsCommand, CommandError
16 from django.conf import settings 17 from django.conf import settings
17 import markdown.inlinepatterns 18 import markdown.inlinepatterns
18 19
24 logger = logging.getLogger(__name__) 25 logger = logging.getLogger(__name__)
25 26
26 IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE) 27 IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE)
27 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE) 28 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE)
28 29
30 SG101_HOSTS = set(['www.surfguitar101.com', 'surfguitar101.com'])
29 MODEL_CHOICES = ['comments', 'posts'] 31 MODEL_CHOICES = ['comments', 'posts']
30 32
31 quit_flag = False 33 quit_flag = False
32 34
33 35
44 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') 46 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
45 handler.setFormatter(formatter) 47 handler.setFormatter(formatter)
46 logger.addHandler(handler) 48 logger.addHandler(handler)
47 49
48 50
51 def save_image_to_cloud(src):
52 # TODO
53 return src
54
55
49 def replace_image_markup(match): 56 def replace_image_markup(match):
50 return match.group(0) 57 src_parts = match.group(9).split()
58 if src_parts:
59 src = src_parts[0]
60 if src[0] == "<" and src[-1] == ">":
61 src = src[1:-1]
62 else:
63 src = ''
64
65 title = ''
66 if len(src_parts) > 1:
67 title = " ".join(src_parts[1:])
68 alt = match.group(2)
69
70 new_src = ''
71 if src:
72 r = urlparse.urlparse(src)
73 if r.scheme == 'http':
74 if r.hostname in SG101_HOSTS:
75 new_src = r.path # convert to relative path
76 else:
77 new_src = save_image_to_cloud(src)
78 elif r.scheme == 'https':
79 new_src = src # already https, accept it as-is
80
81 if new_src:
82 if title:
83 s = u'![{alt}]({src} "{title}")'.format(alt=alt, src=new_src, title=title)
84 else:
85 s = u'![{alt}]({src})'.format(alt=alt, src=new_src)
86 else:
87 # something's messed up, convert to a link using original src
88 s = u'[{alt}]({src})'.format(alt=alt, src=src)
89
90 return s
51 91
52 92
53 def process_post(text): 93 def process_post(text):
54 """Process the post object: 94 """Process the post object:
55 95