From 4e47940307123a559c02780fc5b5e2006a5f431e Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Mon, 10 May 2021 12:23:27 -0400 Subject: [PATCH] Allow multiple input formats per task --- config.go | 25 +++++++++++++++++-------- config.toml | 6 +++--- dir.go | 16 ++++++++-------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/config.go b/config.go index 88a55e0..ddc8551 100644 --- a/config.go +++ b/config.go @@ -24,14 +24,23 @@ type Config struct { // Task represents a site build task. type Task struct { - InputExt string `toml:"input_ext"` // input file extension - OutputExt string `toml:"output_ext"` // output file extension - TemplateExt string `toml:"template_ext"` // template file extension - PreProcess string `toml:"preprocess"` // preprocess command - PostProcess string `toml:"postprocess"` // postprocess command - StaticDir string `toml:"static_dir"` // static file directory - OutputDir string `toml:"output_dir"` // output directory - UglyURLs bool `toml:"ugly_urls"` // whether to use ugly URLs + Input []string `toml:"input"` // input file suffixes + OutputExt string `toml:"output"` // output file suffix + TemplateExt string `toml:"template"` // template file suffix + Preprocess map[string]string `toml:"preprocess"` // preprocess commands + Postprocess string `toml:"postprocess"` // postprocess command + StaticDir string `toml:"static_dir"` // static file directory + OutputDir string `toml:"output_dir"` // output directory + UglyURLs bool `toml:"ugly_urls"` // whether to use ugly URLs +} + +func (t *Task) Match(ext string) bool { + for i := range t.Input { + if t.Input[i] == ext { + return true + } + } + return false } // LoadConfig loads the configuration from the provided path. diff --git a/config.toml b/config.toml index d71604a..59b243b 100644 --- a/config.toml +++ b/config.toml @@ -13,8 +13,8 @@ urls = [] # Site tasks [[tasks]] -input_ext = ".gmi" -output_ext = ".gmi" -template_ext = ".gmi" +input = [".gmi"] +output = ".gmi" +template = ".gmi" static_dir = "static" output_dir = "public" diff --git a/dir.go b/dir.go index 16bc2e8..912b5f8 100644 --- a/dir.go +++ b/dir.go @@ -70,7 +70,7 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Config) error { return err } d.Dirs = append(d.Dirs, dir) - } else if ext := pathpkg.Ext(name); ext == task.InputExt { + } else if ext := pathpkg.Ext(name); task.Match(ext) { srcPath := pathpkg.Join(srcDir, path) content, err := ioutil.ReadFile(srcPath) if err != nil { @@ -115,8 +115,8 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Config) error { content = bytes.TrimLeft(content, "\r\n") } - if cmd := task.PreProcess; cmd != "" { - content = RunProcessCmd(cmd, bytes.NewReader(content)) + if cmd, ok := task.Preprocess[strings.TrimPrefix(ext, ".")]; ok { + content = process(cmd, bytes.NewReader(content)) } page.Content = string(content) @@ -124,7 +124,7 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Config) error { page.Path = d.Path d.index = page } else { - path = "/" + strings.TrimSuffix(path, task.InputExt) + path = "/" + strings.TrimSuffix(path, ext) if task.UglyURLs { path += task.OutputExt } else { @@ -227,8 +227,8 @@ func (d *Dir) write(dstDir string, task *Task) error { path = pathpkg.Join(path, "index"+task.OutputExt) } var content []byte - if cmd := task.PostProcess; cmd != "" { - content = RunProcessCmd(cmd, strings.NewReader(page.Content)) + if cmd := task.Postprocess; cmd != "" { + content = process(cmd, strings.NewReader(page.Content)) } else { content = []byte(page.Content) } @@ -269,8 +269,8 @@ func (d *Dir) sort() { } } -// RunProcessCmd runs a process command. -func RunProcessCmd(command string, input io.Reader) []byte { +// process runs a process command. +func process(command string, input io.Reader) []byte { split := strings.Split(command, " ") cmd := exec.Command(split[0], split[1:]...) cmd.Stdin = input