-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrender_utils.py
More file actions
executable file
·298 lines (230 loc) · 8.4 KB
/
render_utils.py
File metadata and controls
executable file
·298 lines (230 loc) · 8.4 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
import codecs
from datetime import datetime
from html.parser import HTMLParser
import json
import logging
import time
import urllib
import subprocess
from flask import Markup, g, render_template, request
from slimit import minify
from smartypants import smartypants
import app_config
import copytext
logging.basicConfig(format=app_config.LOG_FORMAT)
logger = logging.getLogger(__name__)
logger.setLevel(app_config.LOG_LEVEL)
class BetterJSONEncoder(json.JSONEncoder):
"""
A JSON encoder that intelligently handles datetimes.
"""
def default(self, obj):
if isinstance(obj, datetime):
encoded_object = obj.isoformat()
else:
encoded_object = json.JSONEncoder.default(self, obj)
return encoded_object
class Includer(object):
"""
Base class for Javascript and CSS psuedo-template-tags.
See `make_context` for an explanation of `asset_depth`.
"""
def __init__(self, asset_depth=0):
self.includes = []
self.tag_string = None
self.asset_depth = asset_depth
def push(self, path):
self.includes.append(path)
return ''
def _compress(self):
raise NotImplementedError()
def _relativize_path(self, path):
relative_path = path
if relative_path.startswith('www/'):
relative_path = relative_path[4:]
depth = len(request.path.split('/')) - (2 + self.asset_depth)
while depth > 0:
relative_path = '../%s' % relative_path
depth -= 1
return relative_path
def render(self, path):
if getattr(g, 'compile_includes', False):
if path in g.compiled_includes:
timestamp_path = g.compiled_includes[path]
else:
# Add a querystring to the rendered filename to prevent caching
timestamp_path = '%s?%i' % (path, int(time.time()))
out_path = 'www/%s' % path
if path not in g.compiled_includes:
logger.info('Rendering %s' % out_path)
with codecs.open(out_path, 'w', encoding='utf-8') as f:
f.write(self._compress())
# See "fab render"
g.compiled_includes[path] = timestamp_path
markup = Markup(self.tag_string % self._relativize_path(timestamp_path))
else:
response = ','.join(self.includes)
response = '\n'.join([
self.tag_string % self._relativize_path(src) for src in self.includes
])
markup = Markup(response)
del self.includes[:]
return markup
class JavascriptIncluder(Includer):
"""
Psuedo-template tag that handles collecting Javascript and serving appropriate clean or compressed versions.
"""
def __init__(self, *args, **kwargs):
Includer.__init__(self, *args, **kwargs)
self.tag_string = '<script type="text/javascript" src="%s"></script>'
def _compress(self):
output = []
src_paths = []
for src in self.includes:
src_paths.append('www/%s' % src)
with codecs.open('www/%s' % src, encoding='utf-8') as f:
logger.info('- compressing %s' % src)
output.append(minify(f.read()))
context = make_context()
context['paths'] = src_paths
header = render_template('_js_header.js', **context)
output.insert(0, header)
return '\n'.join(output)
class CSSIncluder(Includer):
"""
Psuedo-template tag that handles collecting CSS and serving appropriate clean or compressed versions.
"""
def __init__(self, *args, **kwargs):
Includer.__init__(self, *args, **kwargs)
self.tag_string = '<link rel="stylesheet" type="text/css" href="%s" />'
def _compress(self):
output = []
src_paths = []
for src in self.includes:
src_paths.append('%s' % src)
try:
compressed_src = subprocess.check_output(["node_modules/less/bin/lessc", "-x", src])
output.append(compressed_src)
except:
logger.error('It looks like "lessc" isn\'t installed. Try running: "npm install"')
raise
context = make_context()
context['paths'] = src_paths
header = render_template('_css_header.css', **context)
output.insert(0, header)
return '\n'.join(output)
def flatten_app_config():
"""
Returns a copy of app_config containing only
configuration variables.
"""
config = {}
# Only all-caps [constant] vars get included
for k, v in app_config.__dict__.items():
if k.upper() == k:
config[k] = v
return config
def make_context(asset_depth=0):
"""
Create a base-context for rendering views.
Includes app_config and JS/CSS includers.
`asset_depth` indicates how far into the url hierarchy
the assets are hosted. If 0, then they are at the root.
If 1 then at /foo/, etc.
"""
context = flatten_app_config()
try:
context['COPY'] = copytext.Copy(app_config.COPY_PATH)
except copytext.CopyException, e:
logger.warning('Exception while add COPY to context: %s' % e)
pass
context['JS'] = JavascriptIncluder(asset_depth=asset_depth)
context['CSS'] = CSSIncluder(asset_depth=asset_depth)
return context
def urlencode_filter(s):
"""
Filter to urlencode strings.
"""
if type(s) == 'Markup':
s = s.unescape()
# Evaulate COPY elements
if type(s) is not unicode:
s = unicode(s)
s = s.encode('utf8')
s = urllib.quote_plus(s)
return Markup(s)
def smarty_filter(s):
"""
Filter to smartypants strings.
"""
if type(s) == 'Markup':
s = s.unescape()
# Evaulate COPY elements
if type(s) is not unicode:
s = unicode(s)
s = s.encode('utf-8')
s = smartypants(s)
try:
return Markup(s)
except:
logger.error('This string failed to encode: %s' % s)
return Markup(s)
class GetFirstElement(HTMLParser):
'''
Given a blob of markup, find and return the contents and attributes
of the first of a particular type of element.
Currently tuned to work on <p> and <img> elements
(return the contents of <p>'s, return the the attributes of <img>').
Here's a doctest for reference more than for actual doctest'ing.
>>> el = GetFirstElement('img')
>>> markup = '<p>First p tag<img alt="The first img" src=""></p>'
>>> el.feed(markup)
>>> print dict(el.attrs)
{u'src': u'', u'alt': u'The first img'}
'''
def __init__(self, el, without_classes=[], with_classes=[]):
'''
What element are we looking for? That gets set here.
'''
HTMLParser.__init__(self)
self.el = el.lower()
# Be able to ignore certain sorts of elements, such as photo captions,
# by requiring or ignoring specific classes
self.without_classes = without_classes
self.with_classes = with_classes
self.attrs = None
self.data = ""
# self.match_start and self.match_data helps us figure out when we've already gotten a match for the element.
self.match_start = False
self.match_end = False
self.match_data = False
self.depth = 0;
def handle_starttag(self, tag, attrs):
'''
Some elements have an opening and closing tag, those get handled differently
than the elements that are standalone.
'''
classes = dict(attrs).get('class', '').split(' ')
if tag == self.el and \
not any([c in self.without_classes for c in classes]) and \
all([c in classes for c in self.with_classes]) and \
not self.match_start:
logger.debug('Found a matching start tag: %s' % tag)
self.match_start = True
self.matched_el = tag
self.attrs = attrs
else:
if self.match_start and not self.match_end:
self.depth += 1
def handle_endtag(self, tag):
if self.match_start and not self.match_end:
self.depth = self.depth - 1
if self.depth < 0:
self.match_end = True
def handle_data(self, data):
'''
This processes the contents of the tags.
'''
if self.match_start and not self.match_end:
self.data += data