diff --git a/dir.go b/dir.go index 4a18ccb..1f0545b 100644 --- a/dir.go +++ b/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) diff --git a/format.go b/format.go new file mode 100644 index 0000000..e27fe34 --- /dev/null +++ b/format.go @@ -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 +} diff --git a/main.go b/main.go index 74a89dc..617710e 100644 --- a/main.go +++ b/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 -}