mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 09:23:09 +00:00
46 lines
991 B
Go
46 lines
991 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
pathpkg "path"
|
|
"strings"
|
|
)
|
|
|
|
// Format represents an output format.
|
|
type Format interface {
|
|
Format(*Page) (path string, content []byte)
|
|
}
|
|
|
|
type FormatFunc func(*Page) (string, []byte)
|
|
|
|
func (f FormatFunc) Format(p *Page) (string, []byte) {
|
|
return f(p)
|
|
}
|
|
|
|
// GeminiToHTML returns an output format that converts Gemini text to HTML.
|
|
func GeminiToHTML(cfg *Config) Format {
|
|
return FormatFunc(func(p *Page) (path string, content []byte) {
|
|
path = pathpkg.Join(p.Path, "index.html")
|
|
|
|
r := strings.NewReader(p.Content)
|
|
content = textToHTML(r)
|
|
|
|
// html template context
|
|
type htmlCtx struct {
|
|
Title string // page title
|
|
Content string // page HTML contents
|
|
}
|
|
|
|
var b bytes.Buffer
|
|
// clean path to remove trailing slash
|
|
dir := pathpkg.Dir(pathpkg.Clean(p.Path))
|
|
tmpl := cfg.Templates.FindTemplate(dir, "output.html")
|
|
tmpl.Execute(&b, &htmlCtx{
|
|
Title: p.Title,
|
|
Content: string(content),
|
|
})
|
|
content = b.Bytes()
|
|
return
|
|
})
|
|
}
|