Allow multiple input formats per task

This commit is contained in:
adnano 2021-05-10 12:23:27 -04:00
parent c2c20ffd1e
commit 61a1380f1c
3 changed files with 28 additions and 19 deletions

View file

@ -24,14 +24,23 @@ type Config struct {
// Task represents a site build task. // Task represents a site build task.
type Task struct { type Task struct {
InputExt string `toml:"input_ext"` // input file extension Input []string `toml:"input"` // input file suffixes
OutputExt string `toml:"output_ext"` // output file extension OutputExt string `toml:"output"` // output file suffix
TemplateExt string `toml:"template_ext"` // template file extension TemplateExt string `toml:"template"` // template file suffix
PreProcess string `toml:"preprocess"` // preprocess command Preprocess map[string]string `toml:"preprocess"` // preprocess commands
PostProcess string `toml:"postprocess"` // postprocess command Postprocess string `toml:"postprocess"` // postprocess command
StaticDir string `toml:"static_dir"` // static file directory StaticDir string `toml:"static_dir"` // static file directory
OutputDir string `toml:"output_dir"` // output directory OutputDir string `toml:"output_dir"` // output directory
UglyURLs bool `toml:"ugly_urls"` // whether to use ugly URLs 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. // LoadConfig loads the configuration from the provided path.

View file

@ -13,8 +13,8 @@ urls = []
# Site tasks # Site tasks
[[tasks]] [[tasks]]
input_ext = ".gmi" input = [".gmi"]
output_ext = ".gmi" output = ".gmi"
template_ext = ".gmi" template = ".gmi"
static_dir = "static" static_dir = "static"
output_dir = "public" output_dir = "public"

16
dir.go
View file

@ -70,7 +70,7 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Config) error {
return err return err
} }
d.Dirs = append(d.Dirs, dir) 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) srcPath := pathpkg.Join(srcDir, path)
content, err := ioutil.ReadFile(srcPath) content, err := ioutil.ReadFile(srcPath)
if err != nil { 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") content = bytes.TrimLeft(content, "\r\n")
} }
if cmd := task.PreProcess; cmd != "" { if cmd, ok := task.Preprocess[strings.TrimPrefix(ext, ".")]; ok {
content = RunProcessCmd(cmd, bytes.NewReader(content)) content = process(cmd, bytes.NewReader(content))
} }
page.Content = string(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 page.Path = d.Path
d.index = page d.index = page
} else { } else {
path = "/" + strings.TrimSuffix(path, task.InputExt) path = "/" + strings.TrimSuffix(path, ext)
if task.UglyURLs { if task.UglyURLs {
path += task.OutputExt path += task.OutputExt
} else { } else {
@ -227,8 +227,8 @@ func (d *Dir) write(dstDir string, task *Task) error {
path = pathpkg.Join(path, "index"+task.OutputExt) path = pathpkg.Join(path, "index"+task.OutputExt)
} }
var content []byte var content []byte
if cmd := task.PostProcess; cmd != "" { if cmd := task.Postprocess; cmd != "" {
content = RunProcessCmd(cmd, strings.NewReader(page.Content)) content = process(cmd, strings.NewReader(page.Content))
} else { } else {
content = []byte(page.Content) content = []byte(page.Content)
} }
@ -269,8 +269,8 @@ func (d *Dir) sort() {
} }
} }
// RunProcessCmd runs a process command. // process runs a process command.
func RunProcessCmd(command string, input io.Reader) []byte { func process(command string, input io.Reader) []byte {
split := strings.Split(command, " ") split := strings.Split(command, " ")
cmd := exec.Command(split[0], split[1:]...) cmd := exec.Command(split[0], split[1:]...)
cmd.Stdin = input cmd.Stdin = input