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

73
main.go
View file

@ -1,10 +1,12 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"log" "log"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -82,8 +84,12 @@ func main() {
return defaultPermalinkFormat return defaultPermalinkFormat
} }
// iterate through file tree source
files := make(chan File) files := make(chan File)
go collectFiles(source, files) go collectFiles(source, files)
// for each file, get destination path, switch file extension, remove underscore for index
var tree FileTree
for file := range files { for file := range files {
pattern := linkcfg(file.Parent) pattern := linkcfg(file.Parent)
err := destinationPath(&file, pattern) err := destinationPath(&file, pattern)
@ -91,19 +97,67 @@ func main() {
fmt.Println(err, file) fmt.Println(err, file)
continue 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? // replace links?
// write rss? // write rss?
// write listings from template? // write listings from template?
} }
type FileTree struct { type FileTree struct {
Files map[string]*File Files []File
} }
type File struct { type File struct {
@ -115,6 +169,7 @@ type File struct {
Extension string Extension string
Body []byte Body []byte
NewBody []byte
} }
func parse(fullpath string) ([]byte, *Content, error) { func parse(fullpath string) ([]byte, *Content, error) {
@ -122,6 +177,8 @@ func parse(fullpath string) ([]byte, *Content, error) {
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
defer file.Close()
page, err := ReadFrom(file) page, err := ReadFrom(file)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -131,7 +188,11 @@ func parse(fullpath string) ([]byte, *Content, error) {
return nil, nil, err return nil, nil, err
} }
c := NewContentFromMeta(meta) 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 { func destinationPath(file *File, pattern string) error {