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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
package main
import (
"bytes"
"fmt"
"mime"
"net/url"
"strings"
"tildegit.org/tjp/sliderule"
"tildegit.org/tjp/sliderule/gemini"
"tildegit.org/tjp/sliderule/gemini/gemtext"
"tildegit.org/tjp/sliderule/gopher"
"tildegit.org/tjp/sliderule/gopher/gophermap"
"tildegit.org/tjp/sliderule/spartan"
)
func docType(u *url.URL, response *sliderule.Response) string {
_, gopherType := gopherURL(u)
switch gopherType {
case gopher.MenuType, gopher.SearchType:
return "text/x-gophermap"
case gopher.TextFileType, gopher.ErrorType, gopher.InfoMessageType:
return "text/plain"
case gopher.MacBinHexType,
gopher.DosBinType,
gopher.BinaryFileType,
gopher.ImageFileType,
gopher.MovieFileType,
gopher.SoundFileType,
gopher.DocumentType:
return "application/octet-stream"
case gopher.UuencodedType:
return "text/x-uuencode"
case gopher.GifFileType:
return "image/gif"
case gopher.BitmapType:
return "image/bmp"
case gopher.PngImageFileType:
return "image/png"
case gopher.HTMLType:
return "text/html"
case gopher.RtfDocumentType:
return "application/rtf"
case gopher.WavSoundFileType:
return "audio/wav"
case gopher.PdfDocumentType:
return "application/pdf"
case gopher.XmlDocumentType:
return "application/xml"
}
if metaIsMediaType(u, response) {
mtype, _, err := mime.ParseMediaType(response.Meta.(string))
if err == nil {
return mtype
}
}
return "text/plain"
}
func metaIsMediaType(u *url.URL, response *sliderule.Response) bool {
if u.Scheme == "gemini" && response.Status == gemini.StatusSuccess {
return true
}
if u.Scheme == "spartan" && response.Status == spartan.StatusSuccess {
return true
}
return false
}
func parseDoc(doctype string, body []byte, conf *Config) (string, []Link, error) {
switch doctype {
case "text/x-gophermap":
return parseGophermapDoc(body, conf.SoftWrap)
case "text/gemini":
return parseGemtextDoc(body, conf.SoftWrap)
}
return string(body), nil, nil
}
func parseGophermapDoc(body []byte, softWrap int) (string, []Link, error) {
var b strings.Builder
var l []Link
mapdoc, err := gophermap.Parse(bytes.NewBuffer(body))
if err != nil {
return "", nil, err
}
links := 0
for _, item := range mapdoc {
if item.Type != gopher.InfoMessageType {
links += 1
}
}
width := numberWidth(links - 1)
infopad := string(bytes.Repeat([]byte{' '}, width + 3))
i := 0
for _, item := range mapdoc {
switch item.Type {
case gopher.InfoMessageType:
for _, line := range fold(item.Display, softWrap) {
if _, err := b.WriteString(infopad + line + "\n"); err != nil {
return "", nil, err
}
}
default:
l = append(l, Link{
Text: item.Display,
Target: fmtGopherURL(item.Type, item.Selector, item.Hostname, item.Port),
})
if _, err := b.WriteString(fmt.Sprintf("[%d]%s %s\n", i, padding(i, width), item.Display)); err != nil {
return "", nil, err
}
i += 1
}
}
return b.String(), l, nil
}
func parseGemtextDoc(body []byte, softWrap int) (string, []Link, error) {
var b strings.Builder
var l []Link
gemdoc, err := gemtext.Parse(bytes.NewBuffer(body))
if err != nil {
return "", nil, err
}
links := 0
for _, item := range gemdoc {
if item.Type() == gemtext.LineTypeLink {
links += 1
}
}
width := numberWidth(links - 1)
textpad := string(bytes.Repeat([]byte{' '}, width + 3))
i := 0
for _, item := range gemdoc {
isPrompt := false
switch item.Type() {
case gemtext.LineTypePrompt:
isPrompt = true
fallthrough
case gemtext.LineTypeLink:
ll := item.(gemtext.LinkLine)
u, err := url.Parse(ll.URL())
if err != nil {
return "", nil, err
}
l = append(l, Link{
Text: ll.Label(),
Target: u,
Prompt: isPrompt,
})
label := ll.Label()
if len(label) == 0 {
label = ll.URL()
}
if _, err := b.WriteString(fmt.Sprintf("[%d]%s %s\n", i, padding(i, width), label)); err != nil {
return "", nil, err
}
i += 1
default:
for _, line := range fold(item.String(), softWrap) {
if _, err := b.WriteString(textpad + line + "\n"); err != nil {
return "", nil, err
}
}
}
}
return b.String(), l, nil
}
func fold(line string, width int) []string {
rs := []rune(strings.TrimSuffix(line, "\n"))
if len(rs) == 0 {
return []string{""}
}
var b []string
outer:
for len(rs) > 0 {
if len(rs) <= width {
b = append(b, string(rs))
break
}
w := width
for rs[w] != ' ' && w > 0 {
w -= 1
}
if w == 0 {
for i := width + 1; i < len(rs); i += 1 {
if rs[i] == ' ' {
b = append(b, string(rs[:i]))
rs = rs[i+1:]
continue outer
}
}
b = append(b, string(rs))
break outer
}
b = append(b, string(rs[:w]))
rs = rs[w+1:]
}
return b
}
func fmtGopherURL(itemtype sliderule.Status, selector, hostname, port string) *url.URL {
if port != "70" {
hostname += ":" + port
}
return &url.URL{
Scheme: "gopher",
Host: hostname,
Path: "/" + string(byte(itemtype)) + selector,
}
}
func numberWidth(i int) int {
n := 1
for i > 9 {
i /= 10
n += 1
}
return n
}
func padding(num int, width int) string {
return string(bytes.Repeat([]byte{' '}, width - numberWidth(num)))
}
|