-
Notifications
You must be signed in to change notification settings - Fork 5
/
line_format.py
291 lines (237 loc) · 8.16 KB
/
line_format.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
import re
import fastcache
import jinja2.utils
from flask import url_for
from markupsafe import escape
import util
CTRL_COLOR = '\x03' # ^C = color
CTRL_RESET = '\x0F' # ^O = reset
CTRL_UNDERLINE = '\x1F' # ^_ = underline
CTRL_BOLD = '\x02' # ^B = bold
CTRL_REGEX = re.compile(r'(?:[%s%s%s])|(%s(?:\d{1,2})?,?(?:\d{1,2})?)' % (
CTRL_RESET,
CTRL_UNDERLINE,
CTRL_BOLD,
CTRL_COLOR
))
# Support urlization of urls with control codes immediately preceding and following
jinja2.utils._punctuation_re = re.compile(
'^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % (
'|'.join(['[\(<\x03\x0F\x1F\x02]'] + [re.escape(string) for string in ('<',)]),
'|'.join(['[\.,\)>\n\x03\x0F\x1F\x02]'] + [re.escape(string) for string in ('>', ''', '"')])
)
)
def ctrl_to_colors(text):
def try_color(char):
try:
return int(char)
except ValueError:
return None
# the first character is CTRL_COLOR
colors = text[1:].split(',')
# People who spam the color changer without any color code
# ruin it for the rest of us.
if len(text) == 1:
fg_color_id = None
bg_color_id = None
elif len(colors) == 1:
fg_color_id = try_color(colors[0])
bg_color_id = None
else:
fg_color_id = try_color(colors[0])
bg_color_id = try_color(colors[1])
return (fg_color_id, bg_color_id)
class LineState(object):
def __init__(self):
self.reset()
def reset(self):
self.fg_color = None
self.bg_color = None
self.bold = False
self.underline = False
def toggle_bold(self):
self.bold = not self.bold
def toggle_underline(self):
self.underline = not self.underline
def set_color(self, fg_color_id=None, bg_color_id=None):
self.fg_color = fg_color_id
if bg_color_id is not None:
self.bg_color = bg_color_id
def generate_span(state):
classes = []
if state.bold:
classes.append('irc-bold')
if state.underline:
classes.append('irc-underline')
# we don't display colors higher than 15
if state.fg_color is not None and state.fg_color < 16:
classes.append("irc-fg-%s" % state.fg_color)
if state.bg_color is not None and state.fg_color < 16:
classes.append("irc-bg-%s" % state.bg_color)
return "<span class=\"%s\">" % ' '.join(classes)
# Don't ask me why the filter name is different from the function name.
@util.delay_template_filter('control_codes')
@fastcache.clru_cache(maxsize=16384)
def irc_format(text, autoescape=None):
result = ''
# split text into fragments that are either plain text
# or a control code sequence
text = CTRL_REGEX.sub("\n\g<0>\n", text)
fragments = text.split("\n")
line_state = LineState()
is_inside_span = False
for fragment in fragments:
if not fragment:
# for blank fragments
continue
first_char = fragment[0]
was_control_code = True
if first_char == CTRL_COLOR:
(fg_color_id, bg_color_id) = ctrl_to_colors(fragment)
if fg_color_id or bg_color_id:
line_state.set_color(fg_color_id, bg_color_id)
else:
line_state.reset()
elif first_char == CTRL_RESET:
line_state.reset()
elif first_char == CTRL_UNDERLINE:
line_state.toggle_underline()
elif first_char == CTRL_BOLD:
line_state.toggle_bold()
else:
was_control_code = False
if was_control_code:
to_concat = ''
if is_inside_span:
to_concat = "</span>"
span = generate_span(line_state)
to_concat = "%s%s" % (to_concat, span)
is_inside_span = True
else:
to_concat = fragment
result = "%s%s" % (result, to_concat)
if is_inside_span:
result = "%s</span>" % result
return result
@util.delay_template_filter('line_style')
@fastcache.clru_cache(maxsize=16384)
def line_style(s, line_no, is_search, network=None, ctx=None):
"""
ctx is a grep Line object. Yes, I know it's duplicating s and line_no.
Deal with it.
"""
# At some point this should become yet another regex.
timestamp, rest = s.split(' ', 1)
rest_split = rest.split(' ', 1)
if len(rest_split) == 1:
user, = rest_split
msg = ''
else:
user, msg = rest_split
classes = []
msg_user_classes = []
msg_classes = []
if ctx and ctx.line_marker == ':':
classes.append("irc-highlight")
if msg.startswith("Quits"):
msg_user_classes.append("irc-part")
elif msg.startswith("Parts"):
msg_user_classes.append("irc-part")
elif msg.startswith("Joins"):
msg_user_classes.append("irc-join")
# escaping is done before this.
if msg.startswith(">"):
msg_classes.append("irc-greentext")
# Make links back to actual line if we're in search.
if is_search:
href = url_for(
'log',
network=network,
channel=ctx.channel,
date=ctx.date,
_anchor='L{}'.format(line_no),
)
id_ = ''
else:
href = '#L{}'.format(line_no)
id_ = 'L{}'.format(line_no)
# Do we have to resort to this?
h, m, s = timestamp.strip("[]").split(":")
timestamp = "[{h}:{m}<span class='seconds'>:{s}</span>]".format(h=h, m=m, s=s)
return '<span class="{line_class}">' \
'<a href="{href}" id="{id_}" class="js-line-no-highlight">{timestamp}</a> ' \
'<span class="{msg_user_class}">{user} ' \
'<span class="{msg_class}">{msg}' \
'</span>' \
'</span>' \
'</span>' \
.format(
line_class=' '.join(classes),
timestamp=timestamp,
href=href,
msg_user_class=' '.join(msg_user_classes),
msg_class=' '.join(msg_classes),
user=user,
id_=id_,
msg=msg,
)
@util.delay_template_filter('clinkify')
def clinkify(s):
splitted = s.split(' ')
for i, fragment in enumerate(splitted):
# Remove beginning punctuation
begin = re.match(r'^[\(<\x03\x0f\x1f\x02]+', fragment)
if begin:
middle_start = begin.end()
begin = begin.group()
else:
middle_start = 0
begin = ''
# Remove end punctuation.
end = re.search(r'[\.,\)>\n\x04\x0F\x1F\x02]+$', fragment[middle_start:])
if end:
middle_end = middle_start + end.start()
end = end.group()
else:
middle_end = len(fragment)
end = ''
# Has protocol?
middle = fragment[middle_start:middle_end]
if middle.startswith(('http://', 'https://', 'www.')):
unclosed_parens = middle.count('(') - middle.count(')')
# Special case for parentheses (Wikipedia), but not brackets (Slack bridge)
if end and len(end) >= unclosed_parens > 0 and end[:unclosed_parens] == ')' * unclosed_parens:
middle += end[:unclosed_parens]
end = end[unclosed_parens:]
if middle.startswith('www.'):
href = "http://" + middle
else:
href = middle
splitted[i] = "{0}<a href=\'{1}\'>{2}</a>{3}".format(escape(begin), href, escape(middle), escape(end))
else:
splitted[i] = escape(fragment)
return ' '.join(splitted)
_js_escapes = {
'\\': '\\u005C',
'\'': '\\u0027',
'"': '\\u0022',
'>': '\\u003E',
'<': '\\u003C',
'&': '\\u0026',
'=': '\\u003D',
'-': '\\u002D',
';': '\\u003B',
u'\u2028': '\\u2028',
u'\u2029': '\\u2029'
}
# Escape every ASCII character with a value less than 32.
_js_escapes.update(('%c' % z, '\\u%04X' % z) for z in range(32))
@util.delay_template_filter('escapejs')
def jinja2_escapejs_filter(value):
retval = []
for letter in value:
if letter in _js_escapes:
retval.append(_js_escapes[letter])
else:
retval.append(letter)
return jinja2.Markup("".join(retval))