-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxhtmlpremailer.py
357 lines (303 loc) · 13.1 KB
/
xhtmlpremailer.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Modified by Marvin Reimer version of premailer by Peter Ben
# It will only work with XHTML!
# Original premailer:
# http://www.peterbe.com/plog/premailer.py
import codecs
from lxml import etree
from lxml.cssselect import CSSSelector
import os
import re
import urllib
import urlparse
import operator
from copy import deepcopy
__all__ = ['xhtmlPremailerError', 'xhtmlPremailer', 'transform']
class xhtmlPremailerError(Exception):
pass
grouping_regex = re.compile('([:\-\w]*){([^}]+)}')
def merge_styles(old, new, class_=''):
"""
if ::
old = 'font-size:1px; color: red'
and ::
new = 'font-size:2px; font-weight: bold'
then ::
return 'color: red; font-size:2px; font-weight: bold'
In other words, the new style bits replace the old ones.
The @class_ parameter can be something like ':hover' and if that
is there, you split up the style with '{...} :hover{...}'
Note: old could be something like '{...} ::first-letter{...}'
"""
new_keys = set()
news = []
for k, v in [x.strip().split(':', 1) for x in new.split(';') if x.strip()]:
news.append((k.strip(), v.strip()))
new_keys.add(k.strip())
groups = {}
grouped_split = grouping_regex.findall(old)
if grouped_split:
for old_class, old_content in grouped_split:
olds = []
for k, v in [x.strip().split(':', 1) for
x in old_content.split(';') if x.strip()]:
olds.append((k.strip(), v.strip()))
groups[old_class] = olds
else:
olds = []
for k, v in [x.strip().split(':', 1) for
x in old.split(';') if x.strip()]:
olds.append((k.strip(), v.strip()))
groups[''] = olds
# Perform the merge
relevant_olds = groups.get(class_, {})
merged = [style for style in relevant_olds if style[0] not in new_keys] + news
groups[class_] = merged
if len(groups) == 1:
return '; '.join('%s:%s' % (k, v) for
(k, v) in groups.values()[0])
else:
all = []
for class_, mergeable in sorted(groups.items(),
lambda x, y: cmp(x[0].count(':'),
y[0].count(':'))):
all.append('%s{%s}' % (class_,
'; '.join('%s:%s' % (k, v)
for (k, v)
in mergeable)))
return ' '.join(x for x in all if x != '{}')
_css_comments = re.compile(r'/\*.*?\*/', re.MULTILINE | re.DOTALL)
_regex = re.compile('((.*?){(.*?)})', re.DOTALL | re.M)
_semicolon_regex = re.compile(';(\s+)')
_colon_regex = re.compile(':(\s+)')
_element_selector_regex = re.compile(r'(^|\s)\w')
_importants = re.compile('\s*!important')
# These selectors don't apply to all elements. Rather, they specify
# which elements to apply to.
FILTER_PSEUDOSELECTORS = [':last-child', ':first-child', 'nth-child']
class xhtmlPremailer(object):
def __init__(self, html, base_url=None,
preserve_internal_links=False,
exclude_pseudoclasses=True,
keep_style_tags=False,
include_star_selectors=False,
remove_classes=True,
strip_important=True,
external_styles=None):
self.html = html
self.base_url = base_url
self.preserve_internal_links = preserve_internal_links
self.exclude_pseudoclasses = exclude_pseudoclasses
# whether to delete the <style> tag once it's been processed
self.keep_style_tags = keep_style_tags
self.remove_classes = remove_classes
# whether to process or ignore selectors like '* { foo:bar; }'
self.include_star_selectors = include_star_selectors
if isinstance(external_styles, basestring):
external_styles = [external_styles]
self.external_styles = external_styles
self.strip_important = strip_important
def _parse_style_rules(self, css_body, ruleset_index):
leftover = []
rules = []
css_body = _css_comments.sub('', css_body or '')
rule_index = 0
for each in _regex.findall(css_body.strip()):
__, selectors, bulk = each
bulk = _semicolon_regex.sub(';', bulk.strip())
bulk = _colon_regex.sub(':', bulk.strip())
if bulk.endswith(';'):
bulk = bulk[:-1]
for selector in (x.strip() for
x in selectors.split(',') if x.strip() and
not x.strip().startswith('@')):
if (':' in selector and self.exclude_pseudoclasses and
':' + selector.split(':', 1)[1]
not in FILTER_PSEUDOSELECTORS):
# a pseudoclass
leftover.append((selector, bulk))
continue
elif selector == '*' and not self.include_star_selectors:
continue
# Crudely calculate specificity
id_count = selector.count('#')
class_count = selector.count('.')
element_count = len(_element_selector_regex.findall(selector))
specificity = (id_count, class_count, element_count, ruleset_index, rule_index)
rules.append((specificity, selector, bulk))
rule_index += 1
return rules, leftover
def transform(self, pretty_print=True):
"""change the self.html and return it with CSS turned into style
attributes.
"""
if etree is None:
return self.html
# Marvin Reimer: changed HTMLParser to XMLParser
parser = etree.XMLParser()
stripped = self.html.strip()
tree = etree.fromstring(stripped, parser).getroottree()
page = tree.getroot()
# Marvin Reimer: Do not check if Tree is valid on XML
if page is None:
print repr(self.html)
raise xhtmlPremailerError("Could not parse the html")
assert page is not None
page = deepcopy(tree)
##
## style selectors
##
rules = []
# Marvin Reimer:
# Use xhtml namespace!
for index, style in enumerate(CSSSelector("xhtml|style", namespaces={'xhtml': 'http://www.w3.org/1999/xhtml'})(page)):
# If we have a media attribute whose value is anything other than
# 'screen', ignore the ruleset.
media = style.attrib.get('media')
if media and media != 'screen':
continue
these_rules, these_leftover = self._parse_style_rules(style.text, index)
rules.extend(these_rules)
parent_of_style = style.getparent()
if these_leftover:
style.text = '\n'.join(['%s {%s}' % (k, v) for
(k, v) in these_leftover])
elif not self.keep_style_tags:
parent_of_style.remove(style)
if self.external_styles:
for stylefile in self.external_styles:
if stylefile.startswith('http://'):
css_body = urllib.urlopen(stylefile).read()
elif os.path.exists(stylefile):
try:
f = codecs.open(stylefile)
css_body = f.read()
finally:
f.close()
else:
raise ValueError(u"Could not find external style: %s" %
stylefile)
these_rules, these_leftover = self._parse_style_rules(css_body, -1)
rules.extend(these_rules)
# rules is a tuple of (specificity, selector, styles), where specificity is a tuple
# ordered such that more specific rules sort larger.
rules.sort(key=operator.itemgetter(0))
first_time = []
first_time_styles = []
for __, selector, style in rules:
new_selector = selector
class_ = ''
if ':' in selector:
new_selector, class_ = re.split(':', selector, 1)
class_ = ':%s' % class_
# Keep filter-type selectors untouched.
if class_ in FILTER_PSEUDOSELECTORS:
class_ = ''
else:
selector = new_selector
# Marvin Reimer
# use XHTML namespace for node elements h1 -> xhtml|h1 but do not convert classes like .c1
if not selector.startswith('.') and re.match('^xhtml\|', 'selector', re.IGNORECASE) is None:
selector = 'xhtml|' + selector
sel = CSSSelector(selector, namespaces={'xhtml': 'http://www.w3.org/1999/xhtml'})
for item in sel(page):
old_style = item.attrib.get('style', '')
if not item in first_time:
new_style = merge_styles(old_style, style, class_)
first_time.append(item)
first_time_styles.append((item, old_style))
else:
new_style = merge_styles(old_style, style, class_)
item.attrib['style'] = new_style
self._style_to_basic_html_attributes(item, new_style,
force=True)
# Re-apply initial inline styles.
for item, inline_style in first_time_styles:
old_style = item.attrib.get('style', '')
if not inline_style:
continue
new_style = merge_styles(old_style, inline_style, class_)
item.attrib['style'] = new_style
self._style_to_basic_html_attributes(item, new_style, force=True)
if self.remove_classes:
# now we can delete all 'class' attributes
for item in page.xpath('//@class'):
parent = item.getparent()
del parent.attrib['class']
##
## URLs
##
if self.base_url:
for attr in ('href', 'src'):
for item in page.xpath("//@%s" % attr):
parent = item.getparent()
if attr == 'href' and self.preserve_internal_links \
and parent.attrib[attr].startswith('#'):
continue
parent.attrib[attr] = urlparse.urljoin(self.base_url,
parent.attrib[attr])
# Marvin Reimer:
# Do not force any encoding to LXML
# return etree.tostring(page, pretty_print=pretty_print, xml_declaration=True, encoding='utf8')\
# Also include xml declaration
out = etree.tostring(page, pretty_print=pretty_print, xml_declaration=True)\
.replace('<head/>','<head></head>')
if self.strip_important:
out = _importants.sub('', out)
return out
def _style_to_basic_html_attributes(self, element, style_content,
force=False):
"""given an element and styles like
'background-color:red; font-family:Arial' turn some of that into HTML
attributes. like 'bgcolor', etc.
Note, the style_content can contain pseudoclasses like:
'{color:red; border:1px solid green} :visited{border:1px solid green}'
"""
if style_content.count('}') and \
style_content.count('{') == style_content.count('{'):
style_content = style_content.split('}')[0][1:]
attributes = {}
for key, value in [x.split(':') for x in style_content.split(';')
if len(x.split(':')) == 2]:
key = key.strip()
if key == 'text-align':
attributes['align'] = value.strip()
elif key == 'background-color':
attributes['bgcolor'] = value.strip()
elif key == 'width' or key == 'height':
value = value.strip()
if value.endswith('px'):
value = value[:-2]
attributes[key] = value
#else:
# print "key", repr(key)
# print 'value', repr(value)
for key, value in attributes.items():
if key in element.attrib and not force:
# already set, don't dare to overwrite
continue
element.attrib[key] = value
def transform(html, base_url=None):
return xhtmlPremailer(html, base_url=base_url).transform()
if __name__ == '__main__':
html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style>
h1, h2 { color:red; }
strong {
text-decoration:none
}
p { font-size:2px }
p.footer { font-size: 1px}
</style>
</head>
<body>
<h1>Hi!</h1>
<p><strong>Yes!</strong></p>
<p class="footer" style="color:red">Feetnuts</p>
</body>
</html>"""
p = xhtmlPremailer(html)
print p.transform()