This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
/
to_html.go
299 lines (250 loc) · 7.12 KB
/
to_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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package gem
import (
"html"
"regexp"
"strconv"
"strings"
"github.com/bossley9/gem/pkg/url"
)
type lineType int
const (
lineParagraph lineType = iota
lineWhitespace
lineLink
linePreformattedEdge
lineHeadingOne
lineHeadingTwo
lineHeadingThree
lineUnorderedListItem
lineBlockQuote
)
// converts a given string of Gemtext to basic HTML.
func ToHTML(gemtext string) string {
if len(gemtext) == 0 {
return ""
}
idRefs := make(map[string]int, 0)
const (
stateDefault = iota
stateParagraph
statePreformatted
stateUnorderedList
stateBlockquote
)
var output strings.Builder
var state = stateDefault
lines := strings.Split(gemtext, "\n")
for i, line := range lines {
lineType := getLineType(line)
if state == statePreformatted {
if lineType == linePreformattedEdge {
// closing preformatted line
output.WriteString(convertPreformattedClosing(line))
state = stateDefault
} else {
// preformatted line
output.WriteString(html.EscapeString(line) + "\n")
}
continue
}
switch lineType {
case lineLink:
output.WriteString(convertLink(line))
case linePreformattedEdge:
// opening preformatted line
state = statePreformatted
output.WriteString(convertPreformattedOpening(line))
case lineHeadingThree:
fallthrough
case lineHeadingTwo:
fallthrough
case lineHeadingOne:
id := url.GenerateID(line)
idCount, ok := idRefs[id]
if ok {
idRefs[id] = idCount + 1
id = id + "-" + strconv.Itoa(idCount)
} else {
idRefs[id] = 1
}
output.WriteString(convertHeading(line, id))
case lineUnorderedListItem:
if state != stateUnorderedList {
output.WriteString("<ul>")
state = stateUnorderedList
}
output.WriteString(convertUnorderedListItem(line))
if nextIsNotType(lines, i, lineUnorderedListItem) {
output.WriteString("</ul>")
state = stateDefault
}
case lineBlockQuote:
// opening blockquote
if state != stateBlockquote {
output.WriteString("<blockquote><p>")
state = stateBlockquote
}
quote := convertBlockquote(line)
output.WriteString(quote)
// closing blockquote
if nextIsNotType(lines, i, lineBlockQuote) {
output.WriteString("</p></blockquote>")
state = stateDefault
} else {
// between blockquote lines
if len(quote) > 0 {
output.WriteString(convertBlockquoteFiller(lines[i+1]))
}
}
case lineWhitespace:
if state == stateParagraph {
output.WriteString("<br />")
}
case lineParagraph:
fallthrough
default:
// opening paragraph
if state != stateParagraph {
output.WriteString("<p>")
state = stateParagraph
}
output.WriteString(convertText(line))
// closing paragraph
if nextIsNotType(lines, i, lineParagraph) {
output.WriteString("</p>")
state = stateDefault
} else {
output.WriteString("<br />")
}
}
}
return output.String()
}
// given a line of text, returns the Gemtext line type
func getLineType(line string) lineType {
var lineType lineType
if len(line) == 0 {
lineType = lineWhitespace
} else if strings.HasPrefix(line, "=>") {
lineType = lineLink
} else if strings.HasPrefix(line, "```") {
lineType = linePreformattedEdge
} else if strings.HasPrefix(line, "###") {
lineType = lineHeadingThree
} else if strings.HasPrefix(line, "##") {
lineType = lineHeadingTwo
} else if strings.HasPrefix(line, "#") {
lineType = lineHeadingOne
} else if strings.HasPrefix(line, "* ") {
lineType = lineUnorderedListItem
} else if strings.HasPrefix(line, ">") {
lineType = lineBlockQuote
} else {
lineType = lineParagraph
}
return lineType
}
// converts a given text string to HTML
func convertText(text string) string {
return html.EscapeString(text)
}
// converts a given Gemtext link to HTML
func convertLink(line string) string {
output := "<p>"
link := strings.TrimSpace(strings.TrimPrefix(line, "=>"))
split := strings.SplitN(link, " ", 2)
url := ""
name := ""
if len(split) >= 1 {
url = split[0]
}
if len(split) >= 2 {
name = strings.TrimSpace(split[1])
}
regexImg := regexp.MustCompile(`.*\.(avif|bmp|gif|jpg|jpeg|png|svg|webp|xpm)`)
regexAudio := regexp.MustCompile(`.*\.(m3u|m4a|mp3|ogg|wav)`)
regexVideo := regexp.MustCompile(`.*\.(avi|divx|m4v|mkv|mov|mp4|mpeg|mpg|vob|webm|wmv)`)
if regexImg.MatchString(url) {
output = output + `<img src="` + html.EscapeString(url) + `" alt="`
if len(name) > 0 {
output = output + html.EscapeString(name)
}
output = output + `" />`
} else if regexAudio.MatchString(url) {
ext := url[strings.LastIndex(url, ".")+1:]
output = output + "<audio controls>"
output = output + `<source src="` + url + `" type="audio/` + ext + `" />`
output = output + "Sorry, your browser doesn't support embedded audio."
output = output + "</audio>"
} else if regexVideo.MatchString(url) {
ext := url[strings.LastIndex(url, ".")+1:]
output = output + "<video controls>"
output = output + `<source src="` + url + `" type="video/` + ext + `" />`
output = output + "Sorry, your browser doesn't support embedded video."
output = output + "</video>"
} else {
output = output + `<a href="` + html.EscapeString(url) + `">`
if len(name) > 0 {
output = output + convertText(name)
} else {
output = output + convertText(url)
}
output = output + `</a>`
}
output = output + "</p>"
return output
}
// converts a given opening preformatted Gemtext line to HTML
func convertPreformattedOpening(line string) string {
output := "<figure>"
alt := strings.TrimSpace(strings.TrimPrefix(line, "```"))
if len(alt) > 0 {
output = output + "<figcaption>" + convertText(alt) + "</figcaption>"
}
output = output + "<pre><code>"
return output
}
func convertPreformattedClosing(line string) string {
return "</code></pre></figure>"
}
// converts a given Gemtext heading to HTML
func convertHeading(line string, id string) string {
lineType := getLineType(line)
headingNum := ""
headingPrefix := ""
if lineType == lineHeadingThree {
headingNum = "3"
headingPrefix = "###"
} else if lineType == lineHeadingTwo {
headingNum = "2"
headingPrefix = "##"
} else {
headingNum = "1"
headingPrefix = "#"
}
headingText := strings.TrimSpace(strings.TrimPrefix(line, headingPrefix))
return "<h" + headingNum + ` id="` + id + `"><a href="#` + id + `">` + convertText(headingText) + "</a></h" + headingNum + ">"
}
// converts a given Gemtext unordered list to HTML
func convertUnorderedListItem(line string) string {
listitem := strings.TrimSpace(strings.TrimPrefix(line, "*"))
return "<li>" + convertText(listitem) + "</li>"
}
// given an array of strings, the current index, and a line type, returns true if
// the next line is not that type
func nextIsNotType(lines []string, index int, lineType lineType) bool {
return len(lines) == index+1 || getLineType(lines[index+1]) != lineType
}
// converts a given Gemtext quote line to HTML
func convertBlockquote(line string) string {
text := strings.TrimSpace(strings.TrimPrefix(line, ">"))
return convertText(text)
}
// converts a given Gemtext quote lookahead line to HTML
func convertBlockquoteFiller(line string) string {
if len(convertBlockquote(line)) == 0 {
return "</p><p>"
} else {
return "<br />"
}
}