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.

62 lines
1.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "hash/crc32"
  5. "path/filepath"
  6. "regexp"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "github.com/PumpkinSeed/cage"
  11. "github.com/gohugoio/hugo/commands"
  12. hg "github.com/gohugoio/hugo/common/hugo"
  13. "github.com/spf13/viper"
  14. )
  15. func hugoContext(hugo *Hugo, gitRepoPath string) {
  16. viper.SetConfigName("config")
  17. viper.SetConfigType("toml")
  18. viper.AddConfigPath(filepath.Join(hugo.SourceDir))
  19. err := viper.ReadInConfig()
  20. check(err)
  21. gitRepoPathChecksum := strconv.FormatUint(uint64(crc32.ChecksumIEEE([]byte(gitRepoPath))), 16)
  22. reg, err := regexp.Compile("[^a-zA-Z0-9]+")
  23. check(err)
  24. hugoTitle := reg.ReplaceAllString(viper.GetString("title"), "")
  25. hugo.PublicHTMLName = strings.ToLower(hugoTitle) + "-" + gitRepoPathChecksum
  26. hugo.CatalogName = viper.GetString("params.sandpointsCatalogName")
  27. hugo.CatalogPrefix = viper.GetString("params.sandpointsCatalogPrefix")
  28. hugo.DestinationDir = filepath.Join(hugo.SourceDir, "public")
  29. }
  30. func hugoRun(hugoCommands []string) []string {
  31. logs := cage.Start()
  32. hugoCommand := hugoCommands
  33. goHugo := commands.Execute(hugoCommand)
  34. cage.Stop(logs)
  35. if goHugo.Err != nil {
  36. check(goHugo.Err)
  37. runtime.Goexit()
  38. }
  39. return logs.Data
  40. }
  41. func hugoRender(hugo *Hugo, hook *Hook) string {
  42. // fmt.Println("hugo", "-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics")
  43. hugoCommands := []string{"-s", hugo.SourceDir, "-d", hugo.DestinationDir, "--templateMetrics"}
  44. if hook.Offline {
  45. hugoCommands = append(hugoCommands, []string{"-e", "offline"}...)
  46. } else if hook.Context == "PostReceive" {
  47. hugoCommands = append(hugoCommands, []string{"-e", "gitea"}...)
  48. }
  49. logs := hugoRun(hugoCommands)
  50. lgs := fmt.Sprintf("~~~~~~~ Hugo's logs (v%.2f)~~~~~~~\n", hg.CurrentVersion.Number)
  51. for _, l := range logs {
  52. lgs = lgs + l + "\n"
  53. }
  54. return lgs
  55. }