Compare commits

..

No commits in common. 'ab06bed6ec698d312de3603bb18b3eb57472e93b' and '5225628f8a6a64861ab3424e094b13b314f30cdd' have entirely different histories.

  1. 25
      caddyhugo.go
  2. 52
      hugo.go
  3. 14
      setup.go

@ -1,8 +1,10 @@
package caddyhugo package caddyhugo
import ( import (
"fmt"
"html/template" "html/template"
"net/http" "net/http"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -48,7 +50,12 @@ type CaddyHugo struct {
// Build rebuilds the cached state of the site. TODO: determine if this republishes // Build rebuilds the cached state of the site. TODO: determine if this republishes
func (ch *CaddyHugo) Build() error { func (ch *CaddyHugo) Build() error {
return buildSite(ch.HugoSites) err := ch.HugoSites.Build(hugolib.BuildCfg{ResetState: true})
if err != nil {
return fmt.Errorf("error building hugo sites: %v", err)
}
return nil
} }
// BasePath returns the directory that the CaddyHugo internal/author pages are under // BasePath returns the directory that the CaddyHugo internal/author pages are under
@ -65,6 +72,22 @@ func (ch *CaddyHugo) docFilename(orig string) string {
return filepath.Join(ch.Dir, docname(orig)) 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 // TmplData collects data for template execution
func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{} { func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{} {
var doc *acedoc.Document var doc *acedoc.Document

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/hugolib"
"github.com/spf13/afero" "github.com/spf13/afero"
@ -17,54 +18,35 @@ const (
WebsocketFileTicker = 1 * time.Second WebsocketFileTicker = 1 * time.Second
) )
// Publish really renders new content into the public directory type HugoInteractor interface {
func (ch *CaddyHugo) Publish() error { Render(srcdir, workdir string) HugoRenderer
err := ch.persistAllEdits()
if err != nil {
return err
}
err = buildSite(ch.HugoSites)
if err != nil {
return err
} }
return nil type HugoRenderer interface {
} WriteContent(contents string) error
Start() error
func buildSite(sites *hugolib.HugoSites) error { Stop() error
err := sites.Build(hugolib.BuildCfg{ResetState: true})
if err != nil {
return fmt.Errorf("caddy-hugo: building site: %v", err)
} }
return err func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) {
}
func (ch *CaddyHugo) configWithFs(fs afero.Fs) (*hugolib.HugoSites, *deps.DepsCfg, error) {
var err error var err error
cfg := &deps.DepsCfg{} hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)}
cfgPath := path.Join(ch.Dir, "config.toml") fmt.Println(ch.Dir)
cfg.Cfg, err = hugolib.LoadConfig(fs, "", cfgPath) hugoCfg.Cfg, err = hugolib.LoadConfig(es.tmpfs, "", path.Join(ch.Dir, "config.toml"))
if err != nil { if err != nil {
return nil, cfg, fmt.Errorf("caddy-hugo: loading site configuration: %v", err) return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err)
} }
cfg.Cfg.Set("workingDir", ch.Dir)
sites, err := hugolib.NewHugoSites(*cfg) hugoCfg.Cfg.Set("workingDir", ch.Dir)
hugoSites, err := hugolib.NewHugoSites(*hugoCfg)
if err != nil { if err != nil {
return nil, cfg, fmt.Errorf("caddy-hugo: initializing site: %v", err) return idleshut.Config{}, fmt.Errorf("caddy-hugo: initializing site: %v", err)
}
err = buildSite(sites)
return sites, cfg, err
} }
func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) { err = hugoSites.Build(hugolib.BuildCfg{ResetState: true})
hugoSites, _, err := ch.configWithFs(es.tmpfs)
if err != nil { if err != nil {
return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err) return idleshut.Config{}, fmt.Errorf("caddy-hugo: building site: %v", err)
} }
return idleshut.Config{ return idleshut.Config{

@ -9,7 +9,9 @@ import (
"git.stephensearles.com/stephen/caddy-hugo2/comments" "git.stephensearles.com/stephen/caddy-hugo2/comments"
"git.stephensearles.com/stephen/caddy-hugo2/media" "git.stephensearles.com/stephen/caddy-hugo2/media"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/hugolib"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
) )
@ -54,9 +56,17 @@ func (ch *CaddyHugo) Setup(dir string) error {
ch.docs = make(map[string]*editSession) ch.docs = make(map[string]*editSession)
ch.confirmingToClient = make(map[uint64]struct{}) ch.confirmingToClient = make(map[uint64]struct{})
ch.HugoSites, ch.HugoCfg, err = ch.configWithFs(hugofs.Os) 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)
if err != nil { if err != nil {
return fmt.Errorf("error setting up hugo : %v", err) return fmt.Errorf("error intializing hugo: %v", err)
} }
err = ch.Build() err = ch.Build()

Loading…
Cancel
Save