gmi2html.go (view raw)
1package main
2
3import (
4 "fmt"
5 "html"
6 "net/url"
7 "strings"
8
9 "git.sr.ht/~adnano/go-gemini"
10)
11
12func textToHTML(text gemini.Text) string {
13 var b strings.Builder
14 var pre bool
15 var list bool
16 for _, l := range text {
17 if _, ok := l.(gemini.LineListItem); ok {
18 if !list {
19 list = true
20 fmt.Fprint(&b, "<ul>\n")
21 }
22 } else if list {
23 list = false
24 fmt.Fprint(&b, "</ul>\n")
25 }
26 switch l.(type) {
27 case gemini.LineLink:
28 link := l.(gemini.LineLink)
29 urlstring := html.EscapeString(link.URL)
30 // u = ctx.URL.ResolveReference(u) ?
31 u, err := url.Parse(urlstring)
32 if err != nil {
33 continue
34 }
35 if u.Scheme == "gemini" {
36 u.Path = fmt.Sprintf("/%s%s", u.Host, u.Path)
37 u.Scheme = ""
38 u.Host = "proxy." + c.Host
39 urlstring = html.EscapeString(u.String())
40 }
41 name := html.EscapeString(link.Name)
42 if name == "" {
43 name = urlstring
44 }
45 fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", urlstring, name)
46 case gemini.LinePreformattingToggle:
47 pre = !pre
48 if pre {
49 altText := string(l.(gemini.LinePreformattingToggle))
50 if altText != "" {
51 altText = html.EscapeString(altText)
52 fmt.Fprintf(&b, "<pre title='%s'>\n", altText)
53 } else {
54 fmt.Fprint(&b, "<pre>\n")
55 }
56 } else {
57 fmt.Fprint(&b, "</pre>\n")
58 }
59 case gemini.LinePreformattedText:
60 text := string(l.(gemini.LinePreformattedText))
61 fmt.Fprintf(&b, "%s\n", html.EscapeString(text))
62 case gemini.LineHeading1:
63 text := string(l.(gemini.LineHeading1))
64 fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text))
65 case gemini.LineHeading2:
66 text := string(l.(gemini.LineHeading2))
67 fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text))
68 case gemini.LineHeading3:
69 text := string(l.(gemini.LineHeading3))
70 fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(text))
71 case gemini.LineListItem:
72 text := string(l.(gemini.LineListItem))
73 fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text))
74 case gemini.LineQuote:
75 text := string(l.(gemini.LineQuote))
76 fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(text))
77 case gemini.LineText:
78 text := string(l.(gemini.LineText))
79 if text == "" {
80 fmt.Fprint(&b, "<br>\n")
81 } else {
82 fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text))
83 }
84 }
85 }
86 if pre {
87 fmt.Fprint(&b, "</pre>\n")
88 }
89 if list {
90 fmt.Fprint(&b, "</ul>\n")
91 }
92 return b.String()
93}
94
95// Convert a Gemini link to a proxy link
96func proxyLink() {
97}