...
1 package model
2
3 import (
4 "html"
5 "html/template"
6 "log"
7 "regexp"
8 "strings"
9 "time"
10 )
11
12 type Log struct {
13 ID int
14 Account *Account
15 Board *Board
16 Timestamp int64
17 Message string
18 Changes string
19 }
20
21 func (l *Log) TimestampDate() string {
22 return time.Unix(l.Timestamp, 0).Format("2006-01-02 15:04:05 MST")
23 }
24
25 func (l *Log) formatLabel(message string) template.HTML {
26 if len(message) == 0 {
27 return ""
28 }
29 message = html.EscapeString(message)
30
31 rgxp, err := regexp.Compile(`>>/([0-9A-Za-z_-]+)/([0-9]+)`)
32 if err != nil {
33 log.Fatal(err)
34 }
35 return template.HTML(rgxp.ReplaceAllStringFunc(message, func(s string) string {
36 if strings.HasPrefix(s, ">>/post/") {
37 return rgxp.ReplaceAllString(s, `<a href="/sriracha/$1/$2">>>$2</a>`)
38 }
39 return rgxp.ReplaceAllString(s, `<a href="/sriracha/$1/$2">$1 #$2</a>`)
40 }))
41 }
42
43 func (l *Log) MessageLabel() template.HTML {
44 return l.formatLabel(l.Message)
45 }
46
47 func (l *Log) InfoLabel() template.HTML {
48 return l.formatLabel(l.Changes)
49 }
50
View as plain text