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
| package main
import (
"fmt"
"html"
"net/url"
"path/filepath"
"strings"
"mime"
"git.sr.ht/~adnano/go-gemini"
)
type ConvertedGmiDoc struct {
Content string
Title string
}
func textToHTML(reqUrl *url.URL, text gemini.Text) ConvertedGmiDoc {
var b strings.Builder
var pre bool
var blockquote bool
var list bool
var title string
for _, l := range text {
if _, ok := l.(gemini.LineQuote); ok {
if !blockquote {
blockquote = true
fmt.Fprintf(&b, "<blockquote>\n")
}
} else if blockquote {
blockquote = false
fmt.Fprintf(&b, "</blockquote>\n")
}
if _, ok := l.(gemini.LineListItem); ok {
if !list {
list = true
fmt.Fprint(&b, "<ul>\n")
}
} else if list {
list = false
fmt.Fprint(&b, "</ul>\n")
}
switch l.(type) {
case gemini.LineLink:
link := l.(gemini.LineLink)
urlstring := html.EscapeString(link.URL)
// u = ctx.URL.ResolveReference(u) ?
u, err := url.Parse(urlstring)
if err != nil {
continue
}
if reqUrl != nil {
u = reqUrl.ResolveReference(u)
}
if u.Scheme == "gemini" || (reqUrl != nil && u.Scheme == "") {
if strings.HasSuffix(u.Host, c.Host) {
u.Scheme = ""
urlstring = html.EscapeString(u.String())
} else {
// TODO make configurable
u.Path = fmt.Sprintf("gemini/%s%s", u.Host, u.Path)
u.Scheme = ""
u.Host = "portal.mozz.us"
urlstring = html.EscapeString(u.String())
}
}
name := html.EscapeString(link.Name)
if name == "" {
name = urlstring
}
// embed images
if strings.HasPrefix(mime.TypeByExtension(filepath.Ext(u.Path)), "image") {
fmt.Fprintf(&b, "<p><img src='%s' alt='%s'></p>\n", urlstring, name)
} else {
fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", urlstring, name)
}
case gemini.LinePreformattingToggle:
pre = !pre
if pre {
altText := string(l.(gemini.LinePreformattingToggle))
if altText != "" {
altText = html.EscapeString(altText)
fmt.Fprintf(&b, "<pre title='%s'>\n", altText)
} else {
fmt.Fprint(&b, "<pre>\n")
}
} else {
fmt.Fprint(&b, "</pre>\n")
}
case gemini.LinePreformattedText:
text := string(l.(gemini.LinePreformattedText))
fmt.Fprintf(&b, "%s\n", html.EscapeString(text))
case gemini.LineHeading1:
text := html.EscapeString(string(l.(gemini.LineHeading1)))
fmt.Fprintf(&b, "<h1 id='%s'>%s</h1>\n", text, text)
if title == "" {
title = text
} // TODO deal with repetition
case gemini.LineHeading2:
text := html.EscapeString(string(l.(gemini.LineHeading2)))
fmt.Fprintf(&b, "<h2 id='%s'>%s</h2>\n", text, text)
if title == "" {
title = text
}
case gemini.LineHeading3:
text := html.EscapeString(string(l.(gemini.LineHeading3)))
fmt.Fprintf(&b, "<h3 id='%s'>%s</h3>\n", text, text)
if title == "" {
title = text
}
case gemini.LineListItem:
text := string(l.(gemini.LineListItem))
fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text))
case gemini.LineQuote:
text := string(l.(gemini.LineQuote))
fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text))
case gemini.LineText:
text := string(l.(gemini.LineText))
if text == "" {
fmt.Fprint(&b, "<br>\n")
} else {
fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text))
}
}
}
if pre {
fmt.Fprint(&b, "</pre>\n")
}
if list {
fmt.Fprint(&b, "</ul>\n")
}
if blockquote {
fmt.Fprint(&b, "</blockquote>\n")
}
return ConvertedGmiDoc{
b.String(),
title,
}
}
|