-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlutils.py
562 lines (452 loc) · 19.1 KB
/
htmlutils.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import re
import gitutils
import time
import configuration
import os
try: from json import dumps as json_encode
except: from cjson import encode as json_encode
from cStringIO import StringIO
from linkify import ALL_LINKTYPES, Context
fragments = []
for linktype in ALL_LINKTYPES:
if linktype.fragment:
fragments.append(linktype.fragment)
re_linkify = re.compile("(?:^|\\b|(?=[[<]))(" + "|".join(fragments) + ")([.,:;!?)]*(?:\\s|\\b|$))")
re_simple = re.compile("^[^ \t\r\n&<>]+$")
re_nonascii = re.compile("[^\t\n\r -\x7f]")
def htmlify(text, attributeValue=False, pretty=False):
if isinstance(text, unicode): text = re_nonascii.sub(lambda x: "&#%d;" % ord(x.group()), text.replace('&', '&').replace('<', '<').replace('>', '>'))
else: text = str(text).replace('&', '&').replace('<', '<').replace('>', '>')
if attributeValue:
if not pretty and re_simple.match(text): return text
elif "'" in text:
if '"' not in text: text = '"' + text + '"'
else: text = "'" + text.replace("'", ''') + "'"
else: text = "'" + text + "'"
return text
def jsify(what, json=False):
if what is None: return "null"
elif isinstance(what, int) or isinstance(what, long): return str(what)
else:
what = str(what)
try: what = what.decode("utf-8")
except: what = what.decode("latin-1")
result = json_encode(what)
if not json:
quote = result[0]
return result.replace("</", "<%s+%s/" % (quote, quote)).replace("<!", "<%s+%s!" % (quote, quote))
else:
return result
re_tag = re.compile("<[^>]+>")
def tabify(line, tabwidth=8, indenttabsmode=True):
index = 0
length = len(line)
column = 0
result = ""
try:
leading = True
while index < length:
tabindex = line.index("\t", index)
nontabbed = line[index:tabindex]
nontabbed_length = len(re_tag.sub("", nontabbed))
illegal = ""
if leading:
if nontabbed_length != 0:
leading = False
elif not indenttabsmode:
illegal = " ill"
width = tabwidth - (column + nontabbed_length) % tabwidth
result += nontabbed + "<b class='t w%d%s'></b>" % (width, illegal)
index = tabindex + 1
column = column + nontabbed_length + width
except:
result += line[index:]
return result
BLOCK_ELEMENTS = set(["html", "head", "body", "table", "thead", "tbody", "tfoot", "tr", "td", "th", "div", "p", "ol", "li", "select", "option", "link", "script"])
EMPTY_ELEMENTS = set(["br", "hr", "input", "link"])
def isBlockElement(name):
return name in BLOCK_ELEMENTS
def isEmptyElement(name):
return name in EMPTY_ELEMENTS
def mtime(path):
try: return long(os.stat(path).st_mtime)
except: raise
def base36(n):
s = ""
while n:
s = "0123456789abcdefghijklmnopqrstuvwxyz"[n % 36] + s
n = n // 36
return s
def getStaticResourceURI(name):
uri = "static-resource/" + name
ts = mtime(os.path.join(configuration.paths.INSTALL_DIR, "resources", name))
if ts: uri += "?" + base36(ts)
return uri
class MetaInformation(object):
def __init__(self):
self.__orderIndeces = set()
self.__externalStylesheetList = []
self.__externalStylesheetSet = set()
self.__internalStylesheetList = []
self.__internalStylesheetSet = set()
self.__externalScriptList = []
self.__externalScriptSet = set()
self.__internalScriptList = []
self.__links = {}
self.__title = None
self.__finished = False
self.__request = None
self.__base = "/"
def addExternalStylesheet(self, uri, use_static=True, order=0):
if use_static:
uri = getStaticResourceURI(uri.split("/", 1)[1])
if uri not in self.__externalStylesheetSet:
self.__orderIndeces.add(order)
self.__externalStylesheetList.append((order, uri))
self.__externalStylesheetSet.add(uri)
def addInternalStylesheet(self, text, order=0):
if text not in self.__internalStylesheetSet:
self.__orderIndeces.add(order)
self.__internalStylesheetList.append((order, text))
self.__internalStylesheetSet.add(text)
def addExternalScript(self, uri, use_static=True, order=0):
if use_static:
uri = getStaticResourceURI(uri.split("/", 1)[1])
if uri not in self.__externalScriptSet:
self.__orderIndeces.add(order)
self.__externalScriptList.append((order, uri))
self.__externalScriptSet.add(uri)
def addInternalScript(self, text, order=0):
self.__orderIndeces.add(order)
self.__internalScriptList.append((order, text))
def setTitle(self, title):
self.__title = title
def setLink(self, rel, href):
return self.__links.setdefault(rel, href)
def setBase(self, base):
self.__base = base
def setRequest(self, req):
self.__request = req
def getRequest(self):
return self.__request
def render(self, target):
if not self.__finished:
if self.__title:
target.title().text(self.__title)
if self.__base:
target.base(href=self.__base)
for rel, href in self.__links.items():
target.link(rel=rel, href=href)
for index in sorted(self.__orderIndeces):
def filtered(items): return [data for order, data in items if order==index]
for uri in filtered(self.__externalStylesheetList):
target.link(rel="stylesheet", type="text/css", href=uri)
for uri in filtered(self.__externalScriptList):
target.script(type="text/javascript", src=uri)
for text in filtered(self.__internalStylesheetList):
target.style(type="text/css").text(text.strip(), cdata=True)
for text in filtered(self.__internalScriptList):
target.script(type="text/javascript").text(text.strip(), cdata=True)
if configuration.base.IS_DEVELOPMENT:
favicon = "/static-resource/favicon-dev.png"
else:
favicon = "/static-resource/favicon.png"
target.link(rel="icon", type="image/png", href=favicon)
self.__finished = True
class PausedRendering: pass
class Fragment(object):
def __init__(self, is_element=False, req=None):
self.__children = []
self.__metaInformation = not is_element and MetaInformation() or None
def appendChild(self, child):
self.__children.append(child)
return child
def insertChild(self, child, offset=0):
self.__children.insert(offset, child)
return child
def removeChild(self, child):
assert child in self.__children
self.__children.remove(child)
def metaInformation(self):
return self.__metaInformation
def __len__(self): return len(self.__children)
def __getitem__(self, index): return self.__children[index]
def __str__(self): return "".join(map(str, self.__children))
def render(self, output, level=0, indent_before=True, stop=None, pretty=True):
for child in self.__children:
child.render(output, level, indent_before, stop=stop, pretty=pretty)
if pretty: output.write("\n")
def deleteChildren(self, count=None):
if count is None: self.__children = []
else: del self.__children[:count]
class Element(Fragment):
def __init__(self, name):
super(Element, self).__init__(True)
self.__name = name
self.__attributes = {}
self.__empty = isEmptyElement(name)
self.__preformatted = False
self.__metaInformation = None
self.__rendered = False
self.__disabled = False
def setAttribute(self, name, value):
self.__attributes[name] = value
def setPreFormatted(self):
self.__preformatted = True
def setMetaInformation(self, metaInformation):
self.__metaInformation = metaInformation
def appendChild(self, child):
assert not self.__empty
return Fragment.appendChild(self, child)
def remove(self):
self.__disabled = True
def __str__(self):
attributes = "".join([(" %s=%s" % (name, htmlify(value, True))) for name, value in self.__attributes.items()])
if isEmptyElement(self.__name):
return "<%s%s>" % (self.__name, attributes)
else:
return "<%s%s>%s</%s>" % (self.__name, attributes, Fragment.__str__(self), self.__name)
def render(self, output, level=0, indent_before=True, stop=None, pretty=True):
if self.__disabled: return
if self.__metaInformation: self.__metaInformation.render(Generator(self, None))
if pretty: indent = " " * level
else: indent = ""
if indent_before: startindent = indent
else: startindent = ""
for child in self:
if isinstance(child, Element) and isBlockElement(child.__name) or (isinstance(child, Text) or isinstance(child, Comment)) and '\n' in str(child):
linebreak = "\n"
endindent = indent
break
else:
indent_before = False
linebreak = ""
endindent = ""
if not pretty or self.__preformatted:
child_level = 0
linebreak = ""
endindent = ""
else: child_level = level + 1
attributes = "".join([(" %s=%s" % (name, htmlify(value, True, pretty))) for name, value in self.__attributes.items()])
if self.__empty:
if not self.__rendered:
output.write("%s<%s%s>" % (startindent, self.__name, attributes))
self.__rendered = True
else:
if not self.__rendered:
output.write("%s<%s%s>%s" % (startindent, self.__name, attributes, linebreak))
self.__rendered = True
children_rendered = 0
for child in self:
if self.__preformatted: child.setPreFormatted()
try:
child.render(output, child_level, indent_before, stop, pretty)
output.write(linebreak)
children_rendered += 1
except PausedRendering:
self.deleteChildren(children_rendered)
raise
self.deleteChildren()
if self == stop: raise PausedRendering
else: output.write("%s</%s>" % (endindent, self.__name))
def empty(self):
self.__empty = True
class Text(object):
def __init__(self, value, preformatted=False, cdata=False):
if cdata: self.__value = value
elif value is None: self.__value = " "
else: self.__value = htmlify(value)
self.__preformatted = preformatted
def setPreFormatted(self):
self.__preformatted = True
def render(self, output, level=0, indent_before=True, stop=None, pretty=True):
if pretty and level and not self.__preformatted and '\n' in self.__value:
indent = " " * level
if indent_before: startindent = indent
else: startindent = ""
output.write(startindent + ('\n' + indent).join([line for line in self.__value.strip().splitlines()]))
else:
output.write(self.__value)
def __str__(self):
return self.__value
class Comment(object):
def __init__(self, value):
self.__value = value.replace("--", "- -")
def setPreFormatted(self):
pass
def render(self, output, level=0, indent_before=True, stop=None, pretty=True):
if pretty and level and '\n' in self.__value:
indent = " " * level
if indent_before: startindent = indent
else: startindent = ""
output.write(startindent + "<!-- " + ('\n' + indent + " ").join(htmlify(self.__value).splitlines()) + " -->")
else:
output.write("<!-- " + self.__value + " -->")
def __str__(self):
return self.__value
class HTML(object):
def __init__(self, value):
self.__value = value
def setPreFormatted(self):
pass
def render(self, output, level=0, indent_before=True, stop=None, pretty=True):
output.write(self.__value)
def __str__(self):
return self.__value
def safestr(value):
try: return str(value)
except: return unicode(value)
class Generator(object):
def __init__(self, target, metaInformation):
self.__target = target
self.__metaInformation = metaInformation
def __eq__(self, other):
return other == self.__target
def __open(self, __name, **attributes):
target = self.__target.appendChild(Element(__name))
if "__generator__" in attributes:
del attributes["__generator__"]
generator = True
else:
generator = __name not in EMPTY_ELEMENTS
for name, value in attributes.items():
if value is not None:
target.setAttribute(name.strip("_").replace("_", "-"), safestr(value))
if not generator: return self
else: return Generator(target, self.__metaInformation)
def __getattr__(self, name):
def open(*className, **attributes):
assert len(className) == 0 or len(className) == 1
if className: return self.__open(name, _class=className[0], **attributes)
else: return self.__open(name, **attributes)
return open
def head(self, **attributes):
target = self.__target.appendChild(Element("head"))
for name, value in attributes.items():
if value is not None:
target.setAttribute(name.strip("_").replace("_", "-"), safestr(value))
target.setMetaInformation(self.__metaInformation)
return Generator(target, self.__metaInformation)
def append(self, fragment):
if fragment is not None:
if isinstance(fragment, Generator): self.__target.appendChild(fragment.__target)
else: self.__target.appendChild(fragment)
def remove(self):
self.__target.remove()
def text(self, value=None, preformatted=False, cdata=False, linkify=False, repository=None):
if linkify:
if isinstance(linkify, Context): context = linkify
else: context = Context(repository=repository)
for linktype in ALL_LINKTYPES:
url = linktype.linkify(value, context)
if url:
self.a(href=url).text(value)
break
else:
for word in re_linkify.split(value):
if word:
for linktype in ALL_LINKTYPES:
url = linktype.linkify(word, context)
if url:
self.a(href=url).text(word)
break
else:
self.text(word)
else:
self.__target.appendChild(Text(value, preformatted, cdata))
return self
def comment(self, value):
self.__target.appendChild(Comment(safestr(value)))
return self
def commentFirst(self, value):
self.__target.insertChild(Comment(safestr(value)), offset=0)
return self
def innerHTML(self, value=" "):
self.__target.appendChild(HTML(safestr(value)))
return self
def setAttribute(self, name, value):
self.__target.setAttribute(name, value)
return self
def render(self, output, level=0, stop=None, pretty=True):
self.__target.render(output, level, stop=stop, pretty=pretty)
def empty(self):
self.__target.empty()
return self
def preformatted(self):
self.__target.setPreFormatted()
return self
def addExternalStylesheet(self, uri, use_static=True, order=0):
self.__metaInformation.addExternalStylesheet(uri, use_static, order=order)
def addInternalStylesheet(self, text, order=0):
self.__metaInformation.addInternalStylesheet(text, order=order)
def addExternalScript(self, uri, use_static=True, order=0):
self.__metaInformation.addExternalScript(uri, use_static, order=order)
def addInternalScript(self, text, here=False, order=0):
if here:
self.script(type="text/javascript").text(text.strip().replace("</", "<\/"), cdata=True)
else:
self.__metaInformation.addInternalScript(text, order=order)
def setTitle(self, title):
self.__metaInformation.setTitle(title)
def setLink(self, rel, href):
self.__metaInformation.setLink(rel, href)
def setBase(self, base):
self.__metaInformation.setBase(base)
def getRequest(self):
return self.__metaInformation.getRequest()
class Document(Generator):
def __init__(self, req=None):
self.__fragment = Fragment()
Generator.__init__(self, self.__fragment, self.__fragment.metaInformation())
self.__start = time.time()
self.__generation = 0.0
self.__rendering = 0.0
self.__doctype = True
if req: self.__fragment.metaInformation().setRequest(req)
def render(self, plain=False, stop=None, pretty=True):
self.__generation += time.time() - self.__start
output = StringIO()
if not plain and self.__doctype:
output.write("<!DOCTYPE html>")
self.__doctype = False
before = time.time()
try:
Generator.render(self, output, stop=stop, pretty=pretty)
finished = True
except PausedRendering:
finished = False
after = time.time()
self.__rendering += after - before
if not plain and finished:
output.write("\n<!-- generation: %.2f ms, rendering: %.2f ms -->" % (self.__generation * 1000, self.__rendering * 1000))
self.__start = time.time()
return output.getvalue()
def __str__(self):
return self.render()
def stripStylesheet(text, compact):
if compact:
text = re.sub(r"/\*(?:[^*]|\*[^/])*\*/", "", text)
text = re.sub(r"\s*([,:;{}])\s*", lambda m: m.group(1), text)
text = re.sub(r"\s+", " ", text)
return text
if __name__ == "__main__":
generator = Document()
row = generator.html().body().table(border=1).tbody().tr()
row.td(_class="left").div(id="foo").text("Column 1\nMore text")
row.td(_class="right").text("text").comment("comment")
print generator.render()