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.
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.

View file

@ -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"

16
dir.go
View file

@ -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