folding hugolib into the plugin. adding a richer list of content pages to /author

pull/8/head
Stephen Searles 7 years ago
parent dae9f296fa
commit ae246fa466
  1. 47
      caddyhugo.go
  2. 63
      templates.go

@ -22,6 +22,10 @@ import (
"github.com/gorilla/websocket"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/spf13/afero"
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/hugolib"
)
const (
@ -51,6 +55,8 @@ type docref struct {
type CaddyHugo struct {
ServerType string
Site *httpserver.SiteConfig
HugoSites *hugolib.HugoSites
HugoCfg *deps.DepsCfg
Media *MediaSource
@ -80,12 +86,38 @@ func (ch *CaddyHugo) LTime() uint64 {
return ch.ltime
}
func (ch *CaddyHugo) Build() error {
err := ch.HugoSites.Build(hugolib.BuildCfg{ResetState: true})
if err != nil {
return fmt.Errorf("error building hugo sites: %v", err)
}
return nil
}
func (ch *CaddyHugo) Setup(c *caddy.Controller) error {
var err error
ch.docs = make(map[string]*docref)
ch.Site = httpserver.GetConfig(c)
var err error
ch.HugoCfg = &deps.DepsCfg{}
ch.HugoCfg.Cfg, err = hugolib.LoadConfig(hugofs.Os, ch.Site.Root, "")
if err != nil {
return fmt.Errorf("error loading hugo config: %v", err)
}
ch.HugoCfg.Cfg.Set("workingdir", ch.Site.Root)
ch.HugoSites, err = hugolib.NewHugoSites(*ch.HugoCfg)
if err != nil {
return fmt.Errorf("error intializing hugo: %v", err)
}
err = ch.Build()
if err != nil {
return fmt.Errorf("error building initial hugo: %v", err)
}
ch.authorTmpl, err = template.New("").Parse(AuthorPage)
if err != nil {
@ -222,7 +254,8 @@ func (ch CaddyHugo) Admin() httpserver.Handler {
func (ch CaddyHugo) AuthorHome() httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
err := ch.authorTmpl.Execute(w, ch.TmplData(r, nil))
td := ch.TmplData(r, nil)
err := ch.authorTmpl.Execute(w, td)
if err != nil {
fmt.Println(err)
return http.StatusInternalServerError, err
@ -383,8 +416,11 @@ func (ch *CaddyHugo) Publish() error {
cmd := exec.Command("hugo")
cmd.Dir = ch.Site.Root
_, err := cmd.CombinedOutput()
if err != nil {
return err
}
return err
return nil
}
func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) {
@ -444,7 +480,6 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int
return http.StatusBadRequest, err
}
fmt.Println(message)
ch.ObserveLTime(message.LTime)
timer.Reset(idlePingShort)
@ -492,5 +527,9 @@ func (ch CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} {
if docref != nil {
doc = docref.doc
}
if ch.HugoSites != nil && ch.HugoSites.Fs != nil {
ch.HugoSites.Fs.Destination = afero.NewMemMapFs()
ch.Build()
}
return tmplData{ch.Site, r, ch, doc, docref}
}

@ -7,55 +7,37 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
"git.stephensearles.com/stephen/acedoc"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/spf13/hugo/hugolib"
)
func (t tmplData) Content() ([]string, error) {
type file struct {
name string
modtime time.Time
}
var files = []file{}
err := filepath.Walk(path.Join(t.Site.Root, "content"), func(name string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
type Content struct {
Title string
Path string
Date, Lastmod time.Time
}
name, err = filepath.Rel(t.Site.Root, name)
if err != nil {
return err
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,
})
}
files = append(files, file{name, 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
}
var filenames []string
for _, file := range files {
filenames = append(filenames, file.name)
}
return filenames, nil
return files, nil
}
func (t tmplData) ContentTypes() ([]string, error) {
@ -413,6 +395,7 @@ var AuthorPage = `<html>
<head>
</head>
<body>
{{ $timeFormat := "Jan _2 15:04:05" }}
<p>Create content:</p>
<form action="/hugo/edit/new" method="POST">
<label>Name: <input type="text" name="name" /></label>
@ -425,10 +408,10 @@ var AuthorPage = `<html>
</form>
<p>Edit content:</p>
<ul>{{ range .Content }}
<li><a href="/hugo/edit/{{ . }}">{{ . }}</a></li>
<table>{{ range .Content }}
<tr><td><a href="/hugo/edit/{{ .Path }}">{{ .Title }}</a></td><td>{{ .Date.Format $timeFormat }} (last modified {{ .Lastmod.Format $timeFormat }})</td></tr>
{{- end }}
</ul>
</table>
</body>
</html>`

Loading…
Cancel
Save