html: Make textToHTML accept an io.Reader

Use gemini.ParseLines to convert the Gemini text to HTML in one pass.
This commit is contained in:
adnano 2021-02-27 22:02:45 -05:00
parent 9154319862
commit 1ff05f49f5
2 changed files with 70 additions and 67 deletions

View file

@ -4,8 +4,6 @@ import (
"bytes" "bytes"
pathpkg "path" pathpkg "path"
"strings" "strings"
"git.sr.ht/~adnano/go-gemini"
) )
// Format represents an output format. // Format represents an output format.
@ -31,8 +29,7 @@ func FormatHTML(p *Page, cfg *Config) (path string, content []byte) {
path = pathpkg.Join(p.Path, "index.html") path = pathpkg.Join(p.Path, "index.html")
r := strings.NewReader(p.Content) r := strings.NewReader(p.Content)
text := gemini.ParseText(r) content = textToHTML(r)
content = textToHTML(text)
// html template context // html template context
type htmlCtx struct { type htmlCtx struct {

132
html.go
View file

@ -4,73 +4,79 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"html" "html"
"io"
"git.sr.ht/~adnano/go-gemini" "git.sr.ht/~adnano/go-gemini"
) )
// textToHTML returns the Gemini text response as HTML. // textToHTML returns the Gemini text response as HTML.
func textToHTML(text gemini.Text) []byte { func textToHTML(r io.Reader) []byte {
var b bytes.Buffer buf := &bytes.Buffer{}
var pre bool hw := &HTMLWriter{
var list bool out: buf,
for _, l := range text { }
if _, ok := l.(gemini.LineListItem); ok { defer hw.Finish()
if !list { gemini.ParseLines(r, hw.Handle)
list = true return buf.Bytes()
b.WriteString("<ul>\n") }
}
} else if list { type HTMLWriter struct {
list = false out io.Writer
b.WriteString("</ul>\n") pre bool
} list bool
switch l.(type) { }
case gemini.LineLink:
link := l.(gemini.LineLink) func (h *HTMLWriter) Handle(line gemini.Line) {
url := html.EscapeString(link.URL) if _, ok := line.(gemini.LineListItem); ok {
name := html.EscapeString(link.Name) if !h.list {
if name == "" { h.list = true
name = url fmt.Fprint(h.out, "<ul>\n")
} }
fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name) } else if h.list {
case gemini.LinePreformattingToggle: h.list = false
pre = !pre fmt.Fprint(h.out, "</ul>\n")
if pre { }
b.WriteString("<pre>\n") switch line := line.(type) {
} else { case gemini.LineLink:
b.WriteString("</pre>\n") url := html.EscapeString(line.URL)
} name := html.EscapeString(line.Name)
case gemini.LinePreformattedText: if name == "" {
text := string(l.(gemini.LinePreformattedText)) name = url
fmt.Fprintf(&b, "%s\n", html.EscapeString(text)) }
case gemini.LineHeading1: fmt.Fprintf(h.out, "<p><a href='%s'>%s</a></p>\n", url, name)
text := string(l.(gemini.LineHeading1)) case gemini.LinePreformattingToggle:
fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text)) h.pre = !h.pre
case gemini.LineHeading2: if h.pre {
text := string(l.(gemini.LineHeading2)) fmt.Fprint(h.out, "<pre>\n")
fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text)) } else {
case gemini.LineHeading3: fmt.Fprint(h.out, "</pre>\n")
text := string(l.(gemini.LineHeading3)) }
fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(text)) case gemini.LinePreformattedText:
case gemini.LineListItem: fmt.Fprintf(h.out, "%s\n", html.EscapeString(string(line)))
text := string(l.(gemini.LineListItem)) case gemini.LineHeading1:
fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text)) fmt.Fprintf(h.out, "<h1>%s</h1>\n", html.EscapeString(string(line)))
case gemini.LineQuote: case gemini.LineHeading2:
text := string(l.(gemini.LineQuote)) fmt.Fprintf(h.out, "<h2>%s</h2>\n", html.EscapeString(string(line)))
fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(text)) case gemini.LineHeading3:
case gemini.LineText: fmt.Fprintf(h.out, "<h3>%s</h3>\n", html.EscapeString(string(line)))
text := string(l.(gemini.LineText)) case gemini.LineListItem:
if text == "" { fmt.Fprintf(h.out, "<li>%s</li>\n", html.EscapeString(string(line)))
b.WriteString("<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(text)) case gemini.LineText:
} if line == "" {
} fmt.Fprint(h.out, "<br>\n")
} else {
fmt.Fprintf(h.out, "<p>%s</p>\n", html.EscapeString(string(line)))
}
}
}
func (h *HTMLWriter) Finish() {
if h.pre {
fmt.Fprint(h.out, "</pre>\n")
}
if h.list {
fmt.Fprint(h.out, "</ul>\n")
} }
if pre {
b.WriteString("</pre>\n")
}
if list {
b.WriteString("</ul>\n")
}
return b.Bytes()
} }