Commit 9c783032d683b773b3bb5086b2a636fc126f0ee1

Authored by Sergio Oliveira
1 parent 5346dbc8

Finding and interpreting links inside email msgs

src/super_archives/templates/superarchives/tags/display_message.html
@@ -4,6 +4,6 @@ @@ -4,6 +4,6 @@
4 {% for message, class in messages %} 4 {% for message, class in messages %}
5 {% if class == 'reply' %} 5 {% if class == 'reply' %}
6 <button class="btn btn-info btn-xs" onclick="$(this).next().toggle();">...</button> 6 <button class="btn btn-info btn-xs" onclick="$(this).next().toggle();">...</button>
7 - <pre style="display: none; color: #707";>{% else %}<pre>{% endif %}{{ message }}</pre> 7 + <div style="display: none; color: #707";>{% else %}<div>{% endif %}{{ message|safe|linebreaksbr }}</div>
8 {% endfor %} 8 {% endfor %}
9 {% endcache %} 9 {% endcache %}
src/super_archives/templatetags/superarchives.py
@@ -14,15 +14,27 @@ TEMPLATE_PATH = &#39;superarchives/tags/&#39; @@ -14,15 +14,27 @@ TEMPLATE_PATH = &#39;superarchives/tags/&#39;
14 EXTENDED_PUNCTUATION = '!"#$%&\'()*+,-./:;=?@[\\]^_`{|}~ \t\n\r\x0b\x0c' 14 EXTENDED_PUNCTUATION = '!"#$%&\'()*+,-./:;=?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
15 RE_WRAPPED_BY_HTML = re.compile(r'^<[a-z]+[^>]*>.*</[a-z]+[^>]*>$', 15 RE_WRAPPED_BY_HTML = re.compile(r'^<[a-z]+[^>]*>.*</[a-z]+[^>]*>$',
16 re.MULTILINE|re.IGNORECASE|re.DOTALL) 16 re.MULTILINE|re.IGNORECASE|re.DOTALL)
  17 +RE_LINKS = re.compile(r'(?P<link>https?://[^ \t\r\n\<]+)')
  18 +
  19 +
  20 +def find_links(block):
  21 + links = RE_LINKS.finditer(block)
  22 +
  23 + block, n = RE_LINKS.subn(r'<a target="_blank" href="\g<link>">\g<link></a>',
  24 + block)
  25 +
  26 + return block
17 27
18 28
19 def join(block): 29 def join(block):
20 block_txt = u''.join(block) 30 block_txt = u''.join(block)
21 31
22 if RE_WRAPPED_BY_HTML.match(block_txt.strip()): 32 if RE_WRAPPED_BY_HTML.match(block_txt.strip()):
23 - return html2text(block_txt) 33 + block = html2text(block_txt)
  34 + else:
  35 + block = block_txt
24 36
25 - return block_txt 37 + return find_links(block)
26 38
27 def is_reply(line, message, thread): 39 def is_reply(line, message, thread):
28 clean_line = line.strip() 40 clean_line = line.strip()
@@ -41,8 +53,7 @@ def is_reply(line, message, thread): @@ -41,8 +53,7 @@ def is_reply(line, message, thread):
41 return False 53 return False
42 54
43 55
44 -@register.inclusion_tag(TEMPLATE_PATH + 'display_message.html',  
45 - takes_context=False) 56 +@register.inclusion_tag(TEMPLATE_PATH + 'display_message.html')
46 def display_message(email, thread): 57 def display_message(email, thread):
47 message = email.body 58 message = email.body
48 messages = [] 59 messages = []