-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformers.py
More file actions
131 lines (95 loc) · 3.47 KB
/
transformers.py
File metadata and controls
131 lines (95 loc) · 3.47 KB
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
import re
import urlparse
class BaseTransformer(object):
is_url_handler = True
def match(self, word):
''' Word matcher
Args:
word (smarturlize.smarturlize.Word)
Returns:
(bool): Indicates wheather the given word object should be handled
by this transformer or not.
'''
raise NotImplementedError
def transform(self, word):
''' Word transformer
Args:
word (smarturlize.smarturlize.Word)
Returns:
(str) The string you want to replace the given word with.
'''
raise NotImplementedError
class Registry(list):
def register(self, transformer):
'''Register transformer
Attrs:
transformer (BaseTransformer): Any subclass of BaseTransformer
Raises:
ValueError if transformer is already registered
AssertionError if transformer is not a subclass of BaseTransformer
'''
assert (isinstance(transformer, BaseTransformer),
'%s is not a subclass of BaseTransformer' % transformer)
if transformer.__name__ in (t.__name__ for t in self):
raise ValueError('Transformer %s is already registered.'
% transformer.__name__)
self.insert(0, transformer)
return transformer
def unregister(self, transformer_name):
'''Unregister transformer
Attrs:
transformer (str): Classname of a transformer
Raises:
ValueError if no transformer with the provided classname exists.
'''
for t in self:
if t.__name__ == transformer_name:
self.remove(t)
return
raise ValueError('Transformer %s is not registered' % transformer_name)
registry = Registry()
transformer = registry.register
@transformer
class ClickableLinks(BaseTransformer):
'''Converts urls to clickable links'''
def match(self, word):
return True
def transform(self, word):
return '<a href="%s">%s</a>' % (word.word, word.word)
@transformer
class DisplayImages(BaseTransformer):
'''Wraps urls in html img tags'''
def match(self, word):
image_extensions = ['jpg', 'jpeg', 'png', 'gif']
for ext in image_extensions:
if word.word.lower().endswith('.%s' % ext):
return True
return False
def transform(self, word):
return '<img src="%s" />' % word.word
@transformer
class EmailLinks(BaseTransformer):
'''Makes email addresses clickable'''
is_url_handler = False
def match(self, word):
return re.match('[\w\.-]+@[\w\.-]+\.\w{2,4}', word.word)
def transform(self, word):
return '<a href="mailto:%s">%s</a>' % (word.word, word.word)
@transformer
class YoutubeEmbed(BaseTransformer):
'''Converts youtube links to embedded youtube frames'''
width = '640px'
height = '390px'
def match(self, word):
return word.url.netloc.endswith('youtube.com')
def transform(self, word):
youtube_id = urlparse.parse_qs(word.url.query).get('v', '')[0]
params = {
'type': 'text/html',
'src': 'http://www.youtube.com/embed/%s' % youtube_id,
'height': self.height,
'width': self.width,
'frameBorder': '0',
}
params_string = ['%s="%s"' % (k, v) for k, v in params.iteritems()]
return '<iframe %s></iframe>' % ' '.join(params_string)