2020-10-26 10:49:16 -06:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// This example illustrates a gemtext to HTML converter.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.sr.ht/~adnano/go-gemini"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
text := gemini.Text{
|
|
|
|
gemini.LineHeading1("Hello, world!"),
|
|
|
|
gemini.LineText("This is a gemini text document."),
|
|
|
|
}
|
|
|
|
|
|
|
|
html := textToHTML(text)
|
|
|
|
fmt.Print(html)
|
|
|
|
}
|
|
|
|
|
|
|
|
// textToHTML returns the Gemini text response as HTML.
|
|
|
|
func textToHTML(text gemini.Text) string {
|
|
|
|
var b strings.Builder
|
|
|
|
var pre bool
|
|
|
|
var list bool
|
|
|
|
for _, l := range text {
|
|
|
|
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")
|
|
|
|
}
|
2020-10-31 22:58:34 -06:00
|
|
|
switch l := l.(type) {
|
2020-10-26 10:49:16 -06:00
|
|
|
case gemini.LineLink:
|
2020-10-31 22:58:34 -06:00
|
|
|
url := html.EscapeString(l.URL)
|
|
|
|
name := html.EscapeString(l.Name)
|
2020-10-26 10:49:16 -06:00
|
|
|
if name == "" {
|
|
|
|
name = url
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name)
|
|
|
|
case gemini.LinePreformattingToggle:
|
|
|
|
pre = !pre
|
|
|
|
if pre {
|
|
|
|
fmt.Fprint(&b, "<pre>\n")
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(&b, "</pre>\n")
|
|
|
|
}
|
|
|
|
case gemini.LinePreformattedText:
|
2020-10-31 22:58:34 -06:00
|
|
|
fmt.Fprintf(&b, "%s\n", html.EscapeString(string(l)))
|
2020-10-26 10:49:16 -06:00
|
|
|
case gemini.LineHeading1:
|
2020-10-31 22:58:34 -06:00
|
|
|
fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(string(l)))
|
2020-10-26 10:49:16 -06:00
|
|
|
case gemini.LineHeading2:
|
2020-10-31 22:58:34 -06:00
|
|
|
fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(string(l)))
|
2020-10-26 10:49:16 -06:00
|
|
|
case gemini.LineHeading3:
|
2020-10-31 22:58:34 -06:00
|
|
|
fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(string(l)))
|
2020-10-26 10:49:16 -06:00
|
|
|
case gemini.LineListItem:
|
2020-10-31 22:58:34 -06:00
|
|
|
fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(string(l)))
|
2020-10-26 10:49:16 -06:00
|
|
|
case gemini.LineQuote:
|
2020-10-31 22:58:34 -06:00
|
|
|
fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(string(l)))
|
2020-10-26 10:49:16 -06:00
|
|
|
case gemini.LineText:
|
2020-10-31 22:58:34 -06:00
|
|
|
if l == "" {
|
2020-10-26 10:49:16 -06:00
|
|
|
fmt.Fprint(&b, "<br>\n")
|
|
|
|
} else {
|
2020-10-31 22:58:34 -06:00
|
|
|
fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(string(l)))
|
2020-10-26 10:49:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pre {
|
|
|
|
fmt.Fprint(&b, "</pre>\n")
|
|
|
|
}
|
|
|
|
if list {
|
|
|
|
fmt.Fprint(&b, "</ul>\n")
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|