You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
caddy-hugo2/content.go

82 lines
1.4 KiB

package caddyhugo
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"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
}