isolate and split out more functions
This commit is contained in:
parent
fe3351a2c9
commit
77d963a3a9
1 changed files with 67 additions and 39 deletions
106
main.go
106
main.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -24,11 +25,11 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var ext, pipe, source, destination, cfgPath string
|
var ext, pipecmd, source, destination, cfgPath string
|
||||||
var noSectionList bool
|
var noSectionList bool
|
||||||
|
|
||||||
flag.StringVar(&ext, "ext", defaultExt, "ext to look for templates in ./layout")
|
flag.StringVar(&ext, "ext", defaultExt, "ext to look for templates in ./layout")
|
||||||
flag.StringVar(&pipe, "pipe", defaultProcessor, "pipe markdown to this program for content processing")
|
flag.StringVar(&pipecmd, "pipe", defaultProcessor, "pipe markdown to this program for content processing")
|
||||||
flag.StringVar(&source, "source", defaultSource, "source directory")
|
flag.StringVar(&source, "source", defaultSource, "source directory")
|
||||||
flag.StringVar(&destination, "destination", defaultDestination, "output directory")
|
flag.StringVar(&destination, "destination", defaultDestination, "output directory")
|
||||||
flag.StringVar(&cfgPath, "config", defaultConfigPath, "hugo config path")
|
flag.StringVar(&cfgPath, "config", defaultConfigPath, "hugo config path")
|
||||||
|
@ -36,7 +37,7 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// what are we doing
|
// what are we doing
|
||||||
fmt.Printf("hugoext: converting hugo markdown to %v with %v\n", ext, pipe)
|
fmt.Printf("hugoext: converting hugo markdown to %v with %v\n", ext, pipecmd)
|
||||||
|
|
||||||
// config
|
// config
|
||||||
cfg, err := config.FromFile(afero.NewOsFs(), "config.toml")
|
cfg, err := config.FromFile(afero.NewOsFs(), "config.toml")
|
||||||
|
@ -79,10 +80,9 @@ func main() {
|
||||||
pattern := linkpattern(file.Parent)
|
pattern := linkpattern(file.Parent)
|
||||||
err := destinationPath(&file, pattern)
|
err := destinationPath(&file, pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err, file)
|
log.Fatalf("failed to derive destination for %v error: %v", file.Source, err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
//fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
|
|
||||||
if file.Draft && !buildDrafts {
|
if file.Draft && !buildDrafts {
|
||||||
fmt.Printf("skipping draft %s (%dbytes)\n", file.Source, len(file.Body))
|
fmt.Printf("skipping draft %s (%dbytes)\n", file.Source, len(file.Body))
|
||||||
continue
|
continue
|
||||||
|
@ -92,55 +92,36 @@ func main() {
|
||||||
|
|
||||||
// call proc and pipe content through it, catch output of proc
|
// call proc and pipe content through it, catch output of proc
|
||||||
for i, file := range tree.Files {
|
for i, file := range tree.Files {
|
||||||
// fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
|
|
||||||
extpipe := exec.Command(pipe)
|
|
||||||
buf := bytes.NewReader(file.Body)
|
buf := bytes.NewReader(file.Body)
|
||||||
extpipe.Stdin = buf
|
out := pipe(pipecmd, buf)
|
||||||
|
|
||||||
var procout bytes.Buffer
|
|
||||||
extpipe.Stdout = &procout
|
|
||||||
|
|
||||||
extpipe.Start()
|
|
||||||
extpipe.Wait()
|
|
||||||
|
|
||||||
// write to source
|
// write to source
|
||||||
tree.Files[i].NewBody = procout.Bytes()
|
tree.Files[i].NewBody = out
|
||||||
fmt.Printf("processed %s (%dbytes)\n", file.Source, len(tree.Files[i].Body))
|
fmt.Printf("processed %s (%dbytes)\n", file.Source, len(tree.Files[i].Body))
|
||||||
}
|
}
|
||||||
|
|
||||||
newpath := filepath.Join(".", "public")
|
newdir := filepath.Join(".", "public")
|
||||||
err = os.MkdirAll(newpath, os.ModePerm)
|
if made, err := mkdir(newdir); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
} else if made {
|
||||||
|
fmt.Printf("mkdir %s\n", newdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to destination
|
// write to destination
|
||||||
for _, file := range tree.Files {
|
for _, file := range tree.Files {
|
||||||
//fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
|
outdir, outfile := targetPath(file.Destination, ext, uglyURLs)
|
||||||
|
|
||||||
outfile := "index." + ext
|
|
||||||
outdir := file.Destination
|
|
||||||
|
|
||||||
if uglyURLs && file.Destination != "index" {
|
|
||||||
// make the last element in destination the file
|
|
||||||
outfile = filepath.Base(file.Destination) + "." + ext
|
|
||||||
// set the parent directory of that file to be the dir to create
|
|
||||||
outdir = filepath.Dir(file.Destination)
|
|
||||||
}
|
|
||||||
|
|
||||||
if file.Destination == "index" {
|
|
||||||
outdir = "."
|
|
||||||
}
|
|
||||||
|
|
||||||
// ensure directory exists
|
// ensure directory exists
|
||||||
newpath := filepath.Join(destination, outdir)
|
newdir := filepath.Join(destination, outdir)
|
||||||
fmt.Printf("mkdir %s\n", newpath)
|
if made, err := mkdir(newdir); err != nil {
|
||||||
if err = os.MkdirAll(newpath, os.ModePerm); err != nil {
|
log.Fatal(err)
|
||||||
log.Fatalf("cannot directory file %s, error: %v", newpath, err)
|
} else if made {
|
||||||
|
fmt.Printf("mkdir %s\n", newdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fullpath := filepath.Join(newdir, outfile)
|
||||||
|
|
||||||
// create file based on directory and filename
|
// create file based on directory and filename
|
||||||
fullpath := filepath.Join(newpath, outfile)
|
|
||||||
newfile, err := os.Create(fullpath)
|
newfile, err := os.Create(fullpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("cannot create file %s, error: %v", fullpath, err)
|
log.Fatalf("cannot create file %s, error: %v", fullpath, err)
|
||||||
|
@ -155,6 +136,10 @@ func main() {
|
||||||
fmt.Printf("written %s (%dbytes)\n", fullpath, n)
|
fmt.Printf("written %s (%dbytes)\n", fullpath, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, file := range tree.Files {
|
||||||
|
fmt.Printf("section %v\n", file.Parent)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
//
|
//
|
||||||
// append section listings
|
// append section listings
|
||||||
|
@ -167,3 +152,46 @@ func main() {
|
||||||
// write rss?
|
// write rss?
|
||||||
// write listings from template?
|
// write listings from template?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pipe(cmd string, input io.Reader) []byte {
|
||||||
|
extpipe := exec.Command(cmd)
|
||||||
|
extpipe.Stdin = input
|
||||||
|
|
||||||
|
var pipeout bytes.Buffer
|
||||||
|
extpipe.Stdout = &pipeout
|
||||||
|
|
||||||
|
extpipe.Start()
|
||||||
|
extpipe.Wait()
|
||||||
|
|
||||||
|
return pipeout.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func targetPath(dest, newext string, uglyURLs bool) (dir string, filename string) {
|
||||||
|
filename = "index." + newext
|
||||||
|
dir = dest
|
||||||
|
|
||||||
|
if uglyURLs && dest != "index" {
|
||||||
|
// make the last element in destination the file
|
||||||
|
filename = filepath.Base(dest) + "." + newext
|
||||||
|
// set the parent directory of that file to be the dir to create
|
||||||
|
dir = filepath.Dir(dest)
|
||||||
|
}
|
||||||
|
|
||||||
|
if dest == "index" {
|
||||||
|
dir = "."
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func mkdir(dir string) (bool, error) {
|
||||||
|
// skip if this exists
|
||||||
|
if _, err := os.Stat(dir); err == nil {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue