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