diff options
| author | Adnan Maolood <[email protected]> | 2020-10-26 12:49:16 -0400 |
|---|---|---|
| committer | Adnan Maolood <[email protected]> | 2020-10-26 12:49:16 -0400 |
| commit | c44f011b15f92373d58b451c8620d99fcf3c4296 (patch) | |
| tree | 2b8deace58c34edf4ef50113b404a990cff16b2a /examples/html.go | |
| parent | Add contributing instructions to README.md (diff) | |
| download | go-gemini-c44f011b15f92373d58b451c8620d99fcf3c4296.tar.xz go-gemini-c44f011b15f92373d58b451c8620d99fcf3c4296.zip | |
Remove (Text).HTML functionv0.1.1
Diffstat (limited to 'examples/html.go')
| -rw-r--r-- | examples/html.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/examples/html.go b/examples/html.go new file mode 100644 index 0000000..fe59c87 --- /dev/null +++ b/examples/html.go @@ -0,0 +1,90 @@ +// +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") + } + switch l.(type) { + case gemini.LineLink: + link := l.(gemini.LineLink) + url := html.EscapeString(link.URL) + name := html.EscapeString(link.Name) + 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: + text := string(l.(gemini.LinePreformattedText)) + fmt.Fprintf(&b, "%s\n", html.EscapeString(text)) + case gemini.LineHeading1: + text := string(l.(gemini.LineHeading1)) + fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text)) + case gemini.LineHeading2: + text := string(l.(gemini.LineHeading2)) + fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text)) + case gemini.LineHeading3: + text := string(l.(gemini.LineHeading3)) + fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(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, "<blockquote>%s</blockquote>\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") + } + return b.String() +} |