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
|
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"
)
func docType(u *url.URL, response *sliderule.Response) string {
_, gopherType := gopherURL(u)
switch gopherType {
case gopher.MenuType:
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 u.Scheme == "gemini" {
if response.Status == gemini.StatusSuccess {
mtype, _, err := mime.ParseMediaType(response.Meta.(string))
if err == nil {
return mtype
}
}
}
return "text/plain"
}
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
}
i := 0
for _, item := range mapdoc {
switch item.Type {
case gopher.InfoMessageType:
for _, line := range fold(item.Display, softWrap) {
if _, err := b.WriteString(" " + 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, linkSpaces(i), 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
}
i := 0
for _, item := range gemdoc {
switch item.Type() {
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,
})
if _, err := b.WriteString(fmt.Sprintf("[%d]%s %s\n", i, linkSpaces(i), ll.Label())); err != nil {
return "", nil, err
}
i += 1
default:
for _, line := range fold(item.String(), softWrap) {
if _, err := b.WriteString(" " + 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 linkSpaces(i int) string {
switch {
case i < 10:
return " "
case i < 100:
return " "
default:
return ""
}
}
|