-
Notifications
You must be signed in to change notification settings - Fork 6
/
markdown.go
127 lines (115 loc) · 3.61 KB
/
markdown.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
package main
import (
"fmt"
"regexp"
"strings"
"time"
)
type MarkdownTranslator struct {
}
func NewMarkdownTranslator() *MarkdownTranslator {
r := new(MarkdownTranslator)
return r
}
func (t *MarkdownTranslator) FileNameIndex() string {
return "index.md"
}
func (t *MarkdownTranslator) FileNameChannel(channelName string) string {
return fmt.Sprintf("channel--%v.md", channelName)
}
func (t *MarkdownTranslator) FileNameHistory(channelName string, chunkNumber int) string {
return fmt.Sprintf("history--%v--%v.md", channelName, chunkNumber)
}
var mdSpecialCharsRegexp = regexp.MustCompile(`[\\\[\]#*!<>` + "`" + `|]`)
var mdSpecialCharsReplacer = func(matched string) string {
return "\\" + matched
}
func (t *MarkdownTranslator) Escape(text string) string {
result := text
result = mdSpecialCharsRegexp.ReplaceAllStringFunc(result, mdSpecialCharsReplacer)
// TODO needs more for escaping
return result
}
func (t *MarkdownTranslator) ToHeading(level int, text string) []string {
return []string{
strings.Repeat("#", level) + " " + t.Escape(text),
"",
}
}
func (t *MarkdownTranslator) ToChannelList(channels []Channel) []string {
result := make([]string, 0, len(channels)+1)
for _, ch := range channels {
text := "#" + ch.Name
link := t.FileNameChannel(ch.Name)
result = append(result, fmt.Sprintf("* [%v](%v)", t.Escape(text), link))
}
result = append(result, "")
return result
}
func (t *MarkdownTranslator) ToChunkList(chunks []ChunkInfo) []string {
result := make([]string, 0, len(chunks)+1)
for i, ch := range chunks {
text := fmt.Sprintf("%v (%v - %v)", i+1, toTimeStampString(ch.Start), toTimeStampString(ch.End))
link := t.FileNameHistory(ch.ChannelName, i+1)
result = append(result, fmt.Sprintf("* [%v](%v)", t.Escape(text), link))
}
result = append(result, "")
return result
}
func (t *MarkdownTranslator) ToMessageList(chunk []MessageResolved) []string {
result := make([]string, 0, len(chunk)+1)
for _, m := range chunk {
md := make([]string, 0, 1+len(m.MessageTokens))
header := toTimeStampString(m.Ts)
if nil != m.User {
header += " @" + m.User.Name
} else if "" != m.BotID {
header += " (APP)" + m.BotID
} else {
// `subtype=file_comment` does not have `user` or `bot_id`
}
header += ": "
md = append(md, "* "+header)
if "" != m.Subtype {
md = append(md, "*")
}
for _, token := range m.MessageTokens {
switch token := token.(type) {
case MessageTokenNewLine:
md = append(md, "\n ")
case MessageTokenText:
md = append(md, token.Text)
case MessageTokenLink:
md = append(md, fmt.Sprintf("[%v](%v)", t.Escape(token.Text), token.Href))
case MessageTokenChannel:
md = append(md, "#"+token.Text)
case MessageTokenUser:
md = append(md, "@"+token.Text)
case MessageTokenVariable:
md = append(md, "@"+token.Text)
default:
panic("Unknown message type")
}
}
if "" != m.Subtype {
md = append(md, "*")
}
result = append(result, strings.Join(md, ""))
}
result = append(result, "")
return result
}
func (t *MarkdownTranslator) ToUserTable(users []User) []string {
result := make([]string, 0, len(users)+2+1)
result = append(result, "|ID|Icon|Name|Email|FirstName|LastName|Title|")
result = append(result, strings.Repeat("|----", 7)+"|")
for _, u := range users {
vals := []string{u.ID, "![](" + u.Profile.Image24 + ")", u.Name, u.Profile.Email, t.Escape(u.Profile.FirstName), t.Escape(u.Profile.LastName), t.Escape(u.Profile.Title)}
result = append(result, "|"+strings.Join(vals, "|")+"|")
}
result = append(result, "")
return result
}
func toTimeStampString(ts time.Time) string {
return ts.UTC().Format(time.RFC3339)
}