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.

content.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package caddyhugo
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path"
  7. "path/filepath"
  8. "sort"
  9. "time"
  10. "github.com/gohugoio/hugo/hugolib"
  11. )
  12. type Content struct {
  13. Filename string
  14. Modtime time.Time
  15. Metadata *Metadata
  16. }
  17. type Metadata struct {
  18. Title string
  19. Path string
  20. Date, Lastmod time.Time
  21. }
  22. // GetContent fetches the list of available content from the site directory. If
  23. // possible, the return value will be enriched with metadata from Hugo.
  24. func GetContent(siteRoot string, sites *hugolib.HugoSites) ([]Content, error) {
  25. var files = []Content{}
  26. err := filepath.Walk(path.Join(siteRoot, "content"), func(name string, fi os.FileInfo, err error) error {
  27. if err != nil {
  28. return err
  29. }
  30. if fi.IsDir() {
  31. return nil
  32. }
  33. name, err = filepath.Rel(siteRoot, name)
  34. if err != nil {
  35. return err
  36. }
  37. files = append(files, Content{
  38. Filename: name,
  39. Modtime: fi.ModTime(),
  40. })
  41. return nil
  42. })
  43. sort.Slice(files, func(i, j int) bool {
  44. return files[i].Modtime.Before(files[j].Modtime)
  45. })
  46. if err != nil {
  47. fmt.Println(err)
  48. return nil, err
  49. }
  50. for i, file := range files {
  51. fn := path.Join(siteRoot, file.Filename)
  52. page := sites.GetContentPage(fn)
  53. if page != nil {
  54. files[i].Metadata = &Metadata{
  55. Title: page.Title(),
  56. Path: file.Filename,
  57. Date: page.Date,
  58. Lastmod: page.Lastmod,
  59. }
  60. }
  61. }
  62. return files, nil
  63. }
  64. // NewContent initializes new content with the name (title) and content type.
  65. // If ctype is empty string, "default" is used. The return value is the filename,
  66. // which may be modified from the title and includes the content type if other than
  67. // default, but does not include the full directory relative to the site.
  68. func (ch *CaddyHugo) NewContent(name, ctype string) (string, error) {
  69. if filepath.Ext(name) != ".md" {
  70. name += ".md"
  71. }
  72. if ctype == "" {
  73. ctype = "default"
  74. }
  75. name = docname(name)
  76. filename := path.Join(ctype, name)
  77. if ctype == "default" {
  78. filename = name
  79. }
  80. _, err := os.Stat(path.Join(ch.Dir, "content", filename))
  81. if os.IsNotExist(err) {
  82. cmd := exec.Command("hugo", "new", filename)
  83. cmd.Dir = ch.Dir
  84. out, err := cmd.CombinedOutput()
  85. if err != nil {
  86. return filename, fmt.Errorf("error running 'hugo new': %v; %v", err, string(out))
  87. }
  88. }
  89. ch.Build()
  90. return filename, nil
  91. }