2021-02-28 03:53:16 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
pathpkg "path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Format represents an output format.
|
|
|
|
type Format interface {
|
|
|
|
Format(*Page, *Config) (path string, content []byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
type FormatFunc func(*Page, *Config) (string, []byte)
|
|
|
|
|
|
|
|
func (f FormatFunc) Format(p *Page, cfg *Config) (string, []byte) {
|
|
|
|
return f(p, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FormatGemini formats the page as Gemini text.
|
|
|
|
func FormatGemini(p *Page, cfg *Config) (path string, content []byte) {
|
2021-03-20 04:44:02 +01:00
|
|
|
path = pathpkg.Join(p.Path, "index.gmi")
|
2021-02-28 03:53:16 +01:00
|
|
|
content = []byte(p.Content)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// FormatHTML formats the page as HTML.
|
|
|
|
func FormatHTML(p *Page, cfg *Config) (path string, content []byte) {
|
|
|
|
path = pathpkg.Join(p.Path, "index.html")
|
|
|
|
|
|
|
|
r := strings.NewReader(p.Content)
|
2021-02-28 04:02:45 +01:00
|
|
|
content = textToHTML(r)
|
2021-02-28 03:53:16 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|