-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.go
172 lines (148 loc) · 3.25 KB
/
html.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
169
170
171
172
package sisyphus
import (
"fmt"
"html"
"net/url"
"path/filepath"
"regexp"
"blekksprut.net/aspeq"
)
func Safe(raw string) string {
return html.EscapeString(raw)
}
type Html struct {
Self string
State State
LinkHooks map[string]LinkHook
QuoteHook QuoteHook
OpenHook Hook
CloseHook Hook
Greentext bool
}
var tags = map[State][2]string{
None: {"", ""},
Text: {"<p>", ""},
List: {"<ul>\n", "</ul>\n"},
Pre: {"<pre>", "</pre>\n"},
Quote: {"<blockquote>\n<p>", "</blockquote>\n"},
}
func (html *Html) OnLink(rule string, hook LinkHook) {
if html.LinkHooks == nil {
html.LinkHooks = make(map[string]LinkHook)
}
html.LinkHooks[rule] = hook
}
func (html *Html) OnQuote(hook QuoteHook) {
html.QuoteHook = hook
}
func (html *Html) OnOpen(hook Hook) {
html.OpenHook = hook
}
func (html *Html) OnClose(hook Hook) {
html.CloseHook = hook
}
func (html *Html) Wrap(tag string) {
if tag == "" {
return
}
html.OnOpen(func() string {
return fmt.Sprintf("<%s>\n", tag)
})
html.OnClose(func() string {
return fmt.Sprintf("</%s>\n", tag)
})
}
func (html *Html) Open() string {
if html.OpenHook != nil {
return html.OpenHook()
}
return ""
}
func (html *Html) Close() string {
if html.CloseHook != nil {
return html.CloseHook()
}
return ""
}
func (html *Html) Header(level int, text string) string {
return fmt.Sprintf("<h%d>%s</h%d>\n", level, Safe(text), level)
}
func (html *Html) Link(url string, text string) string {
if html.Greentext {
text = "=> " + text
}
text, url = Safe(text), Safe(url)
ext := filepath.Ext(url)
hook, ok := html.LinkHooks[ext]
if ok {
return hook(url, text, ext)
}
re := regexp.MustCompile(`\((.+)\)$`)
tag := re.FindStringSubmatch(text)
if len(tag) > 1 {
hook, ok := html.LinkHooks[tag[1]]
if ok {
return hook(url, text, ext)
}
}
attrs := "data-friendly "
if text == "" {
text = url
attrs = ""
}
if html.Self == url {
return fmt.Sprintf("<a %sclass=self href='%s'>%s</a>", attrs, url, text)
} else {
return fmt.Sprintf("<a %shref='%s'>%s</a>", attrs, url, text)
}
}
func (html *Html) ListItem(text string) string {
return fmt.Sprintf("<li>%s\n", Safe(text))
}
func (html *Html) Pre(text string) string {
return Safe(text)
}
func (html *Html) Quote(text string) string {
if html.QuoteHook != nil {
return html.QuoteHook(Safe(text))
}
if html.Greentext {
return html.Text("> " + text)
}
return html.Text(text)
}
func (html *Html) Text(text string) string {
return Safe(text)
}
func (html *Html) GetState() State {
return html.State
}
func (html *Html) SetState(state State) string {
if state == html.State {
if state == Text || state == Quote {
return "<br>"
}
return ""
}
closing := tags[html.State][1]
opening := tags[state][0]
html.State = state
return closing + opening
}
func (html *Html) Aspeq(prefix string, useBase bool) LinkHook {
return func(uri, text, suffix string) string {
parsed, err := url.Parse(uri)
format := "<img src='%s' class=%s alt='%s'>"
if err == nil && !parsed.IsAbs() {
if useBase {
uri = filepath.Base(uri)
}
path := fmt.Sprintf("%s/%s", prefix, uri)
ar, err := aspeq.FromImage(path)
if err == nil {
return fmt.Sprintf(format, uri, ar.Name, text)
}
}
return fmt.Sprintf(format, uri, "unknown", text)
}
}