hugoext/file_tree.go

154 lines
2.8 KiB
Go
Raw Permalink Normal View History

package main
import (
2021-07-11 12:34:52 +03:00
"fmt"
"os"
"path"
"path/filepath"
"strings"
2024-06-08 21:27:32 +03:00
"gitrepo.ru/neonxp/hugoext/hugo"
)
type FileTree struct {
Files []File
}
type File struct {
Root string
Source string
Destination string
Parent string
Name string
Extension string
2021-07-11 07:00:30 +03:00
Draft bool
2021-07-11 07:00:30 +03:00
Metadata hugo.PageMetadata
Body []byte
NewBody []byte
}
2021-07-11 13:07:11 +03:00
func (file *File) Write(dest, newext string, uglyURLs bool) (string, error) {
outdir, outfile := targetPath(file.Destination, newext, uglyURLs)
// ensure directory exists
newdir := filepath.Join(dest, outdir)
if made, err := mkdir(newdir); err != nil {
return "", err
} else if made {
fmt.Printf("mkdir %s\n", newdir)
}
fullpath := filepath.Join(newdir, outfile)
// create file based on directory and filename
newfile, err := os.Create(fullpath)
if err != nil {
return fullpath, err
}
if _, err = newfile.Write(file.NewBody); err != nil {
return fullpath, err
}
newfile.Close()
return fullpath, nil
}
2021-07-11 07:00:30 +03:00
func parsePage(fullpath string) (hugo.Page, error) {
file, err := os.Open(fullpath)
if err != nil {
2021-07-11 12:34:52 +03:00
return nil, fmt.Errorf("file open: %w", err)
}
defer file.Close()
page, err := hugo.ReadFrom(file)
if err != nil {
2021-07-11 12:34:52 +03:00
return nil, fmt.Errorf("hugo read from: %w", err)
}
2021-07-11 12:34:52 +03:00
2021-07-11 07:00:30 +03:00
return page, nil
}
func parseMetadata(page hugo.Page) (*hugo.PageMetadata, error) {
meta, err := page.Metadata()
if err != nil {
2021-07-11 12:34:52 +03:00
return nil, fmt.Errorf("page metadata: %w", err)
}
2021-07-11 12:34:52 +03:00
c := NewContentFromMeta(meta)
2021-07-11 07:00:30 +03:00
return c, nil
}
func destinationPath(file *File, pattern string) error {
2021-07-11 07:00:30 +03:00
p, err := parsePage(file.Source)
if err != nil {
2021-07-11 12:34:52 +03:00
return fmt.Errorf("parse page: %w", err)
}
2021-07-11 07:00:30 +03:00
// create content
c, err := parseMetadata(p)
2021-07-11 12:34:52 +03:00
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 {
2021-07-11 12:34:52 +03:00
return fmt.Errorf("hugo pathpattern: %w", err)
}
2021-07-11 12:34:52 +03:00
file.Destination = link
} else {
file.Destination = strings.TrimLeft(file.Name, "_")
}
2021-07-11 07:00:30 +03:00
file.Draft = c.Draft
file.Metadata = *c
file.Body = p.Body()
return nil
}
func collectFiles(fullpath string, filechan chan File) error {
defer close(filechan)
2021-07-11 12:34:52 +03:00
err := filepath.Walk(fullpath,
func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
rel, err := filepath.Rel(fullpath, p)
if err != nil {
2021-07-11 12:34:52 +03:00
return fmt.Errorf("rel path: %w", err)
}
filename := info.Name()
ext := path.Ext(filename)
name := filename[0 : len(filename)-len(ext)]
parent := filepath.Dir(rel)
filechan <- File{
Root: fullpath,
Source: p,
Name: name,
Extension: ext,
Parent: parent,
}
return nil
})
2021-07-11 12:34:52 +03:00
if err != nil {
return fmt.Errorf("filetree walk: %w", err)
}
return nil
}