package caddyhugo import ( "fmt" "os" "os/exec" "path" "path/filepath" "sort" "strings" "time" "github.com/gohugoio/hugo/hugolib" ) type Content struct { Filename string Modtime time.Time Metadata *Metadata } type Metadata struct { Title string Path string Date, Lastmod time.Time } func ListContent() ([]Content, error) { return nil, nil } func GetContent(siteRoot string, sites *hugolib.HugoSites) ([]Content, error) { var files = []Content{} err := filepath.Walk(path.Join(siteRoot, "content"), func(name string, fi os.FileInfo, err error) error { if err != nil { return err } if fi.IsDir() { return nil } name, err = filepath.Rel(siteRoot, name) if err != nil { return err } files = append(files, Content{ Filename: name, Modtime: fi.ModTime(), }) return nil }) sort.Slice(files, func(i, j int) bool { return files[i].Modtime.Before(files[j].Modtime) }) if err != nil { fmt.Println(err) return nil, err } pages := sites.Pages() for _, page := range pages { if page.Kind == hugolib.KindPage { for i, file := range files { path := path.Join("content", page.Source.Path()) if file.Filename == path { files[i].Metadata = &Metadata{ Title: page.Title, Path: path, Date: page.Date, Lastmod: page.Lastmod, } break } } } } return files, nil } func (ch CaddyHugo) NewContent(name, ctype string) (string, error) { if filepath.Ext(name) != ".md" { name += ".md" } if ctype == "" { ctype = "default" } filename := path.Join(ctype, strings.ToLower(name)) if ctype == "default" { filename = strings.ToLower(name) } _, err := os.Stat(path.Join(ch.Dir, "content", filename)) if os.IsNotExist(err) { cmd := exec.Command("hugo", "new", filename) cmd.Dir = ch.Dir out, err := cmd.CombinedOutput() if err != nil { return filename, fmt.Errorf("error running 'hugo new': %v; %v", err, string(out)) return filename, err } } return filename, nil }