diff --git a/caddyhugo.go b/caddyhugo.go index f7c53bf..1751bba 100644 --- a/caddyhugo.go +++ b/caddyhugo.go @@ -1,10 +1,8 @@ package caddyhugo import ( - "fmt" "html/template" "net/http" - "os/exec" "path/filepath" "strings" "sync" @@ -50,12 +48,7 @@ type CaddyHugo struct { // Build rebuilds the cached state of the site. TODO: determine if this republishes 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 + return buildSite(ch.HugoSites) } // BasePath returns the directory that the CaddyHugo internal/author pages are under @@ -72,22 +65,6 @@ func (ch *CaddyHugo) docFilename(orig string) string { return filepath.Join(ch.Dir, docname(orig)) } -// Publish really renders new content into the public directory -func (ch *CaddyHugo) Publish() error { - err := ch.persistAllEdits() - if err != nil { - return err - } - cmd := exec.Command("hugo") - cmd.Dir = ch.Dir - _, err = cmd.CombinedOutput() - if err != nil { - return err - } - - return nil -} - // TmplData collects data for template execution func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{} { var doc *acedoc.Document diff --git a/hugo.go b/hugo.go index f05d8b3..edb2a7e 100644 --- a/hugo.go +++ b/hugo.go @@ -2,11 +2,11 @@ package caddyhugo import ( "fmt" + "os/exec" "path" "time" "github.com/gohugoio/hugo/deps" - "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugolib" "github.com/spf13/afero" @@ -18,35 +18,55 @@ const ( WebsocketFileTicker = 1 * time.Second ) -type HugoInteractor interface { - Render(srcdir, workdir string) HugoRenderer -} +// Publish really renders new content into the public directory +func (ch *CaddyHugo) Publish() error { + err := ch.persistAllEdits() + if err != nil { + return err + } + cmd := exec.Command("hugo") + cmd.Dir = ch.Dir + _, err = cmd.CombinedOutput() + if err != nil { + return err + } -type HugoRenderer interface { - WriteContent(contents string) error - Start() error - Stop() error + return nil } -func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) { +func buildSite(sites *hugolib.HugoSites) error { + err := sites.Build(hugolib.BuildCfg{ResetState: true}) + if err != nil { + return fmt.Errorf("caddy-hugo: building site: %v", err) + } + return err +} + +func (ch *CaddyHugo) configWithFs(fs afero.Fs) (*hugolib.HugoSites, *deps.DepsCfg, error) { var err error - hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)} - fmt.Println(ch.Dir) - hugoCfg.Cfg, err = hugolib.LoadConfig(es.tmpfs, "", path.Join(ch.Dir, "config.toml")) + cfg := &deps.DepsCfg{} + cfgPath := path.Join(ch.Dir, "config.toml") + cfg.Cfg, err = hugolib.LoadConfig(fs, "", cfgPath) if err != nil { - return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err) + return nil, cfg, fmt.Errorf("caddy-hugo: loading site configuration: %v", err) } + cfg.Cfg.Set("workingDir", ch.Dir) - hugoCfg.Cfg.Set("workingDir", ch.Dir) - hugoSites, err := hugolib.NewHugoSites(*hugoCfg) + sites, err := hugolib.NewHugoSites(*cfg) if err != nil { - return idleshut.Config{}, fmt.Errorf("caddy-hugo: initializing site: %v", err) + return nil, cfg, fmt.Errorf("caddy-hugo: initializing site: %v", err) } - err = hugoSites.Build(hugolib.BuildCfg{ResetState: true}) + err = buildSite(sites) + + return sites, cfg, err +} + +func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) { + hugoSites, _, err := ch.configWithFs(es.tmpfs) if err != nil { - return idleshut.Config{}, fmt.Errorf("caddy-hugo: building site: %v", err) + return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err) } return idleshut.Config{ diff --git a/setup.go b/setup.go index 8505e1d..b65588e 100644 --- a/setup.go +++ b/setup.go @@ -9,9 +9,7 @@ import ( "git.stephensearles.com/stephen/caddy-hugo2/comments" "git.stephensearles.com/stephen/caddy-hugo2/media" - "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/hugofs" - "github.com/gohugoio/hugo/hugolib" "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -56,17 +54,9 @@ func (ch *CaddyHugo) Setup(dir string) error { ch.docs = make(map[string]*editSession) ch.confirmingToClient = make(map[uint64]struct{}) - ch.HugoCfg = &deps.DepsCfg{} - - ch.HugoCfg.Cfg, err = hugolib.LoadConfig(hugofs.Os, dir, "") - if err != nil { - return fmt.Errorf("error loading hugo config: %v", err) - } - - ch.HugoCfg.Cfg.Set("workingdir", dir) - ch.HugoSites, err = hugolib.NewHugoSites(*ch.HugoCfg) + ch.HugoSites, ch.HugoCfg, err = ch.configWithFs(hugofs.Os) if err != nil { - return fmt.Errorf("error intializing hugo: %v", err) + return fmt.Errorf("error setting up hugo : %v", err) } err = ch.Build()