examples/html: Read from stdin and write to stdout

This commit is contained in:
Adnan Maolood 2020-12-18 00:45:09 -05:00
parent 36c2086c82
commit 5af1acbd54

View File

@ -7,76 +7,77 @@ package main
import ( import (
"fmt" "fmt"
"html" "html"
"strings" "io"
"os"
"git.sr.ht/~adnano/go-gemini" "git.sr.ht/~adnano/go-gemini"
) )
func main() { func main() {
text := gemini.Text{ hw := HTMLWriter{
gemini.LineHeading1("Hello, world!"), out: os.Stdout,
gemini.LineText("This is a gemini text document."), }
gemini.ParseLines(os.Stdin, hw.Handle)
hw.Finish()
} }
html := textToHTML(text) type HTMLWriter struct {
fmt.Print(html) out io.Writer
pre bool
list bool
} }
// textToHTML returns the Gemini text response as HTML. func (h *HTMLWriter) Handle(line gemini.Line) {
func textToHTML(text gemini.Text) string { if _, ok := line.(gemini.LineListItem); ok {
var b strings.Builder if !h.list {
var pre bool h.list = true
var list bool fmt.Fprint(h.out, "<ul>\n")
for _, l := range text {
if _, ok := l.(gemini.LineListItem); ok {
if !list {
list = true
fmt.Fprint(&b, "<ul>\n")
} }
} else if list { } else if h.list {
list = false h.list = false
fmt.Fprint(&b, "</ul>\n") fmt.Fprint(h.out, "</ul>\n")
} }
switch l := l.(type) { switch line := line.(type) {
case gemini.LineLink: case gemini.LineLink:
url := html.EscapeString(l.URL) url := html.EscapeString(line.URL)
name := html.EscapeString(l.Name) name := html.EscapeString(line.Name)
if name == "" { if name == "" {
name = url name = url
} }
fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name) fmt.Fprintf(h.out, "<p><a href='%s'>%s</a></p>\n", url, name)
case gemini.LinePreformattingToggle: case gemini.LinePreformattingToggle:
pre = !pre h.pre = !h.pre
if pre { if h.pre {
fmt.Fprint(&b, "<pre>\n") fmt.Fprint(h.out, "<pre>\n")
} else { } else {
fmt.Fprint(&b, "</pre>\n") fmt.Fprint(h.out, "</pre>\n")
} }
case gemini.LinePreformattedText: case gemini.LinePreformattedText:
fmt.Fprintf(&b, "%s\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "%s\n", html.EscapeString(string(line)))
case gemini.LineHeading1: case gemini.LineHeading1:
fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<h1>%s</h1>\n", html.EscapeString(string(line)))
case gemini.LineHeading2: case gemini.LineHeading2:
fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<h2>%s</h2>\n", html.EscapeString(string(line)))
case gemini.LineHeading3: case gemini.LineHeading3:
fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<h3>%s</h3>\n", html.EscapeString(string(line)))
case gemini.LineListItem: case gemini.LineListItem:
fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<li>%s</li>\n", html.EscapeString(string(line)))
case gemini.LineQuote: case gemini.LineQuote:
fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<blockquote>%s</blockquote>\n", html.EscapeString(string(line)))
case gemini.LineText: case gemini.LineText:
if l == "" { if line == "" {
fmt.Fprint(&b, "<br>\n") fmt.Fprint(h.out, "<br>\n")
} else { } else {
fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<p>%s</p>\n", html.EscapeString(string(line)))
} }
} }
} }
if pre {
fmt.Fprint(&b, "</pre>\n") func (h *HTMLWriter) Finish() {
if h.pre {
fmt.Fprint(h.out, "</pre>\n")
} }
if list { if h.list {
fmt.Fprint(&b, "</ul>\n") fmt.Fprint(h.out, "</ul>\n")
} }
return b.String()
} }