1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-11-08 14:19:20 +01:00

Move HTML rendering code to kiln.go

This commit is contained in:
adnano 2020-09-29 14:10:49 -04:00
parent 455eb221ac
commit 0d5d8e9102
2 changed files with 80 additions and 74 deletions

82
kiln.go

@ -2,6 +2,8 @@ package main
import (
"bytes"
"fmt"
"html"
"io/ioutil"
"os"
"path/filepath"
@ -10,6 +12,8 @@ import (
"strings"
"text/template"
"time"
"git.sr.ht/~adnano/gmi"
)
// Site represents a kiln site.
@ -255,7 +259,7 @@ func (d *Directory) Read(site *Site, srcDir string, path string) error {
// Write writes the Directory to the provided destination path.
func (d *Directory) Write(dstDir string, format OutputFormat) error {
// Create the directory
dirPath := filepath.Join(dstDir, d.Path)
dirPath := filepath.Join(dstDir, d.Permalink)
if err := os.MkdirAll(dirPath, 0755); err != nil {
return err
}
@ -318,8 +322,82 @@ func OutputGemini(p *Page) (path string, content []byte) {
func OutputHTML(p *Page) (path string, content []byte) {
const indexPath = "index.html"
const meta = `<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="/style.css">
<title>%s</title>
`
path = filepath.Join(p.Permalink, indexPath)
var b bytes.Buffer
fmt.Fprintf(&b, meta, p.Title)
var pre bool
var list bool
r := bytes.NewReader(p.content)
content = GeminiToHTML(r)
text := gmi.Parse(r)
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")
}
content = b.Bytes()
return
}

@ -1,72 +0,0 @@
package main
import (
"bytes"
"fmt"
"html"
"io"
"git.sr.ht/~adnano/gmi"
)
// GeminiToHTML reads Gemini from the provided reader and returns an HTML string.
func GeminiToHTML(r io.Reader) []byte {
var b bytes.Buffer
var pre bool
var list bool
b.WriteString("<link rel='stylesheet' href='/style.css'>\n")
text := gmi.Parse(r)
for _, l := range text {
if _, ok := l.(gmi.LineListItem); ok {
if !list {
list = true
b.WriteString("<ul>\n")
}
} else if list {
list = false
b.WriteString("</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>", url, name)
case gmi.LinePreformattingToggle:
pre = !pre
if pre {
b.WriteString("<pre>\n")
} else {
b.WriteString("</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))
fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text))
}
}
return b.Bytes()
}