No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

caddyhugo.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package caddyhugo
  2. import (
  3. "html/template"
  4. "net/http"
  5. "path/filepath"
  6. "strings"
  7. "sync"
  8. "git.stephensearles.com/stephen/acedoc"
  9. "git.stephensearles.com/stephen/caddy-hugo2/comments"
  10. "git.stephensearles.com/stephen/caddy-hugo2/media"
  11. "github.com/gohugoio/hugo/deps"
  12. "github.com/gohugoio/hugo/hugolib"
  13. "github.com/mholt/caddy"
  14. "github.com/mholt/caddy/caddyhttp/httpserver"
  15. "github.com/spf13/afero"
  16. )
  17. func init() {
  18. caddy.RegisterPlugin("hugo", caddy.Plugin{
  19. ServerType: "http",
  20. Action: SetupCaddy,
  21. })
  22. }
  23. // CaddyHugo implements the plugin
  24. type CaddyHugo struct {
  25. ServerType string
  26. Site *httpserver.SiteConfig
  27. HugoSites *hugolib.HugoSites
  28. HugoCfg *deps.DepsCfg
  29. Dir string
  30. Media *media.MediaSource
  31. Comments *comments.Service
  32. docs map[string]*editSession
  33. mtx sync.Mutex
  34. authorTmpl, adminTmpl, editTmpl *template.Template
  35. ltime uint64
  36. confirmingToClient map[uint64]struct{}
  37. }
  38. // Build rebuilds the cached state of the site. TODO: determine if this republishes
  39. func (ch *CaddyHugo) Build() error {
  40. return buildSite(ch.HugoSites)
  41. }
  42. // BasePath returns the directory that the CaddyHugo internal/author pages are under
  43. func (ch *CaddyHugo) BasePath() string {
  44. return "/hugo"
  45. }
  46. func docname(orig string) string {
  47. orig = strings.Replace(orig, " ", "-", -1)
  48. return strings.ToLower(orig)
  49. }
  50. func (ch *CaddyHugo) docFilename(orig string) string {
  51. return filepath.Join(ch.Dir, docname(orig))
  52. }
  53. // TmplData collects data for template execution
  54. func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{} {
  55. var doc *acedoc.Document
  56. if docref != nil {
  57. doc = docref.doc
  58. }
  59. if ch.HugoSites != nil && ch.HugoSites.Fs != nil {
  60. ch.HugoSites.Cfg.Set("buildDrafts", true)
  61. ch.HugoSites.Fs.Destination = afero.NewMemMapFs()
  62. ch.Build()
  63. }
  64. return &tmplData{ch.Site, r, ch, doc, docref}
  65. }