mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
Add Format interface
This commit is contained in:
parent
abfd442ab5
commit
9154319862
6
dir.go
6
dir.go
|
@ -134,7 +134,7 @@ func (d *Dir) manipulate(cfg *Config) error {
|
|||
}
|
||||
|
||||
// write writes the Dir to the provided destination path.
|
||||
func (d *Dir) write(dstDir string, format outputFormat, cfg *Config) error {
|
||||
func (d *Dir) write(dstDir string, format Format, cfg *Config) error {
|
||||
// Create the directory
|
||||
dirPath := pathpkg.Join(dstDir, d.Path)
|
||||
if err := os.MkdirAll(dirPath, 0755); err != nil {
|
||||
|
@ -156,7 +156,7 @@ func (d *Dir) write(dstDir string, format outputFormat, cfg *Config) error {
|
|||
|
||||
// Write pages
|
||||
for _, page := range d.Pages {
|
||||
path, content := format(page, cfg)
|
||||
path, content := format.Format(page, cfg)
|
||||
dstPath := pathpkg.Join(dstDir, path)
|
||||
dir := pathpkg.Dir(dstPath)
|
||||
os.MkdirAll(dir, 0755)
|
||||
|
@ -171,7 +171,7 @@ func (d *Dir) write(dstDir string, format outputFormat, cfg *Config) error {
|
|||
|
||||
// Write the index file
|
||||
if d.index != nil {
|
||||
path, content := format(d.index, cfg)
|
||||
path, content := format.Format(d.index, cfg)
|
||||
dstPath := pathpkg.Join(dstDir, path)
|
||||
dir := pathpkg.Dir(dstPath)
|
||||
os.MkdirAll(dir, 0755)
|
||||
|
|
53
format.go
Normal file
53
format.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
pathpkg "path"
|
||||
"strings"
|
||||
|
||||
"git.sr.ht/~adnano/go-gemini"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
path = pathpkg.Join(path, "index.gmi")
|
||||
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)
|
||||
text := gemini.ParseText(r)
|
||||
content = textToHTML(text)
|
||||
|
||||
// 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
|
||||
}
|
61
main.go
61
main.go
|
@ -1,13 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"log"
|
||||
pathpkg "path"
|
||||
"strings"
|
||||
|
||||
"git.sr.ht/~adnano/go-gemini"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -21,16 +16,6 @@ func run() error {
|
|||
flag.StringVar(&format, "format", "gemini", "output format to use. Supported formats include gemini and html")
|
||||
flag.Parse()
|
||||
|
||||
var output outputFormat
|
||||
switch format {
|
||||
case "gemini":
|
||||
output = outputGemini
|
||||
case "html":
|
||||
output = outputHTML
|
||||
default:
|
||||
log.Fatalf("unknown output format %q", format)
|
||||
}
|
||||
|
||||
// Load config
|
||||
cfg := NewConfig()
|
||||
if err := cfg.Load("config.ini"); err != nil {
|
||||
|
@ -40,6 +25,16 @@ func run() error {
|
|||
return err
|
||||
}
|
||||
|
||||
var output Format
|
||||
switch format {
|
||||
case "gemini":
|
||||
output = FormatFunc(FormatGemini)
|
||||
case "html":
|
||||
output = FormatFunc(FormatHTML)
|
||||
default:
|
||||
log.Fatalf("unknown output format %q", format)
|
||||
}
|
||||
|
||||
// Load content
|
||||
dir := NewDir("")
|
||||
if err := dir.read("content", ""); err != nil {
|
||||
|
@ -56,39 +51,3 @@ func run() error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// outputFormat represents an output format.
|
||||
type outputFormat func(*Page, *Config) (path string, content []byte)
|
||||
|
||||
// outputGemini outputs the page as Gemini text.
|
||||
func outputGemini(p *Page, cfg *Config) (path string, content []byte) {
|
||||
path = p.Path + "index.gmi"
|
||||
content = []byte(p.Content)
|
||||
return
|
||||
}
|
||||
|
||||
// outputHTML outputs the page as HTML.
|
||||
func outputHTML(p *Page, cfg *Config) (path string, content []byte) {
|
||||
path = p.Path + "index.html"
|
||||
|
||||
r := strings.NewReader(p.Content)
|
||||
text := gemini.ParseText(r)
|
||||
content = textToHTML(text)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue