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.

hugo.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package caddyhugo
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "path/filepath"
  7. "time"
  8. "github.com/gohugoio/hugo/deps"
  9. "github.com/gohugoio/hugo/hugofs"
  10. "github.com/gohugoio/hugo/hugolib"
  11. "github.com/spf13/afero"
  12. "github.com/spf13/viper"
  13. themeadditions "git.stephensearles.com/stephen/caddy-hugo2/theme-additions"
  14. "git.stephensearles.com/stephen/idleshut"
  15. )
  16. const (
  17. IdleWebsocketTimeout = 10 * time.Minute
  18. WebsocketFileTicker = 1 * time.Second
  19. )
  20. // Publish really renders new content into the public directory
  21. func (ch *CaddyHugo) Publish() error {
  22. err := ch.persistAllEdits()
  23. if err != nil {
  24. return err
  25. }
  26. err = buildSite(ch.HugoSites)
  27. if err != nil {
  28. return err
  29. }
  30. return nil
  31. }
  32. func buildSite(sites *hugolib.HugoSites) error {
  33. err := sites.Build(hugolib.BuildCfg{ResetState: true})
  34. if err != nil {
  35. return fmt.Errorf("caddy-hugo: building site: %v", err)
  36. }
  37. return err
  38. }
  39. func writeThemeFiles(dir string) error {
  40. for _, asset := range themeadditions.AssetNames() {
  41. err := os.MkdirAll(path.Join(dir, filepath.Dir(asset)), 0755)
  42. if err != nil {
  43. return err
  44. }
  45. f, err := os.Create(path.Join(dir, asset))
  46. if err != nil {
  47. return err
  48. }
  49. b, err := themeadditions.Asset(asset)
  50. if err != nil {
  51. return err
  52. }
  53. _, err = f.Write(b)
  54. if err != nil {
  55. return err
  56. }
  57. err = f.Close()
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }
  64. func (ch *CaddyHugo) configWithFs(fs afero.Fs) (*hugolib.HugoSites, *deps.DepsCfg, error) {
  65. var err error
  66. hfs := hugofs.NewFrom(fs, &viper.Viper{})
  67. cfg := &deps.DepsCfg{Fs: hfs}
  68. cfgPath := path.Join(ch.Dir, "config.toml")
  69. cfg.Cfg, _, err = hugolib.LoadConfig(hugolib.ConfigSourceDescriptor{Fs: fs, Path: cfgPath})
  70. if err != nil {
  71. return nil, cfg, fmt.Errorf("caddy-hugo: loading site configuration: %v", err)
  72. }
  73. cfg.Cfg.Set("workingDir", ch.Dir)
  74. sites, err := hugolib.NewHugoSites(*cfg)
  75. if err != nil {
  76. return nil, cfg, fmt.Errorf("caddy-hugo: initializing site: %v", err)
  77. }
  78. err = buildSite(sites)
  79. return sites, cfg, err
  80. }
  81. func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) {
  82. hugoSites, _, err := ch.configWithFs(es.tmpfs)
  83. if err != nil {
  84. return idleshut.Config{}, fmt.Errorf("caddy-hugo: loading site configuration: %v", err)
  85. }
  86. return idleshut.Config{
  87. TickDuration: WebsocketFileTicker,
  88. MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1,
  89. Stop: func() error {
  90. ch.persistEditsForSession(es)
  91. es.doc.Close()
  92. ch.mtx.Lock()
  93. defer ch.mtx.Unlock()
  94. delete(ch.docs, es.filename)
  95. return nil
  96. },
  97. TickError: func(err error) {
  98. fmt.Println("error processing draft:", err)
  99. },
  100. Tick: func() error {
  101. err := afero.WriteFile(es.tmpfs, es.filename, []byte(es.doc.Contents()), 0644)
  102. if err != nil {
  103. return err
  104. }
  105. err = buildSite(hugoSites)
  106. if err != nil {
  107. return err
  108. }
  109. return nil
  110. },
  111. }, nil
  112. }