decoupling content lookups a bit; supporting both filesystem and hugolib lookups
This commit is contained in:
+82
@@ -0,0 +1,82 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
+24
-25
@@ -8,36 +8,14 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.stephensearles.com/stephen/acedoc"
|
"git.stephensearles.com/stephen/acedoc"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/hugolib"
|
|
||||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Content struct {
|
|
||||||
Title string
|
|
||||||
Path string
|
|
||||||
Date, Lastmod time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t tmplData) Content() ([]Content, error) {
|
func (t tmplData) Content() ([]Content, error) {
|
||||||
var files []Content
|
return GetContent(t.Site.Root, t.HugoSites)
|
||||||
|
|
||||||
pages := t.HugoSites.Pages().ByDate()
|
|
||||||
for _, page := range pages {
|
|
||||||
if page.Kind == hugolib.KindPage {
|
|
||||||
files = append(files, Content{
|
|
||||||
Title: page.Title,
|
|
||||||
Path: path.Join("content", page.Source.Path()),
|
|
||||||
Date: page.Date,
|
|
||||||
Lastmod: page.Lastmod,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return files, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t tmplData) ContentTypes() ([]string, error) {
|
func (t tmplData) ContentTypes() ([]string, error) {
|
||||||
@@ -423,8 +401,29 @@ var AuthorPage = `<html>
|
|||||||
|
|
||||||
<p>Edit content:</p>
|
<p>Edit content:</p>
|
||||||
<table>{{ range .Content }}
|
<table>{{ range .Content }}
|
||||||
<tr><td><a href="/hugo/edit/{{ .Path }}">{{ .Title }}</a></td><td>{{ .Date.Format $timeFormat }}{{ if not (.Lastmod.Equal .Date) }} (last modified {{.Lastmod.Format $timeFormat }}){{end}}</td></tr>
|
<tr>
|
||||||
{{- end }}
|
{{ if .Metadata }}
|
||||||
|
<td>
|
||||||
|
<a href="/hugo/edit/{{ .Filename }}">
|
||||||
|
{{ .Metadata.Title }}
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{ .Metadata.Date.Format $timeFormat }}
|
||||||
|
{{ if not (.Metadata.Lastmod.Equal .Metadata.Date) }}
|
||||||
|
(last modified {{.Metadata.Lastmod.Format $timeFormat }})
|
||||||
|
{{end}}
|
||||||
|
</td>
|
||||||
|
{{ else }}
|
||||||
|
<td>
|
||||||
|
<a href="/hugo/edit/{{ .Filename }}">
|
||||||
|
{{ .Filename }}
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
|
<td>(unable to load metadata)</td>
|
||||||
|
{{ end }}
|
||||||
|
</tr>
|
||||||
|
{{- end }}
|
||||||
</table>
|
</table>
|
||||||
</body>
|
</body>
|
||||||
</html>`
|
</html>`
|
||||||
|
|||||||
Reference in New Issue
Block a user