pipe through proc and write to destination

This commit is contained in:
dre 2021-07-11 00:09:35 +08:00
parent 3744aea0be
commit 492cf2de4b

75
main.go
View file

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