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.

static.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // +build bindata
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package templates
  6. import (
  7. "html/template"
  8. "io/ioutil"
  9. "path"
  10. "strings"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/Unknwon/com"
  14. "github.com/go-macaron/bindata"
  15. "gopkg.in/macaron.v1"
  16. )
  17. var (
  18. templates = template.New("")
  19. )
  20. // Renderer implements the macaron handler for serving the templates.
  21. func Renderer() macaron.Handler {
  22. return macaron.Renderer(macaron.RenderOptions{
  23. Funcs: NewFuncMap(),
  24. AppendDirectories: []string{
  25. path.Join(setting.CustomPath, "templates"),
  26. },
  27. TemplateFileSystem: bindata.Templates(
  28. bindata.Options{
  29. Asset: Asset,
  30. AssetDir: AssetDir,
  31. AssetInfo: AssetInfo,
  32. AssetNames: AssetNames,
  33. Prefix: "",
  34. },
  35. ),
  36. })
  37. }
  38. // Mailer provides the templates required for sending notification mails.
  39. func Mailer() *template.Template {
  40. for _, funcs := range NewFuncMap() {
  41. templates.Funcs(funcs)
  42. }
  43. for _, assetPath := range AssetNames() {
  44. if !strings.HasPrefix(assetPath, "mail/") {
  45. continue
  46. }
  47. if !strings.HasSuffix(assetPath, ".tmpl") {
  48. continue
  49. }
  50. content, err := Asset(assetPath)
  51. if err != nil {
  52. log.Warn("Failed to read embedded %s template. %v", assetPath, err)
  53. continue
  54. }
  55. templates.New(
  56. strings.TrimPrefix(
  57. strings.TrimSuffix(
  58. assetPath,
  59. ".tmpl",
  60. ),
  61. "mail/",
  62. ),
  63. ).Parse(string(content))
  64. }
  65. customDir := path.Join(setting.CustomPath, "templates", "mail")
  66. if com.IsDir(customDir) {
  67. files, err := com.StatDir(customDir)
  68. if err != nil {
  69. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  70. } else {
  71. for _, filePath := range files {
  72. if !strings.HasSuffix(filePath, ".tmpl") {
  73. continue
  74. }
  75. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  76. if err != nil {
  77. log.Warn("Failed to read custom %s template. %v", filePath, err)
  78. continue
  79. }
  80. templates.New(
  81. strings.TrimSuffix(
  82. filePath,
  83. ".tmpl",
  84. ),
  85. ).Parse(string(content))
  86. }
  87. }
  88. }
  89. return templates
  90. }