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"
pathpkg "path"
"strings"
"git.sr.ht/~adnano/go-gemini"
)
// 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")
r := strings.NewReader(p.Content)
text := gemini.ParseText(r)
content = textToHTML(text)
content = textToHTML(r)
// html template context
type htmlCtx struct {

90
html.go
View file

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