-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathelements.py
More file actions
414 lines (351 loc) · 13.4 KB
/
Copy pathelements.py
File metadata and controls
414 lines (351 loc) · 13.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from bs4 import BeautifulSoup
from bs4.dammit import EntitySubstitution
from bs4.element import Comment
from bs4.element import Doctype
from bs4.element import NavigableString
from bs4.element import ProcessingInstruction
from bs4.element import Tag
from zpretty.attributes import PrettyAttributes
from zpretty.text import endswith_whitespace
from zpretty.text import lstrip_first_line
from zpretty.text import rstrip_last_line
from zpretty.text import startswith_whitespace
class OpenTagException(Exception):
"""We want this element to be closed"""
def __init__(self, el):
"""el is a PrettyElement instance"""
self.el = el
def __str__(self):
return "Known self closing tag %r is not closed" % self.el.context
def memo(f):
"""Simple memoize"""
key = "__zpretty_memo__" + f.__name__
def wrapped(obj):
if not hasattr(obj, key):
setattr(obj, key, f(obj))
return getattr(obj, key)
return wrapped
class PrettyElement:
"""A pretty element class that can render prettified html"""
null_tag_name = "null_tag_name"
knownself_closing_elements = [
"area",
"base",
"basefont",
"br",
"col",
"embed",
"frame",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr",
]
indent = " "
attribute_klass = PrettyAttributes
first_attribute_on_new_line = False
before_closing_multiline = ""
self_closing_singleline_attributeless_template = "{prefix}<{tag} />"
self_closing_singleline_attributefull_template = "{prefix}<{tag} {attributes} />"
self_closing_multiline_template = "\n".join(
("{prefix}<{tag} {attributes}", "{prefix}{before_closing_multiline}/>")
)
start_tag_singleline_attributeless_template = "{prefix}<{tag}>"
start_tag_singleline_attributefull_template = "{prefix}<{tag} {attributes}>"
start_tag_multiline_template = "\n".join(
("{prefix}<{tag} {attributes}", "{prefix}{before_closing_multiline}>")
)
escaper = EntitySubstitution()
preserve_text_whitespace_elements = ["pre"]
skip_text_escaping_elements = [
# Do not fiddle with the content of script tags,
# as it may contain html entities that we do not want to be escaped
"script",
# Do not fiddle with the content of style tags,
# as it may contain html entities that we do not want to be escaped
"style",
# The title and textarea tags may contain markup-like text
# that should that by HTML parser is escaped into text,
# but that we want to be rendered as markup in page templates.
"title",
"textarea",
]
def __init__(self, config, context, level=0):
"""Take something a (bs4) element and an indentation level"""
self.config = config
self.context = context
self.level = level
def __str__(self):
"""Reuse the context method"""
return str(self.context)
def __repr__(self):
"""Try to make evident:
- the element type
- the level
"""
if self.is_comment():
tag = "!--"
if self.is_text():
tag = '""'
else:
tag = self.tag
return f"<pretty:{self.level}:{tag} />"
def is_comment(self):
"""Check if this element is a comment"""
return isinstance(self.context, Comment)
def is_doctype(self):
"""Check if this element is a doctype"""
return isinstance(self.context, Doctype)
def is_text(self):
"""Check if this element is a text
Also comments and processing instructions
are instances of NavigableString,
so we have to make additional checks
"""
if not isinstance(self.context, NavigableString):
return False
if self.is_comment() or self.is_doctype() or self.is_processing_instruction():
return False
return True
def is_tag(self):
"""Check if this element is a notmal tag"""
return isinstance(self.context, Tag)
def is_self_closing(self):
"""Is this element self closing?"""
if not self.is_tag():
raise ValueError("This is not a tag")
# First check if element has some content.
# If it has it cannot be self closing
tag_name = self.tag
if self.getchildren():
if tag_name in self.knownself_closing_elements:
raise OpenTagException(self)
return False
# Then we have some know elements that we want to be self closing
if tag_name in self.knownself_closing_elements:
return True
# Also elements in the tal namespace may be prettified as self closing
# if needed, e.g.: <tal:name replace="${here/title}" />
if tag_name.startswith("tal:"):
return True
if tag_name.startswith("metal:"):
return True
# All the other elements will have an open an close tag
return False
def is_null(self):
"""We define a special tag null_tag_name to wrap text"""
return self.context.name == self.null_tag_name
def is_soup(self):
"""Check if this element is a BeautifulSoup instance"""
return isinstance(self.context, BeautifulSoup)
def is_processing_instruction(self):
"""Check if this element is a processing instruction like <?xml...>"""
return isinstance(self.context, ProcessingInstruction)
@memo
def getparent(self):
"""Return the element parent as an instance of this class"""
parent = self.context.parent
if not parent or parent.name == BeautifulSoup.ROOT_TAG_NAME:
return None
return self.__class__(self.config, parent)
@memo
def getchildren(self):
"""Return this element children as instances of this class"""
children = []
next_level = self.level + 1
for child in getattr(self.context, "children", []):
child = self.__class__(self.config, child, next_level)
try:
child.is_tag() and child.is_self_closing()
except OpenTagException:
# Fix open tags and repeat
nephews = reversed(tuple(child.context.children))
for nephew in nephews:
child.context.insert_after(nephew)
return self.getchildren()
children.append(child)
return children
@property
def tag(self):
"""Return the tag name"""
return self.context.name
@property
def text(self):
"""Return the text contained in this element (if any)
Convert the text characters to html entities
"""
if not isinstance(self.context, NavigableString):
return ""
if self.is_comment():
return self.context
if (
not self.escaper
or self.context.parent.name in self.skip_text_escaping_elements
):
return self.context.string
return self.escaper.substitute_html(self.context.string)
@property
@memo
def attributes(self):
"""Return the wrapped attributes"""
attributes = getattr(self.context, "attrs", {})
return self.attribute_klass(self.config, attributes, self)
@property
@memo
def preserve_text_whitespace(self):
children = self.getchildren()
return (
self.tag in self.preserve_text_whitespace_elements
and len(children) == 1
and children[0].is_text()
)
@memo
def render_content(self):
"""Render a properly indented the contents of this element"""
parts = []
previous_part = ""
if self.preserve_text_whitespace:
for child in self.getchildren():
return child.text
for idx, child in enumerate(self.getchildren()):
part = child()
if child.is_text():
part = lstrip_first_line(part)
elif not endswith_whitespace(previous_part):
part = lstrip_first_line(part)
else:
parts[-1] = rstrip_last_line(parts[-1])
parts.append(part)
previous_part = child()
content = "".join(parts)
if endswith_whitespace(content):
content = rstrip_last_line(content)
return content
@property
def prefix(self):
return self.indent * self.level
def render_comment(self):
"""Render a properly indented comment"""
return f"{self.prefix}<!--{self.text}-->"
def render_doctype(self):
"""Render a properly indented comment"""
doctype = f"{self.prefix}{self.context.PREFIX}{self.text}{self.context.SUFFIX}"
if isinstance(
self.context.next_sibling, NavigableString
) and self.context.next_sibling.startswith("\n"):
doctype = doctype.rstrip()
return doctype
def render_processing_instruction(self):
"""Render a properly indented processing instruction"""
return f"{self.prefix}<?{self.text.rstrip('?')}?>"
def render_soup(self):
first_child = next(self.context.children)
if isinstance(first_child, Doctype) and not self.context.is_xml:
return self.render_content()
if isinstance(first_child, ProcessingInstruction):
return self.render_content()
return f'<?xml version="1.0" encoding="utf-8"?>\n{self.render_content()}'
def render_text(self):
"""Render a properly indented text
If the text starts with spaces, strip them and add a newline.
If the text end with spaces, strip them.
"""
text = self.text
lines = text.split("\n")
if not lines:
return ""
prefix = self.prefix
if len(lines) == 1:
line = lines[0]
if not line.strip():
return "\n"
if startswith_whitespace(line):
line = f"\n{prefix}{line.lstrip()}"
if endswith_whitespace(line):
line = f"{line.rstrip()}\n"
return line
if not lines[0].strip():
rendered_lines = ["\n"]
elif startswith_whitespace(lines[0]):
rendered_lines = [f"\n{prefix}{lines[0].rstrip()}\n"]
else:
rendered_lines = [f"{lines[0]}\n"]
for line in lines[1:-1]:
if not line.strip():
rendered_lines.append("\n")
else:
rendered_lines.append(f"{line.rstrip()}\n")
if lines[-1].strip():
if lines[-1].rstrip() == lines[-1]:
if lines[-1].lstrip() == lines[-1]:
rendered_lines.append(lines[-1])
else:
rendered_lines.append(prefix + lines[-1].lstrip())
else:
rendered_lines.append("%s\n" % lines[-1].rstrip())
else:
rendered_lines.append("")
text = "".join(rendered_lines)
return text
def _render_template(self, template):
return template.format(
before_closing_multiline=self.before_closing_multiline,
attributes=self.attributes.lstrip(),
prefix=self.prefix,
tag=self.tag,
)
def render_self_closing(self):
"""Render a properly indented a self closing tag"""
attributes_len = len(self.attributes)
if attributes_len == 0:
template = self.self_closing_singleline_attributeless_template
elif attributes_len == 1:
template = self.self_closing_singleline_attributefull_template
else:
template = self.self_closing_multiline_template
return self._render_template(template)
def render_not_self_closing(self):
"""Render a properly indented not self closing tag"""
attributes_len = len(self.attributes)
if attributes_len == 0:
open_tag_template = self.start_tag_singleline_attributeless_template
elif attributes_len == 1:
open_tag_template = self.start_tag_singleline_attributefull_template
else:
open_tag_template = self.start_tag_multiline_template
text = self.text and self.render_text() or self.render_content()
if not self.preserve_text_whitespace and endswith_whitespace(text):
if text[-1] != "\n":
text = f"{rstrip_last_line(text)}\n"
close_tag_template = "{prefix}</{tag}>"
else:
close_tag_template = "</{tag}>"
open_tag = self._render_template(open_tag_template)
close_tag = self._render_template(close_tag_template)
return "{open_tag}{text}{close_tag}".format(
close_tag=close_tag, open_tag=open_tag, text=text
)
@memo
def __call__(self):
"""Render the element and its contents properly indented"""
if self.is_soup():
return self.render_soup()
if self.is_null():
return self.render_content()
if self.is_comment():
return self.render_comment()
if self.is_tag():
if self.is_self_closing():
return self.render_self_closing()
else:
return self.render_not_self_closing()
if self.is_processing_instruction():
return self.render_processing_instruction()
if self.is_doctype():
return self.render_doctype()
return self.render_text()