-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHTMLGenerator.py
More file actions
68 lines (54 loc) · 2.29 KB
/
HTMLGenerator.py
File metadata and controls
68 lines (54 loc) · 2.29 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
from __future__ import absolute_import
from . import ScintillaConstants
from . import Utils
def replace(text, *replacements):
for old, new in replacements:
text = text.replace(old, new)
return text
class HTMLGenerator:
def escape(self, text):
return replace(text, ('&', '&'), ('<', '<'), ('>', '>'))
def preformat(self, text):
text = self.escape(text.expandtabs())
return replace(text, (' ', ' '), ('\n', '<br/>\n'))
def markup(self, text):
# XXX This is stolen from pydoc and is way too python-centric
# there should be some way to extend it
import re
results = []
here = 0
pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|'
r'RFC[- ]?(\d+)|'
r'PEP[- ]?(\d+))\b')
while 1:
match = pattern.search(text, here)
if not match: break
start, end = match.span()
results.append(self.preformat(text[here:start]))
all, scheme, rfc, pep = match.groups()
if scheme:
results.append('<a href="%s">%s</a>' % (all, self.preformat(all)))
elif rfc:
url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc)
results.append('<a href="%s">%s</a>' % (url, self.preformat(all)))
elif pep:
url = 'http://www.python.org/peps/pep-%04d.html' % int(pep)
results.append('<a href="%s">%s</a>' % (url, self.preformat(all)))
here = end
results.append(self.preformat(text[here:]))
return ''.join(results)
def generate_css_name(state):
return state[4:].lower()
class SimpleHTMLGenerator(HTMLGenerator):
def __init__(self, state_prefix):
self.css_classes = {}
for constant in Utils.list_states(state_prefix):
self.css_classes[getattr(ScintillaConstants, constant)] = \
generate_css_name(constant)
def handle_other(self, style, text, **ignored):
css_class = self.css_classes.get(style, '')
if css_class:
self._file.write('<span class="%s">' % css_class)
self._file.write(self.markup(text))
if css_class:
self._file.write('</span>')