diff --git a/funcs.go b/funcs.go index cff26eb..0c9c63c 100644 --- a/funcs.go +++ b/funcs.go @@ -5,6 +5,8 @@ import ( "path" "reflect" "strings" + + "github.com/google/shlex" ) // funcs returns functions for use in templates. @@ -56,7 +58,11 @@ func (_strings) TrimSuffix(a, b string) string { return strings.TrimSuffi func executeString(command, input string) (string, error) { var b strings.Builder - if err := execute(command, strings.NewReader(input), &b); err != nil { + cmd, err := shlex.Split(command) + if err != nil { + return "", err + } + if err := execute(cmd, strings.NewReader(input), &b); err != nil { return "", err } return b.String(), nil diff --git a/go.mod b/go.mod index 369b553..368bc25 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module kiln go 1.16 require ( + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/pelletier/go-toml v1.9.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index e475c4e..1238ecf 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/pelletier/go-toml v1.9.0 h1:NOd0BRdOKpPf0SxkL3HxSQOG7rNh+4kl6PHcBPFs7Q0= github.com/pelletier/go-toml v1.9.0/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/page.go b/page.go index 273b4ba..27fbe32 100644 --- a/page.go +++ b/page.go @@ -120,7 +120,7 @@ func (p *Page) _read(srcDir, path string, task *Task, cfg *Site) error { content = bytes.TrimLeft(content, "\r\n") } - if cmd, ok := task.Preprocess[strings.TrimPrefix(ext, ".")]; ok { + if cmd, ok := task.preprocess[strings.TrimPrefix(ext, ".")]; ok { var buf bytes.Buffer if err := execute(cmd, bytes.NewReader(content), &buf); err != nil { return err @@ -275,7 +275,7 @@ func (p *Page) write(dstDir string, task *Task) error { func (p *Page) writeTo(dstPath string, task *Task) error { var content []byte - if cmd := task.Postprocess; cmd != "" { + if cmd := task.postprocess; cmd != nil { var buf bytes.Buffer if err := execute(cmd, strings.NewReader(p.Content), &buf); err != nil { return err @@ -331,9 +331,11 @@ func sortPages(pages []*Page) { } // execute runs a command. -func execute(command string, input io.Reader, output io.Writer) error { - split := strings.Split(command, " ") - cmd := exec.Command(split[0], split[1:]...) +func execute(command []string, input io.Reader, output io.Writer) error { + if len(command) == 0 { + return nil + } + cmd := exec.Command(command[0], command[1:]...) cmd.Stdin = input cmd.Stderr = os.Stderr cmd.Stdout = output diff --git a/site.go b/site.go index 34f1331..c4aae12 100644 --- a/site.go +++ b/site.go @@ -6,6 +6,7 @@ import ( "text/template" "time" + "github.com/google/shlex" "github.com/pelletier/go-toml" ) @@ -33,6 +34,8 @@ type Task struct { URL string `toml:"url"` UglyURLs bool `toml:"ugly_urls"` Feeds []Feed `toml:"feeds"` + preprocess map[string][]string + postprocess []string feeds map[string][]Feed } @@ -92,13 +95,30 @@ func LoadSite(config string) (*Site, error) { return nil, err } - // Populate task feeds map for _, task := range site.Tasks { + // Populate feeds task.feeds = map[string][]Feed{} for _, feed := range task.Feeds { dir := feed.InputDir task.feeds[dir] = append(task.feeds[dir], feed) } + + // Parse commands + task.preprocess = map[string][]string{} + for path := range task.Preprocess { + preprocess, err := shlex.Split(task.Preprocess[path]) + if err != nil { + return nil, err + } + task.preprocess[path] = preprocess + } + if task.Postprocess != "" { + postprocess, err := shlex.Split(task.Postprocess) + if err != nil { + return nil, err + } + task.postprocess = postprocess + } } return site, nil