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
+60 -59
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)
html := textToHTML(text) hw.Finish()
fmt.Print(html)
} }
// textToHTML returns the Gemini text response as HTML. type HTMLWriter struct {
func textToHTML(text gemini.Text) string { out io.Writer
var b strings.Builder pre bool
var pre bool list bool
var list bool }
for _, l := range text {
if _, ok := l.(gemini.LineListItem); ok { func (h *HTMLWriter) Handle(line gemini.Line) {
if !list { if _, ok := line.(gemini.LineListItem); ok {
list = true if !h.list {
fmt.Fprint(&b, "<ul>\n") h.list = true
} fmt.Fprint(h.out, "<ul>\n")
} else if list {
list = false
fmt.Fprint(&b, "</ul>\n")
} }
switch l := l.(type) { } else if h.list {
case gemini.LineLink: h.list = false
url := html.EscapeString(l.URL) fmt.Fprint(h.out, "</ul>\n")
name := html.EscapeString(l.Name) }
if name == "" { switch line := line.(type) {
name = url case gemini.LineLink:
} url := html.EscapeString(line.URL)
fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name) name := html.EscapeString(line.Name)
case gemini.LinePreformattingToggle: if name == "" {
pre = !pre name = url
if pre { }
fmt.Fprint(&b, "<pre>\n") fmt.Fprintf(h.out, "<p><a href='%s'>%s</a></p>\n", url, name)
} else { case gemini.LinePreformattingToggle:
fmt.Fprint(&b, "</pre>\n") h.pre = !h.pre
} if h.pre {
case gemini.LinePreformattedText: fmt.Fprint(h.out, "<pre>\n")
fmt.Fprintf(&b, "%s\n", html.EscapeString(string(l))) } else {
case gemini.LineHeading1: fmt.Fprint(h.out, "</pre>\n")
fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(string(l))) }
case gemini.LineHeading2: case gemini.LinePreformattedText:
fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "%s\n", html.EscapeString(string(line)))
case gemini.LineHeading3: case gemini.LineHeading1:
fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<h1>%s</h1>\n", html.EscapeString(string(line)))
case gemini.LineListItem: case gemini.LineHeading2:
fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<h2>%s</h2>\n", html.EscapeString(string(line)))
case gemini.LineQuote: case gemini.LineHeading3:
fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(string(l))) fmt.Fprintf(h.out, "<h3>%s</h3>\n", html.EscapeString(string(line)))
case gemini.LineText: case gemini.LineListItem:
if l == "" { fmt.Fprintf(h.out, "<li>%s</li>\n", html.EscapeString(string(line)))
fmt.Fprint(&b, "<br>\n") case gemini.LineQuote:
} else { fmt.Fprintf(h.out, "<blockquote>%s</blockquote>\n", html.EscapeString(string(line)))
fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(string(l))) case gemini.LineText:
} if line == "" {
fmt.Fprint(h.out, "<br>\n")
} else {
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()
} }