From c7d6e13831c37527cd0129a5e48118405f1130f3 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Sat, 22 May 2021 20:41:52 -0400 Subject: [PATCH] funcs: Add exec --- dir.go | 16 ++++++++++------ funcs.go | 7 +++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/dir.go b/dir.go index b3bac5e..e0a2505 100644 --- a/dir.go +++ b/dir.go @@ -114,7 +114,9 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Site) error { } if cmd, ok := task.Preprocess[strings.TrimPrefix(ext, ".")]; ok { - content = process(cmd, bytes.NewReader(content)) + var buf bytes.Buffer + execute(cmd, bytes.NewReader(content), &buf) + content = buf.Bytes() } page.Content = string(content) @@ -228,7 +230,9 @@ func (d *Dir) write(dstDir string, task *Task) error { } var content []byte if cmd := task.Postprocess; cmd != "" { - content = process(cmd, strings.NewReader(page.Content)) + var buf bytes.Buffer + execute(cmd, strings.NewReader(page.Content), &buf) + content = buf.Bytes() } else { content = []byte(page.Content) } @@ -290,17 +294,17 @@ func (d *Dir) sort() { } } -// process runs a process command. -func process(command string, input io.Reader) []byte { +// execute runs a command. +func execute(command string, input io.Reader, output io.Writer) { split := strings.Split(command, " ") cmd := exec.Command(split[0], split[1:]...) cmd.Stdin = input cmd.Stderr = os.Stderr - output, err := cmd.Output() + cmd.Stdout = output + err := cmd.Run() if err != nil { log.Fatal(err) } - return output } func (d *Dir) Title() string { diff --git a/funcs.go b/funcs.go index ec282bf..7e40dd9 100644 --- a/funcs.go +++ b/funcs.go @@ -11,6 +11,7 @@ import ( func (s *Site) funcs() map[string]interface{} { return map[string]interface{}{ "dir": s.dir, + "exec": executeString, "page": s.page, "path": func() _path { return _path{} }, "partial": s.templates.ExecutePartial, @@ -54,6 +55,12 @@ func (_strings) TrimRight(a, b string) string { return strings.TrimRight func (_strings) TrimSpace(s string) string { return strings.TrimSpace(s) } func (_strings) TrimSuffix(a, b string) string { return strings.TrimSuffix(a, b) } +func executeString(command, input string) string { + var b strings.Builder + execute(command, strings.NewReader(input), &b) + return b.String() +} + func reverse(s interface{}) interface{} { v := reflect.ValueOf(s) n := v.Len()