improve readability
This commit is contained in:
parent
0f4f874b9e
commit
fed9f01b82
5 changed files with 114 additions and 60 deletions
28
file_tree.go
28
file_tree.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
@ -30,22 +31,24 @@ type File struct {
|
|||
func parsePage(fullpath string) (hugo.Page, error) {
|
||||
file, err := os.Open(fullpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("file open: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
page, err := hugo.ReadFrom(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("hugo read from: %w", err)
|
||||
}
|
||||
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func parseMetadata(page hugo.Page) (*hugo.PageMetadata, error) {
|
||||
meta, err := page.Metadata()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("page metadata: %w", err)
|
||||
}
|
||||
|
||||
c := NewContentFromMeta(meta)
|
||||
|
||||
return c, nil
|
||||
|
@ -54,18 +57,23 @@ func parseMetadata(page hugo.Page) (*hugo.PageMetadata, error) {
|
|||
func destinationPath(file *File, pattern string) error {
|
||||
p, err := parsePage(file.Source)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("parse page: %w", err)
|
||||
}
|
||||
|
||||
// create content
|
||||
c, err := parseMetadata(p)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse metadata: %w", err)
|
||||
}
|
||||
|
||||
c.Filepath = file.Name
|
||||
|
||||
if file.Parent != "." {
|
||||
link, err := hugo.PathPattern(pattern).Expand(c)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("hugo pathpattern: %w", err)
|
||||
}
|
||||
|
||||
file.Destination = link
|
||||
} else {
|
||||
file.Destination = strings.TrimLeft(file.Name, "_")
|
||||
|
@ -80,7 +88,8 @@ func destinationPath(file *File, pattern string) error {
|
|||
|
||||
func collectFiles(fullpath string, filechan chan File) error {
|
||||
defer close(filechan)
|
||||
return filepath.Walk(fullpath,
|
||||
|
||||
err := filepath.Walk(fullpath,
|
||||
func(p string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -91,7 +100,7 @@ func collectFiles(fullpath string, filechan chan File) error {
|
|||
|
||||
rel, err := filepath.Rel(fullpath, p)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("rel path: %w", err)
|
||||
}
|
||||
|
||||
filename := info.Name()
|
||||
|
@ -109,4 +118,9 @@ func collectFiles(fullpath string, filechan chan File) error {
|
|||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("filetree walk: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
76
main.go
76
main.go
|
@ -4,10 +4,8 @@ import (
|
|||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gohugoio/hugo/config"
|
||||
|
@ -67,6 +65,7 @@ func main() {
|
|||
if ok {
|
||||
return format
|
||||
}
|
||||
|
||||
return defaultPermalinkFormat
|
||||
}
|
||||
|
||||
|
@ -78,8 +77,10 @@ func main() {
|
|||
|
||||
// for each file, get destination path, switch file extension, remove underscore for index
|
||||
var tree FileTree
|
||||
|
||||
for file := range files {
|
||||
pattern := linkpattern(file.Parent)
|
||||
|
||||
err := destinationPath(&file, pattern)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to derive destination for %v error: %v", file.Source, err)
|
||||
|
@ -87,15 +88,21 @@ func main() {
|
|||
|
||||
if file.Draft && !buildDrafts {
|
||||
fmt.Printf("skipping draft %s (%dbytes)\n", file.Source, len(file.Body))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
tree.Files = append(tree.Files, file)
|
||||
}
|
||||
|
||||
// call proc and pipe content through it, catch output of proc
|
||||
for i, file := range tree.Files {
|
||||
buf := bytes.NewReader(file.Body)
|
||||
out := pipe(pipecmd, buf)
|
||||
|
||||
out, err := pipe(pipecmd, buf)
|
||||
if err != nil {
|
||||
log.Fatalf("pipe command '%v' failed with %v", pipecmd, err)
|
||||
}
|
||||
|
||||
// write to source
|
||||
tree.Files[i].NewBody = out
|
||||
|
@ -133,8 +140,8 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatalf("cannot write file %s, error: %v", fullpath, err)
|
||||
}
|
||||
newfile.Close()
|
||||
|
||||
newfile.Close()
|
||||
fmt.Printf("written %s (%dbytes)\n", fullpath, n)
|
||||
}
|
||||
|
||||
|
@ -145,21 +152,21 @@ func main() {
|
|||
|
||||
// aggregate sections and section entries
|
||||
sections := make(map[string]*Section)
|
||||
|
||||
for _, file := range tree.Files {
|
||||
// not a section
|
||||
if file.Parent == "." {
|
||||
continue
|
||||
}
|
||||
|
||||
name := file.Parent
|
||||
sectionFile := filepath.Join(destination, file.Parent, "index."+ext)
|
||||
|
||||
//fmt.Printf("section %v\n", sectionFile)
|
||||
link := file.Destination
|
||||
if uglyURLs {
|
||||
link += "." + ext
|
||||
}
|
||||
|
||||
//fmt.Printf("link %v\n", link)
|
||||
if _, ok := sections[name]; !ok {
|
||||
sections[name] = &Section{File: sectionFile}
|
||||
}
|
||||
|
@ -175,57 +182,24 @@ func main() {
|
|||
for name, section := range sections {
|
||||
// TODO: come up with sth better as one might have content there.
|
||||
os.Remove(section.File)
|
||||
section.Write(section.File)
|
||||
|
||||
err := section.Write(section.File)
|
||||
if err != nil {
|
||||
log.Fatalf("cannot write file %s, error: %v", section.File, err)
|
||||
}
|
||||
|
||||
fmt.Printf("written section listing %s to %s\n", name, section.File)
|
||||
}
|
||||
|
||||
section, ok := sections[seconOnRoot]
|
||||
if ok {
|
||||
sectionFile := filepath.Join(destination, "index."+ext)
|
||||
section.Write(sectionFile)
|
||||
|
||||
err := section.Write(sectionFile)
|
||||
if err != nil {
|
||||
log.Fatalf("cannot append to file %s, error: %v", sectionFile, err)
|
||||
}
|
||||
|
||||
fmt.Printf("written section listing for root to %s\n", section.File)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -32,8 +32,8 @@ func dateFromInterface(input interface{}) time.Time {
|
|||
str, ok := input.(string)
|
||||
if !ok {
|
||||
return time.Now()
|
||||
|
||||
}
|
||||
|
||||
t, err := time.Parse(time.RFC3339, str)
|
||||
if err != nil {
|
||||
// try just date, or give up
|
||||
|
@ -41,8 +41,10 @@ func dateFromInterface(input interface{}) time.Time {
|
|||
if err != nil {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
|
@ -53,7 +55,9 @@ func stringArrayFromInterface(input interface{}) []string {
|
|||
for _, str := range strarr {
|
||||
out = append(out, stringFromInterface(str))
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
59
pipe.go
Normal file
59
pipe.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func pipe(cmd string, input io.Reader) ([]byte, error) {
|
||||
extpipe := exec.Command(cmd)
|
||||
extpipe.Stdin = input
|
||||
|
||||
var pipeout bytes.Buffer
|
||||
extpipe.Stdout = &pipeout
|
||||
|
||||
if err := extpipe.Start(); err != nil {
|
||||
return nil, fmt.Errorf("pipe start: %w", err)
|
||||
}
|
||||
|
||||
if err := extpipe.Wait(); err != nil {
|
||||
return nil, fmt.Errorf("pipe wait: %w", err)
|
||||
}
|
||||
|
||||
return pipeout.Bytes(), nil
|
||||
}
|
||||
|
||||
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, fmt.Errorf("mkdir: %w", err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
|
@ -27,6 +27,7 @@ func (section *Section) Write(file string) error {
|
|||
})
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
for _, file := range section.List {
|
||||
// TODO: this could be a template
|
||||
entry := "\n"
|
||||
|
@ -38,9 +39,11 @@ func (section *Section) Write(file string) error {
|
|||
|
||||
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("open file: %w", err)
|
||||
}
|
||||
|
||||
_, err = f.Write(buf.Bytes())
|
||||
f.Close()
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue