content.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package main
  2. import (
  3. "errors"
  4. "github.com/spf13/cast"
  5. "github.com/spf13/hugo/hugolib"
  6. "github.com/spf13/hugo/parser"
  7. "os"
  8. )
  9. type (
  10. Frontmatter map[string]interface{}
  11. Content struct {
  12. Path string
  13. Metadata Frontmatter
  14. Body string
  15. }
  16. Page struct {
  17. Path string
  18. Body string
  19. }
  20. )
  21. func NewPage() *Page {
  22. return &Page{}
  23. }
  24. func (c *Content) contentSave() error {
  25. page, err := hugolib.NewPage(c.Path)
  26. if err != nil {
  27. return err
  28. }
  29. page.SetSourceMetaData(c.Metadata, '+')
  30. page.SetSourceContent([]byte(c.Body))
  31. return page.SafeSaveSourceAs(c.Path)
  32. }
  33. type PageManager interface {
  34. Read(fp string) (*Content, error)
  35. Create(fp string, fm Frontmatter, content []byte) (*Content, error)
  36. Update(fp string, fm Frontmatter, content []byte) (*Content, error)
  37. Delete(fp string) error
  38. }
  39. func (p Page) contentRead(filename string) (*Content, error) {
  40. file, err := os.Open(filename)
  41. if err != nil {
  42. return nil, err
  43. }
  44. defer file.Close()
  45. parser, err := parser.ReadFrom(file)
  46. if err != nil {
  47. return nil, err
  48. }
  49. rawdata, err := parser.Metadata()
  50. if err != nil {
  51. return nil, err
  52. }
  53. metadata, err := cast.ToStringMapE(rawdata)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return &Content{
  58. Path: filename,
  59. Metadata: metadata,
  60. Body: string(parser.Content()),
  61. }, nil
  62. }
  63. func (p Page) contentCreate(fp string, fm Frontmatter, content []byte) (*Content, error) {
  64. // create a new page
  65. page := &Content{
  66. Path: fp,
  67. Metadata: fm,
  68. Body: string(content),
  69. }
  70. // save page to disk
  71. err := page.contentSave()
  72. if err != nil {
  73. return nil, err
  74. }
  75. return page, nil
  76. }
  77. // UpdatePage changes the content of an existing page
  78. func (p Page) contentUpdate(fp string, fm Frontmatter, content []byte) (*Content, error) {
  79. // delete existing page
  80. err := p.contentDelete(fp)
  81. if err != nil {
  82. return nil, err
  83. }
  84. // create a new page
  85. page := &Content{
  86. Path: fp,
  87. Metadata: fm,
  88. Body: string(content),
  89. }
  90. // save page to disk
  91. err = page.contentSave()
  92. if err != nil {
  93. return nil, err
  94. }
  95. return page, nil
  96. }
  97. func (p Page) contentDelete(fp string) error {
  98. // check that file exists
  99. info, err := os.Stat(fp)
  100. if err != nil {
  101. return err
  102. }
  103. // that file is a directory
  104. if info.IsDir() {
  105. return errors.New("DeletePage cannot delete directories")
  106. }
  107. // remove the directory
  108. return os.Remove(fp)
  109. }
  110. func getTitle(fm Frontmatter) (string, error) {
  111. // check that title has been specified
  112. t, ok := fm["title"]
  113. if ok == false {
  114. return "", errors.New("page[meta].title must be specified")
  115. }
  116. // check that title is a string
  117. title, ok := t.(string)
  118. if ok == false {
  119. return "", errors.New("page[meta].title must be a string")
  120. }
  121. return title, nil
  122. }