decoupling content lookups a bit; supporting both filesystem and hugolib lookups

pull/8/head
Stephen Searles 7 years ago
parent 406af7b834
commit a6ae6223eb
  1. 82
      content.go
  2. 49
      templates.go

@ -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
}

@ -8,36 +8,14 @@ import (
"path"
"path/filepath"
"strings"
"time"
"git.stephensearles.com/stephen/acedoc"
"github.com/gohugoio/hugo/hugolib"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
type Content struct {
Title string
Path string
Date, Lastmod time.Time
}
func (t tmplData) Content() ([]Content, error) {
var files []Content
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
return GetContent(t.Site.Root, t.HugoSites)
}
func (t tmplData) ContentTypes() ([]string, error) {
@ -423,8 +401,29 @@ var AuthorPage = `<html>
<p>Edit content:</p>
<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>
{{- end }}
<tr>
{{ 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>
</body>
</html>`

Loading…
Cancel
Save