/
short_codes.py
147 lines (102 loc) · 3.92 KB
/
short_codes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""Custom filters to grab code from the web via short codes"""
import requests
import mimetypes
import re
import os
import markdown
import urlparse
from django.utils import simplejson
from django import template
from django.utils.safestring import mark_safe
from settings import MEDIA_ROOT
from codrspace.templatetags.syntax_color import _colorize_table
register = template.Library()
@register.filter(name='explosivo')
def explosivo(value):
"""
Search text for any references to supported short codes and explode them
"""
# Round-robin through all functions as if they are filter methods so we
# don't have to update some silly list of available ones when they are
# added
import sys
import types
module = sys.modules[__name__]
for name, var in vars(module).items():
if type(var) == types.FunctionType and name.startswith('filter_'):
value, match = var(value)
if not match:
value = markdown.markdown(value)
return mark_safe(value)
def filter_gist(value):
pattern = re.compile('\[gist (\d+) *\]', flags=re.IGNORECASE)
ids = re.findall(pattern, value)
if not len(ids):
return value, None
for gist_id in ids:
gist_text = ""
resp = requests.get('https://api.github.com/gists/%d' % (
int(gist_id)))
if resp.status_code != 200:
return value
content = simplejson.loads(resp.content)
# Go through all files in gist and smash 'em together
for name in content['files']:
gist_text += "%s" % (
_colorize_table(content['files'][name]['content'], None))
if content['comments'] > 0:
gist_text += '<hr>Join the conversation on ' + \
'<a href="%s#comments">github</a> (%d comments)' % (
content['html_url'], content['comments'])
# Replace just first instance of the short code found
value = re.sub(pattern, gist_text, markdown.markdown(value), count=1)
return (value, True)
def _validate_url(url):
"""Validate a url, return None if not value or url if valid"""
parsed_url = urlparse.urlparse(url)
if parsed_url.scheme != 'http' and parsed_url.scheme != 'https':
return None
if parsed_url.netloc == '':
return None
return url
def filter_url(value):
pattern = re.compile('\[url (\S+) *\]', flags=re.IGNORECASE)
urls = re.findall(pattern, value)
if not len(urls):
return (value, None)
for url in urls:
url = _validate_url(url)
if url is None:
return (value, None)
# Validate that value is actually a url
resp = requests.get(url)
if resp.status_code != 200:
return (value, None)
value = re.sub(pattern, _colorize_table(resp.content, None),
markdown.markdown(value),
count=1)
return (value, True)
def filter_upload(value):
pattern = re.compile('\[local (\S+) *\]', flags=re.IGNORECASE)
files = re.findall(pattern, value)
if not len(files):
return value, None
for file_path in files:
file_path = os.path.join(MEDIA_ROOT, file_path)
(file_type, encoding) = mimetypes.guess_type(file_path)
if file_type is None:
return (value, None)
# FIXME: Can we trust the 'guessed' mimetype?
if not file_type.startswith('text'):
return (value, None)
# FIXME: Limit to 1MB right now
try:
f = open(file_path)
except IOError:
return (value, None)
text = f.read(1048576)
f.close()
text = _colorize_table(text, None)
text += '<hr><br>'
value = re.sub(pattern, text, markdown.markdown(value), count=1)
return (value, True)