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
|
// Package gemtext implements shared logic related to gemtext files.
package gemtext
import (
"bufio"
"bytes"
"errors"
"fmt"
"html"
"io"
"strings"
)
// HTMLTranslator is used to translate a gemtext file into equivalent HTML DOM
// elements.
type HTMLTranslator struct {
// RenderHeading, if given can be used to override how headings are
// rendered. The level indicates which heading level is being rendered: 1,
// 2, or 3.
RenderHeading func(w io.Writer, level int, text string) error
// RenderLink, if given, can be used to override how links are rendered.
RenderLink func(w io.Writer, url, label string) error
}
// HTML contains the result of a translation from gemtext. The Body will be the
// translated body itself, and Title will correspond to the first primary header
// of the gemtext file, if there was one.
type HTML struct {
Title string
Body string
}
// Translate will read a gemtext file from the Reader and return it as an HTML
// document.
func (t HTMLTranslator) Translate(src io.Reader) (HTML, error) {
var (
r = bufio.NewReader(src)
w = new(bytes.Buffer)
title string
pft, list bool
writeErr error
)
sanitizeText := func(str string) string {
return html.EscapeString(strings.TrimSpace(str))
}
write := func(fmtStr string, args ...any) {
if writeErr != nil {
return
}
_, writeErr = fmt.Fprintf(w, fmtStr, args...)
}
loop:
for {
if writeErr != nil {
return HTML{}, fmt.Errorf("writing line: %w", writeErr)
}
line, err := r.ReadString('\n')
switch {
case errors.Is(err, io.EOF):
break loop
case err != nil:
return HTML{}, fmt.Errorf("reading next line: %w", err)
case strings.HasPrefix(line, "```"):
if !pft {
write("<pre>\n")
pft = true
} else {
write("</pre>\n")
pft = false
}
continue
case pft:
write(html.EscapeString(line))
continue
case len(strings.TrimSpace(line)) == 0:
continue
}
// list case is special, because it requires a prefix and suffix tag
if strings.HasPrefix(line, "*") {
if !list {
write("<ul>\n")
}
write("<li>%s</li>\n", sanitizeText(line[1:]))
list = true
continue
} else if list {
write("</ul>\n")
list = false
}
switch {
case strings.HasPrefix(line, "=>"):
var (
line = strings.TrimSpace(line[2:])
urlStr = line
label = urlStr
)
if i := strings.IndexAny(urlStr, " \t"); i > -1 {
urlStr, label = urlStr[:i], sanitizeText(urlStr[i:])
}
if t.RenderLink == nil {
write("<p><a href=\"%s\">%s</a></p>\n", urlStr, label)
} else {
writeErr = t.RenderLink(w, urlStr, label)
}
case strings.HasPrefix(line, "###"):
text := sanitizeText(line[3:])
if t.RenderHeading == nil {
write("<h3>%s</h3>\n", text)
} else {
writeErr = t.RenderHeading(w, 3, text)
}
case strings.HasPrefix(line, "##"):
text := sanitizeText(line[2:])
if t.RenderHeading == nil {
write("<h2>%s</h2>\n", text)
} else {
writeErr = t.RenderHeading(w, 2, text)
}
case strings.HasPrefix(line, "#"):
text := sanitizeText(line[1:])
if title == "" {
title = text
}
if t.RenderHeading == nil {
write("<h1>%s</h1>\n", text)
} else {
writeErr = t.RenderHeading(w, 1, text)
}
case strings.HasPrefix(line, ">"):
write("<blockquote>%s</blockquote>\n", sanitizeText(line[1:]))
default:
line = strings.TrimSpace(line)
write("<p>%s</p>\n", line)
}
}
return HTML{
Title: title,
Body: w.String(),
}, nil
}
|