Add Format interface

This commit is contained in:
adnano 2021-02-27 21:53:16 -05:00
parent abfd442ab5
commit 9154319862
3 changed files with 66 additions and 54 deletions

6
dir.go
View file

@ -134,7 +134,7 @@ func (d *Dir) manipulate(cfg *Config) error {
} }
// write writes the Dir to the provided destination path. // 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 // Create the directory
dirPath := pathpkg.Join(dstDir, d.Path) dirPath := pathpkg.Join(dstDir, d.Path)
if err := os.MkdirAll(dirPath, 0755); err != nil { 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 // Write pages
for _, page := range d.Pages { for _, page := range d.Pages {
path, content := format(page, cfg) path, content := format.Format(page, cfg)
dstPath := pathpkg.Join(dstDir, path) dstPath := pathpkg.Join(dstDir, path)
dir := pathpkg.Dir(dstPath) dir := pathpkg.Dir(dstPath)
os.MkdirAll(dir, 0755) os.MkdirAll(dir, 0755)
@ -171,7 +171,7 @@ func (d *Dir) write(dstDir string, format outputFormat, cfg *Config) error {
// Write the index file // Write the index file
if d.index != nil { if d.index != nil {
path, content := format(d.index, cfg) path, content := format.Format(d.index, cfg)
dstPath := pathpkg.Join(dstDir, path) dstPath := pathpkg.Join(dstDir, path)
dir := pathpkg.Dir(dstPath) dir := pathpkg.Dir(dstPath)
os.MkdirAll(dir, 0755) os.MkdirAll(dir, 0755)

53
format.go Normal file
View 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
View file

@ -1,13 +1,8 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"log" "log"
pathpkg "path"
"strings"
"git.sr.ht/~adnano/go-gemini"
) )
func main() { 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.StringVar(&format, "format", "gemini", "output format to use. Supported formats include gemini and html")
flag.Parse() 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 // Load config
cfg := NewConfig() cfg := NewConfig()
if err := cfg.Load("config.ini"); err != nil { if err := cfg.Load("config.ini"); err != nil {
@ -40,6 +25,16 @@ func run() error {
return err 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 // Load content
dir := NewDir("") dir := NewDir("")
if err := dir.read("content", ""); err != nil { if err := dir.read("content", ""); err != nil {
@ -56,39 +51,3 @@ func run() error {
} }
return nil 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
}