-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhighlight
executable file
·227 lines (192 loc) · 7.03 KB
/
highlight
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
#!/usr/bin/env python
# Copyright 2011, 2014 (C) Daniel Richman, Adam Greig. License: GNU GPL 3
# Message types:
# [hh:mm] <nick> message
# [hh:mm] Action: nick action
# [hh:mm] Nick change: nick -> nick
# [hh:mm] nick (user@host) (some join, part, netsplit, etc message)
# [hh:mm] anything else (mode change, topic, blah)
import sys
import os
import calendar
import datetime
import traceback
from xml.sax.saxutils import escape
import jinja2
config = {
"template": "highlight.template.html",
"stylesheet": "../highlight.css",
"raw_dir": "../logs"
}
class Line(object):
nick_brockets = False
def html_time(self):
tag = u"L{lineno}".format(lineno=self.lineno)
return u'<a class="time" name="{tag}" href="#{tag}">{time}</a>'.format(
tag=escape(tag), time=escape(self.time))
def html_part(self, name, text=None, csscls=None):
if text == None:
text = getattr(self, name)
return u"<span class='{name}'>{text}</span>".format(
name=name, text=escape(text))
def html_nick(self, nick=None):
if nick == None:
nick = self.nick
fmt = u"{left}<span class='nick term_{colour}'>{nick}</span>{right}"
if self.nick_brockets:
args = {"left": escape('<'), "right": escape('>')}
else:
args = {"left": '', "right": ''}
args["colour"] = self.nick_colour(nick)
args["nick"] = escape(nick)
return fmt.format(**args)
def nick_colour(self, nick):
# This is taken from irssi script nick_color.pl,
# by Timo Sirainen & Ian Peters; Public Domain
allowed_terminal_colours = [2, 3, 4, 5, 6, 7, 9, 10, 2, 3, 4]
return allowed_terminal_colours[sum(map(ord, nick)) % 11]
class ChatLine(Line):
nick_brockets = True
def __init__(self, lineno, time, nick, message):
self.lineno = lineno
self.time = time
self.nick = nick
self.message = message
def __unicode__(self):
return u"{time} {nick} <span class='message'>{message}</span>".format(
time=self.html_time(),
nick=self.html_nick(),
message=self.html_part("message"))
class ActionLine(Line):
def __init__(self, lineno, time, nick, action):
self.lineno = lineno
self.time = time
self.nick = nick
self.action = action
def __unicode__(self):
return u"{time} {ltyp} {nick} {action}".format(
time=self.html_time(),
ltyp=self.html_part("line_type", "Action:"),
nick=self.html_nick(),
action=self.html_part("action"))
class NickChangeLine(Line):
def __init__(self, lineno, time, nick_before, nick_after):
self.lineno = lineno
self.time = time
self.nick_before = nick_before
self.nick_after = nick_after
def __unicode__(self):
return u"{time} {ltyp} <span class='nick_change'>{nick_before} " \
"{thing} {nick_after}</span>".format(
time=self.html_time(),
ltyp=self.html_part("line_type", "Nick change:"),
nick_before=self.html_nick(self.nick_before),
nick_after=self.html_nick(self.nick_after),
thing=escape("->"))
class UnknownTimeLine(Line):
def __init__(self, lineno, time, text):
self.lineno = lineno
self.time = time
self.text = text
def __unicode__(self):
return self.html_time() + " " + \
self.html_part("unknown", self.text)
class UnknownLine(Line):
def __init__(self, text):
self.text = text
def __unicode__(self):
return self.html_part("unknown", self.text)
class UserStatusLine(Line):
def __init__(self, lineno, time, nick, host, thing):
self.lineno = lineno
self.time = time
self.nick = nick
self.host = host
self.thing = thing
def __unicode__(self):
return u"{time} {nick} {host} {thing}".format(
time=self.html_time(),
nick=self.html_nick(),
host=self.html_part("host"),
thing=self.html_part("thing"))
def split_time(text):
(time, sep, text) = text.partition(" ")
assert sep == ' '
return (time, text)
def split_nick(text):
text = text[1:]
(nick, sep, message) = text.partition("> ")
assert sep == "> " and nick
assert " " not in nick
return (nick, message)
def split_action(text):
text = text[len("Action: "):]
(nick, sep, action) = text.partition(" ")
assert nick
return (nick, action)
def split_nickchange(text):
text = text[len("Nick change: "):]
(nick_before, nick_after) = text.split(" -> ")
assert " " not in nick_before and " " not in nick_after
assert nick_before and nick_after
return (nick_before, nick_after)
def check_dateline(text):
text = text[4:]
(day, month, dd, yyyy) = text.split()
day = list(calendar.day_abbr).index(day)
month = list(calendar.month_abbr).index(month)
dd = int(dd)
yyyy = int(yyyy)
assert datetime.date(yyyy, month, dd).weekday() == day
def split_userstatus(text):
(nick, sep, text) = text.partition(" ")
assert nick and sep == " "
(host, sep, thing) = text.partition(" ")
assert host and host.startswith("(") and host.endswith(")") and sep == " "
return (nick, host, thing)
def parse(lineno, text):
try:
(time, text) = split_time(text)
except:
return UnknownLine(text)
try:
if text.startswith("<"):
(nick, message) = split_nick(text)
return ChatLine(lineno, time, nick, message)
elif text.startswith("Action: "):
(nick, action) = split_action(text)
return ActionLine(lineno, time, nick, action)
elif text.startswith("Nick change: "):
(nick_before, nick_after) = split_nickchange(text)
return NickChangeLine(lineno, time, nick_before, nick_after)
elif text.find(" ") == text.find(" ("):
(nick, host, thing) = split_userstatus(text)
return UserStatusLine(lineno, time, nick, host, thing)
except:
pass
return UnknownTimeLine(lineno, time, text)
def try_unicode(text):
for enc in ["ascii", "utf8", "iso-8859-1", "windows-1252"]:
try:
return unicode(text, enc)
except:
pass
return unicode(text, "ascii", "ignore")
def parse_file(f):
for lineno, line in enumerate(f):
assert line.endswith("\n")
line = try_unicode(line[:-1])
yield unicode(parse(lineno, line))
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Usage: {0} <input> <output>\n".format(sys.argv[0])
sys.exit(1)
with open(config["template"]) as t:
template = jinja2.Template(t.read())
context = config.copy()
context["filename"] = os.path.basename(sys.argv[1])
with open(sys.argv[1], "r") as f_in:
context["lines"] = parse_file(f_in)
with open(sys.argv[2], "w") as f_out:
for part in template.generate(context):
f_out.write(part.encode("utf-8"))