package main import ( "bytes" "fmt" "html" "io" "git.sr.ht/~adnano/go-gemini" ) // textToHTML returns the Gemini text response as HTML. func textToHTML(r io.Reader) []byte { buf := &bytes.Buffer{} hw := &HTMLWriter{ out: buf, } defer hw.Finish() gemini.ParseLines(r, hw.Handle) return buf.Bytes() } type HTMLWriter struct { out io.Writer pre bool list bool } func (h *HTMLWriter) Handle(line gemini.Line) { if _, ok := line.(gemini.LineListItem); ok { if !h.list { h.list = true fmt.Fprint(h.out, "
\n") } else { fmt.Fprint(h.out, "\n") } case gemini.LinePreformattedText: fmt.Fprintf(h.out, "%s\n", html.EscapeString(string(line))) case gemini.LineHeading1: fmt.Fprintf(h.out, "
%s\n", html.EscapeString(string(line))) case gemini.LineText: if line == "" { fmt.Fprint(h.out, "
%s
\n", html.EscapeString(string(line))) } } } func (h *HTMLWriter) Finish() { if h.pre { fmt.Fprint(h.out, "\n") } if h.list { fmt.Fprint(h.out, "\n") } }