From 492cf2de4b502c792f6fd1792e19ff788274945d Mon Sep 17 00:00:00 2001 From: dre Date: Sun, 11 Jul 2021 00:09:35 +0800 Subject: [PATCH] pipe through proc and write to destination --- main.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index da68c1f..37d840a 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main import ( + "bytes" "flag" "fmt" "log" "os" + "os/exec" "path" "path/filepath" "strings" @@ -82,8 +84,12 @@ func main() { return defaultPermalinkFormat } + // iterate through file tree source files := make(chan File) go collectFiles(source, files) + + // for each file, get destination path, switch file extension, remove underscore for index + var tree FileTree for file := range files { pattern := linkcfg(file.Parent) err := destinationPath(&file, pattern) @@ -91,19 +97,67 @@ func main() { fmt.Println(err, file) continue } - fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body)) + //fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body)) + tree.Files = append(tree.Files, file) + } + + // call proc and pipe content through it, catch output of proc + for i, file := range tree.Files { + // fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body)) + extpipe := exec.Command(processor) + buf := bytes.NewReader(file.Body) + extpipe.Stdin = buf + + var procout bytes.Buffer + extpipe.Stdout = &procout + + extpipe.Start() + extpipe.Wait() + + // write to source + tree.Files[i].NewBody = procout.Bytes() + } + + newpath := filepath.Join(".", "public") + err = os.MkdirAll(newpath, os.ModePerm) + if err != nil { + log.Fatal(err) + } + + // write to destination + for _, file := range tree.Files { + fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body)) + + outfile := "index." + ext + outdir := filepath.Join(destination, file.Destination) + if file.Destination != "index" { + newpath := filepath.Join(destination, file.Destination) + if err = os.MkdirAll(newpath, os.ModePerm); err != nil { + log.Println(err) + continue + } + } else { + outdir = destination + } + + fullpath := filepath.Join(outdir, outfile) + newfile, err := os.Create(fullpath) + if err != nil { + log.Println(err) + continue + } + fmt.Println(fullpath, string(file.NewBody)) + newfile.Write(file.NewBody) + newfile.Close() } - // iterate through file tree source - // for each file, get destination path, switch file extension, remove underscore for index - // call proc and pipe content through it, catch output of proc and write to destination // replace links? // write rss? // write listings from template? } type FileTree struct { - Files map[string]*File + Files []File } type File struct { @@ -114,7 +168,8 @@ type File struct { Name string Extension string - Body []byte + Body []byte + NewBody []byte } func parse(fullpath string) ([]byte, *Content, error) { @@ -122,6 +177,8 @@ func parse(fullpath string) ([]byte, *Content, error) { if err != nil { return nil, nil, err } + defer file.Close() + page, err := ReadFrom(file) if err != nil { return nil, nil, err @@ -131,7 +188,11 @@ func parse(fullpath string) ([]byte, *Content, error) { return nil, nil, err } c := NewContentFromMeta(meta) - return append(page.FrontMatter(), page.Content()...), c, nil + body := page.FrontMatter() + body = append(body, '\n') + body = append(body, page.Content()...) + + return body, c, nil } func destinationPath(file *File, pattern string) error {