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. 57
      templates.go

@ -22,6 +22,10 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver" "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 ( const (
@ -51,6 +55,8 @@ type docref struct {
type CaddyHugo struct { type CaddyHugo struct {
ServerType string ServerType string
Site *httpserver.SiteConfig Site *httpserver.SiteConfig
HugoSites *hugolib.HugoSites
HugoCfg *deps.DepsCfg
Media *MediaSource Media *MediaSource
@ -80,12 +86,38 @@ func (ch *CaddyHugo) LTime() uint64 {
return ch.ltime 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 { func (ch *CaddyHugo) Setup(c *caddy.Controller) error {
var err error
ch.docs = make(map[string]*docref) ch.docs = make(map[string]*docref)
ch.Site = httpserver.GetConfig(c) 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) ch.authorTmpl, err = template.New("").Parse(AuthorPage)
if err != nil { if err != nil {
@ -222,7 +254,8 @@ func (ch CaddyHugo) Admin() httpserver.Handler {
func (ch CaddyHugo) AuthorHome() httpserver.Handler { func (ch CaddyHugo) AuthorHome() httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { 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 { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@ -383,10 +416,13 @@ func (ch *CaddyHugo) Publish() error {
cmd := exec.Command("hugo") cmd := exec.Command("hugo")
cmd.Dir = ch.Site.Root cmd.Dir = ch.Site.Root
_, err := cmd.CombinedOutput() _, err := cmd.CombinedOutput()
if err != nil {
return err return err
} }
return nil
}
func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) {
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
ReadBufferSize: 1024, ReadBufferSize: 1024,
@ -444,7 +480,6 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int
return http.StatusBadRequest, err return http.StatusBadRequest, err
} }
fmt.Println(message)
ch.ObserveLTime(message.LTime) ch.ObserveLTime(message.LTime)
timer.Reset(idlePingShort) timer.Reset(idlePingShort)
@ -492,5 +527,9 @@ func (ch CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} {
if docref != nil { if docref != nil {
doc = docref.doc 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} return tmplData{ch.Site, r, ch, doc, docref}
} }

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

Loading…
Cancel
Save