From 9edacc1541f56785c1cbc6ed4549b000e6963b2c Mon Sep 17 00:00:00 2001 From: adnano Date: Sun, 11 Apr 2021 17:23:01 -0400 Subject: [PATCH] config: Implement support for postprocess commands --- config.go | 21 ++++++++++++++++++++- example/config.toml | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 50ce170..71ecd3f 100644 --- a/config.go +++ b/config.go @@ -1,8 +1,12 @@ package main import ( + "bytes" + "log" "os" + "os/exec" "path" + "strings" "text/template" "github.com/BurntSushi/toml" @@ -27,7 +31,22 @@ type Task struct { } func (t Task) Format(p *Page) (string, []byte) { - return path.Join(p.Path, "index"+t.Output), []byte(p.Content) + path := path.Join(p.Path, "index"+t.Output) + + // Run a custom command. + if t.PostProcess != "" { + split := strings.Split(t.PostProcess, " ") + cmd := exec.Command(split[0], split[1:]...) + buf := new(bytes.Buffer) + cmd.Stdin = strings.NewReader(p.Content) + cmd.Stdout = buf + if err := cmd.Run(); err != nil { + log.Fatal(err) + } + return path, buf.Bytes() + } + + return path, []byte(p.Content) } // DefaultConfig returns the default configuration. diff --git a/example/config.toml b/example/config.toml index 746c34f..90720fd 100644 --- a/example/config.toml +++ b/example/config.toml @@ -14,5 +14,5 @@ destination = "public" input = ".gmi" output = ".html" template = ".gmi" -postprocess = "geminiToHTML" +postprocess = "geminitohtml" destination = "public.html"