-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.go
126 lines (113 loc) · 3.34 KB
/
conv.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
package tdmarkup
import (
"fmt"
"html"
"log"
"strings"
"time"
"github.com/tada-team/tdproto"
)
type Rules func(middle []rune, e tdproto.MarkupEntity) ([]rune, []rune, []rune)
const timeFormat = "02.01.06 15:04"
type ToPlainOpts struct {
DisableQuotes bool
Location *time.Location
}
func ToHtml(s string, e []tdproto.MarkupEntity) string {
return string(conv([]rune(s), e, func(middle []rune, e tdproto.MarkupEntity) ([]rune, []rune, []rune) {
switch e.Type {
case tdproto.Bold:
return []rune("<b>"), middle, []rune("</b>")
case tdproto.Italic:
return []rune("<i>"), middle, []rune("</i>")
case tdproto.Underscore:
return []rune("<u>"), middle, []rune("</u>")
case tdproto.Code:
return []rune("<code>"), middle, []rune("</code>")
case tdproto.Strike:
return []rune("<s>"), middle, []rune("</s>")
case tdproto.CodeBlock:
return []rune("<pre>"), middle, []rune("</pre>")
case tdproto.Quote:
return []rune("<blockquote>"), middle, []rune("</blockquote>\n")
case tdproto.Link:
return []rune(fmt.Sprintf(`<a href="%s">`, e.Url)), []rune(e.Repl), []rune("</a>")
case tdproto.Time:
return []rune("<time>"), []rune(mustTime(e.Time).Format(timeFormat)), []rune("</time>")
case tdproto.Unsafe:
return []rune(""), []rune(html.EscapeString(string(middle))), []rune("")
default:
return []rune(""), middle, []rune("")
}
}))
}
func ToPlain(s string, e []tdproto.MarkupEntity, opts *ToPlainOpts) string {
if opts == nil {
opts = new(ToPlainOpts)
}
row := string(conv([]rune(s), e, func(middle []rune, e tdproto.MarkupEntity) ([]rune, []rune, []rune) {
switch e.Type {
case tdproto.Quote:
if opts.DisableQuotes {
return []rune(""), []rune(""), []rune("")
}
return []rune("> "), middle, []rune("\n")
case tdproto.Link:
//for _, prefix := range [...]string{
// "tadateam://",
// "tel:",
// "mailto:",
//} {
// if strings.HasPrefix(e.Url, prefix) {
// return []rune(""), []rune(e.Repl), []rune("")
// }
//}
//return []rune(""), []rune(e.Url), []rune("")
return []rune(""), []rune(e.Repl), []rune("")
case tdproto.Time:
t := mustTime(e.Time)
if loc := opts.Location; loc != nil {
t = t.In(loc)
}
return []rune(""), []rune(t.Format(timeFormat)), []rune("")
default:
return []rune(""), middle, []rune("")
}
}))
if !opts.DisableQuotes {
return strings.TrimRight(row, "\n\r")
}
return row
}
func conv(runes []rune, entities []tdproto.MarkupEntity, rules Rules) []rune {
offset := 0
for _, e := range entities {
middleDiff := 0
middle := runes[e.Open+e.OpenLength+offset : e.Close+offset]
op, rulesMiddle, cl := rules(middle, e)
if len(e.Childs) > 0 {
entitiesMiddle := conv(middle, e.Childs, rules)
middleDiff = len(entitiesMiddle) - len(middle)
middle = entitiesMiddle
} else {
middleDiff = len(rulesMiddle) - len(middle)
middle = rulesMiddle
}
res := make([]rune, 0, len(runes))
res = append(res, runes[:e.Open+offset]...)
res = append(res, op...)
res = append(res, middle...)
res = append(res, cl...)
res = append(res, runes[e.Close+e.CloseLength+offset:]...)
runes = res
offset += len(op) + len(cl) - e.OpenLength - e.CloseLength + middleDiff
}
return runes
}
func mustTime(s string) time.Time {
dt, err := time.Parse("2006-01-02T15:04:05Z0700", s)
if err != nil {
log.Panicln("invalid date:", s, err)
}
return dt
}