-
Notifications
You must be signed in to change notification settings - Fork 0
/
parakeet.go
168 lines (138 loc) · 3.44 KB
/
parakeet.go
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
package main
import (
_ "embed"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"math/rand"
"mvdan.cc/xurls/v2"
"os"
"path/filepath"
"strings"
"time"
)
//go:embed "channel.gohtml"
var tplStr string
// Channel represent an IRC channel
type Channel struct {
Name string
Messages []Message
}
// Message represent an IRC message
type Message struct {
Time time.Time
Sender string
Content string
}
// Context is the application context
type Context struct {
Colors []string
Users map[string]string
}
func main() {
inputFileFlag := flag.String("input", "", "IRC log input file")
outputFileFlag := flag.String("output", "", "Where the HTML should be outputted")
flag.Parse()
if *inputFileFlag == "" {
log.Fatalf("missing -input")
}
channelName := trimSuffixes(filepath.Base(*inputFileFlag), []string{".txt", ".log"})
if *outputFileFlag == "" {
*outputFileFlag = channelName + ".html"
}
inputFile, err := os.Open(*inputFileFlag)
if err != nil {
log.Fatal(err)
}
defer inputFile.Close()
ch, err := parseLog(channelName, inputFile)
if err != nil {
panic(err)
}
log.Printf("Successfully parsed %d messages from %s", len(ch.Messages), ch.Name)
outputFile, err := os.Create(*outputFileFlag)
if err != nil {
panic(err)
}
defer outputFile.Close()
if err := generateHTML(ch, outputFile); err != nil {
panic(err)
}
log.Printf("Sucessfully generated HTML template at: %s", *outputFileFlag)
}
func generateHTML(ch *Channel, writer io.Writer) error {
ctx := Context{
Colors: []string{
"red", "green", "blue", "violet", "turquoise",
"coral", "brown", "crimson", "darkblue",
"fuschia", "indigo", "maroon", "navy",
},
Users: map[string]string{},
}
tpl, err := template.New("channel").
Funcs(map[string]interface{}{
"colorUsername": ctx.colorUsername,
}).
Parse(tplStr)
if err != nil {
return err
}
return tpl.Execute(writer, ch)
}
func parseLog(name string, reader io.Reader) (*Channel, error) {
ch := &Channel{
Name: name,
Messages: []Message{},
}
b, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
// Replace all URLs in one pass
content := applyURLs(string(b))
for _, line := range strings.Split(content, "\n") {
// Only keep 'message' line i.e which contains something like '] <username>'
if !strings.Contains(line, "] <") || !strings.Contains(line, ">") {
continue
}
// Approximate line parsing
date := line[1:strings.Index(line, "] <")]
username := line[strings.Index(line, "<")+1 : strings.Index(line, ">")]
content := line[strings.Index(line, "> ")+2:]
t, err := time.Parse(time.RFC3339, date)
if err != nil {
break
}
ch.Messages = append(ch.Messages, Message{
Time: t,
Sender: username,
Content: content,
})
}
return ch, nil
}
func trimSuffixes(s string, suffixes []string) string {
for _, suffix := range suffixes {
s = strings.TrimSuffix(s, suffix)
}
return s
}
func applyURLs(s string) string {
rxStrict := xurls.Strict()
return rxStrict.ReplaceAllStringFunc(s, func(s string) string {
return fmt.Sprintf("<a href=\"%s\">%s</a>", s, s)
})
}
func (c *Context) colorUsername(s string) template.HTML {
// check if username color is not yet applied
color, exist := c.Users[s]
if !exist {
// pick-up new random color for the username
color = c.Colors[rand.Intn(len(c.Colors)-1)]
c.Users[s] = color
}
return template.HTML(fmt.Sprintf("<<span style=\"color: %s; font-weight: bold;\">%s</span>>", color, s))
}