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.

dynamic.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. texttmpl "text/template"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "gitea.com/macaron/macaron"
  15. "github.com/unknwon/com"
  16. )
  17. var (
  18. subjectTemplates = texttmpl.New("")
  19. bodyTemplates = template.New("")
  20. )
  21. // HTMLRenderer implements the macaron handler for serving HTML templates.
  22. func HTMLRenderer() macaron.Handler {
  23. return macaron.Renderer(macaron.RenderOptions{
  24. Funcs: NewFuncMap(),
  25. Directory: path.Join(setting.StaticRootPath, "templates"),
  26. AppendDirectories: []string{
  27. path.Join(setting.CustomPath, "templates"),
  28. },
  29. })
  30. }
  31. // JSONRenderer implements the macaron handler for serving JSON templates.
  32. func JSONRenderer() macaron.Handler {
  33. return macaron.Renderer(macaron.RenderOptions{
  34. Funcs: NewFuncMap(),
  35. Directory: path.Join(setting.StaticRootPath, "templates"),
  36. AppendDirectories: []string{
  37. path.Join(setting.CustomPath, "templates"),
  38. },
  39. HTMLContentType: "application/json",
  40. })
  41. }
  42. // Mailer provides the templates required for sending notification mails.
  43. func Mailer() (*texttmpl.Template, *template.Template) {
  44. for _, funcs := range NewTextFuncMap() {
  45. subjectTemplates.Funcs(funcs)
  46. }
  47. for _, funcs := range NewFuncMap() {
  48. bodyTemplates.Funcs(funcs)
  49. }
  50. staticDir := path.Join(setting.StaticRootPath, "templates", "mail")
  51. if com.IsDir(staticDir) {
  52. files, err := com.StatDir(staticDir)
  53. if err != nil {
  54. log.Warn("Failed to read %s templates dir. %v", staticDir, err)
  55. } else {
  56. for _, filePath := range files {
  57. if !strings.HasSuffix(filePath, ".tmpl") {
  58. continue
  59. }
  60. content, err := ioutil.ReadFile(path.Join(staticDir, filePath))
  61. if err != nil {
  62. log.Warn("Failed to read static %s template. %v", filePath, err)
  63. continue
  64. }
  65. buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
  66. }
  67. }
  68. }
  69. customDir := path.Join(setting.CustomPath, "templates", "mail")
  70. if com.IsDir(customDir) {
  71. files, err := com.StatDir(customDir)
  72. if err != nil {
  73. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  74. } else {
  75. for _, filePath := range files {
  76. if !strings.HasSuffix(filePath, ".tmpl") {
  77. continue
  78. }
  79. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  80. if err != nil {
  81. log.Warn("Failed to read custom %s template. %v", filePath, err)
  82. continue
  83. }
  84. buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
  85. }
  86. }
  87. }
  88. return subjectTemplates, bodyTemplates
  89. }