-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmi2html.go
More file actions
165 lines (144 loc) · 4.25 KB
/
gmi2html.go
File metadata and controls
165 lines (144 loc) · 4.25 KB
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
package gmi2html
import (
"bytes"
_ "embed"
"fmt"
"html/template"
"net/url"
"regexp"
"strings"
)
// Based on https://geminiprotocol.net/docs/gemtext-specification.gmi
//go:embed assets/main.html
var rawTemplate string
// Gmi2html converts Gemini text to HTML with proper escaping and wraps it in a container with typography-focused CSS
func Gmi2html(text string, title string, contentOnly bool, replaceGmiExt bool) (string, error) {
content := convertGeminiContent(text, replaceGmiExt)
if contentOnly {
return content, nil
}
tmpl := template.Must(template.New("gemini").Parse(rawTemplate))
var buffer bytes.Buffer
err := tmpl.Execute(&buffer, struct {
Title string
Content template.HTML
}{
Title: title,
Content: template.HTML(content), // Content already properly escaped in convertGeminiContent
})
if err != nil {
fmt.Printf("Error executing container template: %s\n", err)
return "", err
}
return buffer.String(), nil
}
// convertGeminiContent converts Gemini text to HTML with proper escaping
func convertGeminiContent(text string, replaceGmiExt bool) string {
lines := strings.Split(text, "\n")
var buffer bytes.Buffer
normalMode := true
for _, line := range lines {
if strings.HasPrefix(line, "```") {
if normalMode {
err := preformattedTmplStart.Execute(&buffer, line)
if err != nil {
return ""
}
buffer.WriteString("\n")
} else {
err := preformattedTmplEnd.Execute(&buffer, line)
if err != nil {
return ""
}
}
normalMode = !normalMode
// Don't output the ``` line itself
continue
}
if !normalMode {
// Inside preformatted block - output line directly with HTML escaping
buffer.WriteString(template.HTMLEscapeString(line))
buffer.WriteString("\n")
continue
}
// Normal mode - process gemini markup
switch {
case strings.HasPrefix(line, "=>"):
handleLinkLine(&buffer, line, replaceGmiExt)
case strings.HasPrefix(line, "###"):
content := strings.TrimSpace(strings.TrimPrefix(line, "###"))
err := h3Tmpl.Execute(&buffer, content)
if err != nil {
return ""
}
case strings.HasPrefix(line, "##"):
content := strings.TrimSpace(strings.TrimPrefix(line, "##"))
err := h2Tmpl.Execute(&buffer, content)
if err != nil {
return ""
}
case strings.HasPrefix(line, "#"):
content := strings.TrimSpace(strings.TrimPrefix(line, "#"))
err := h1Tmpl.Execute(&buffer, content)
if err != nil {
return ""
}
case strings.HasPrefix(line, "*"):
content := strings.TrimSpace(strings.TrimPrefix(line, "*"))
err := listItemTmpl.Execute(&buffer, content)
if err != nil {
return ""
}
case strings.HasPrefix(line, ">"):
content := strings.TrimSpace(strings.TrimPrefix(line, ">"))
err := blockquoteTmpl.Execute(&buffer, content)
if err != nil {
return ""
}
default:
err := textLineTmpl.Execute(&buffer, line)
if err != nil {
return ""
}
}
}
return buffer.String()
}
// handleLinkLine parses and renders a link line
func handleLinkLine(buffer *bytes.Buffer, linkLine string, replaceGmiExt bool) {
url, description, err := parseGeminiLink(linkLine, replaceGmiExt)
if err != nil {
fmt.Printf("Error parsing gemini link line: %s\n", err)
return
}
err = linkTmpl.Execute(buffer, struct {
URL, Description string
}{url, description})
if err != nil {
return
}
}
// parseGeminiLink extracts URL and description from a link line
func parseGeminiLink(linkLine string, replaceGmiExt bool) (string, string, error) {
re := regexp.MustCompile(`^=>[ \t]+(\S+)([ \t]+.*)?`)
matches := re.FindStringSubmatch(linkLine)
if len(matches) == 0 {
return "", "", fmt.Errorf("error parsing link line: no regexp match for line %s", linkLine)
}
urlStr := matches[1]
// Check: Unescape the URL if escaped
_, err := url.QueryUnescape(urlStr)
if err != nil {
return "", "", fmt.Errorf("error parsing link line: %w input '%s'", err, linkLine)
}
// Replace .gmi extension with .html if requested
if replaceGmiExt && strings.HasSuffix(urlStr, ".gmi") {
urlStr = strings.TrimSuffix(urlStr, ".gmi") + ".html"
}
// Set description to URL if not provided
description := urlStr
if len(matches) > 2 && strings.TrimSpace(matches[2]) != "" {
description = strings.TrimSpace(matches[2])
}
return urlStr, description, nil
}