Use gmi (Text).HTML function

This commit is contained in:
adnano 2020-10-15 00:42:17 -04:00
parent fa7c320822
commit 101ed843b8
4 changed files with 4 additions and 80 deletions

2
go.mod
View file

@ -2,4 +2,4 @@ module kiln
go 1.15 go 1.15
require git.sr.ht/~adnano/gmi v0.1.0-alpha require git.sr.ht/~adnano/gmi v0.1.0-alpha.1

4
go.sum
View file

@ -1,2 +1,2 @@
git.sr.ht/~adnano/gmi v0.1.0-alpha h1:4TvMuN759vB1W+vI/U7aUJ3FMQB/BJb9KuQ618NedEg= git.sr.ht/~adnano/gmi v0.1.0-alpha.1 h1:5sha5ucev32U95drEEaPw9rm6hGWJtL8y5HViQ3VDJQ=
git.sr.ht/~adnano/gmi v0.1.0-alpha/go.mod h1:t/m2KtH+7lXIF7jjVN+bNvwPbE0nxHOpvlA/WZ/KeLQ= git.sr.ht/~adnano/gmi v0.1.0-alpha.1/go.mod h1:t/m2KtH+7lXIF7jjVN+bNvwPbE0nxHOpvlA/WZ/KeLQ=

76
html.go
View file

@ -1,76 +0,0 @@
package main
import (
"bytes"
"fmt"
"html"
"git.sr.ht/~adnano/gmi"
)
// gmiToHTML converts the provided Gemini text to HTML.
func gmiToHTML(text gmi.Text) []byte {
var b bytes.Buffer
var pre bool
var list bool
for _, l := range text {
if _, ok := l.(gmi.LineListItem); ok {
if !list {
list = true
fmt.Fprint(&b, "<ul>\n")
}
} else if list {
list = false
fmt.Fprint(&b, "</ul>\n")
}
switch l.(type) {
case gmi.LineLink:
link := l.(gmi.LineLink)
url := html.EscapeString(link.URL)
name := html.EscapeString(link.Name)
if name == "" {
name = url
}
fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name)
case gmi.LinePreformattingToggle:
pre = !pre
if pre {
fmt.Fprint(&b, "<pre>\n")
} else {
fmt.Fprint(&b, "</pre>\n")
}
case gmi.LinePreformattedText:
text := string(l.(gmi.LinePreformattedText))
fmt.Fprintf(&b, "%s\n", html.EscapeString(text))
case gmi.LineHeading1:
text := string(l.(gmi.LineHeading1))
fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text))
case gmi.LineHeading2:
text := string(l.(gmi.LineHeading2))
fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text))
case gmi.LineHeading3:
text := string(l.(gmi.LineHeading3))
fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(text))
case gmi.LineListItem:
text := string(l.(gmi.LineListItem))
fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text))
case gmi.LineQuote:
text := string(l.(gmi.LineQuote))
fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(text))
case gmi.LineText:
text := string(l.(gmi.LineText))
if text == "" {
fmt.Fprint(&b, "<br>\n")
} else {
fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text))
}
}
}
if pre {
fmt.Fprint(&b, "</pre>\n")
}
if list {
fmt.Fprint(&b, "</ul>\n")
}
return b.Bytes()
}

View file

@ -446,7 +446,7 @@ func outputHTML(p *Page) (path string, content []byte) {
r := bytes.NewReader(p.content) r := bytes.NewReader(p.content)
text := gmi.Parse(r) text := gmi.Parse(r)
content = gmiToHTML(text) content = []byte(text.HTML())
type tmplCtx struct { type tmplCtx struct {
Title string Title string