From ae246fa466a27f3ce65e491d0c2d546c78df7efc Mon Sep 17 00:00:00 2001 From: Stephen Searles Date: Tue, 4 Jul 2017 22:36:40 -0700 Subject: [PATCH] folding hugolib into the plugin. adding a richer list of content pages to /author --- caddyhugo.go | 47 +++++++++++++++++++++++++++++++++++---- templates.go | 63 +++++++++++++++++++--------------------------------- 2 files changed, 66 insertions(+), 44 deletions(-) diff --git a/caddyhugo.go b/caddyhugo.go index f56bbfc..969b993 100644 --- a/caddyhugo.go +++ b/caddyhugo.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} } diff --git a/templates.go b/templates.go index 988c736..47d8b87 100644 --- a/templates.go +++ b/templates.go @@ -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 = ` +{{ $timeFormat := "Jan _2 15:04:05" }}

Create content:

@@ -425,10 +408,10 @@ var AuthorPage = `

Edit content:

-