This repository was archived by the owner on Dec 26, 2023. It is now read-only.
forked from feincms/feincms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcleanse.py
More file actions
153 lines (123 loc) · 4.92 KB
/
Copy pathcleanse.py
File metadata and controls
153 lines (123 loc) · 4.92 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
import lxml.html
import lxml.html.clean
import re
cleanse_html_allowed = {
'a': ('href', 'name', 'target', 'title'),
'h2': (),
'h3': (),
'strong': (),
'em': (),
'p': (),
'ul': (),
'ol': (),
'li': (),
'span': (),
'br': (),
'sub': (),
'sup': (),
'anything': (),
}
cleanse_html_allowed_empty_tags = ('br',)
cleanse_html_merge = ('h2', 'h3', 'strong', 'em', 'ul', 'ol', 'sub', 'sup')
def cleanse_html(html):
"""
Clean HTML code from ugly copy-pasted CSS and empty elements
Removes everything not explicitly allowed in `cleanse_html_allowed`
above.
"""
doc = lxml.html.fromstring('<anything>%s</anything>' % html)
try:
ignore = lxml.html.tostring(doc, encoding=unicode)
except UnicodeDecodeError:
# fall back to slower BeautifulSoup if parsing failed
from lxml.html import soupparser
doc = soupparser.fromstring(u'<anything>%s</anything>' % html)
cleaner = lxml.html.clean.Cleaner(
allow_tags=cleanse_html_allowed.keys() + ['style'],
remove_unknown_tags=False, # preserve surrounding 'anything' tag
style=False, safe_attrs_only=False, # do not strip out style
# attributes; we still need
# the style information to
# convert spans into em/strong
# tags
)
cleaner(doc)
# walk the tree recursively, because we want to be able to remove
# previously emptied elements completely
for element in reversed(list(doc.iterdescendants())):
if element.tag == 'style':
element.drop_tree()
continue
# convert span elements into em/strong if a matching style rule
# has been found. strong has precedence, strong & em at the same
# time is not supported
elif element.tag == 'span':
style = element.attrib.get('style')
if style:
if 'bold' in style:
element.tag = 'strong'
elif 'italic' in style:
element.tag = 'em'
if element.tag == 'span': # still span
element.drop_tag() # remove tag, but preserve children and text
continue
# remove empty tags if they are not <br />
elif not element.text and element.tag not in \
cleanse_html_allowed_empty_tags and not \
len(list(element.iterdescendants())):
element.drop_tag()
continue
# remove all attributes which are not explicitly allowed
allowed = cleanse_html_allowed.get(element.tag, [])
for key in element.attrib.keys():
if key not in allowed:
del element.attrib[key]
# just to be sure, run cleaner again, but this time with even more
# strict settings
cleaner = lxml.html.clean.Cleaner(
allow_tags=cleanse_html_allowed.keys(),
remove_unknown_tags=False, # preserve surrounding 'anything' tag
style=True, safe_attrs_only=True
)
cleaner(doc)
html = lxml.html.tostring(doc, method='xml')
# remove all sorts of newline characters
html = html.replace('\n', ' ').replace('\r', ' ')
html = html.replace(' ', ' ').replace(' ', ' ')
html = html.replace('
', ' ').replace('
', ' ')
# remove wrapping tag needed by XML parser
html = re.sub(r'</?anything>', '', html)
# remove elements containing only whitespace or linebreaks
whitespace_re = re.compile(r'<([a-z0-9]+)>(<br\s*/>|\ |\ |\s)*</\1>')
while True:
new = whitespace_re.sub('', html)
if new == html:
break
html = new
# merge tags
for tag in cleanse_html_merge:
merge_str = u'</%s><%s>'
while True:
new = html.replace(merge_str, u'')
if new == html:
break
html = new
# fix p-in-p tags
p_in_p_start_re = re.compile(r'<p>(\ |\ |\s)*<p>')
p_in_p_end_re = re.compile('</p>(\ |\ |\s)*</p>')
for tag in cleanse_html_merge:
merge_start_re = re.compile('<p>(\\ |\\ |\\s)*<%s>(\\ |\\ |\\s)*<p>' % tag)
merge_end_re = re.compile('</p>(\\ |\\ |\\s)*</%s>(\\ |\\ |\\s)*</p>' % tag)
while True:
new = merge_start_re.sub('<p>', html)
new = merge_end_re.sub('</p>', new)
new = p_in_p_start_re.sub('<p>', new)
new = p_in_p_end_re.sub('</p>', new)
if new == html:
break
html = new
# remove list markers with <li> tags before them
html = re.sub(r'<li>(\ |\ |\s)*(-|\*|·)(\ |\ |\s)*', '<li>', html)
# add a space before the closing slash in empty tags
html = re.sub(r'<([^/>]+)/>', r'<\1 />', html)
return html