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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // JSRenderer implements the macaron handler for serving JS templates.
  43. func JSRenderer() macaron.Handler {
  44. return macaron.Renderer(macaron.RenderOptions{
  45. Funcs: NewFuncMap(),
  46. Directory: path.Join(setting.StaticRootPath, "templates"),
  47. AppendDirectories: []string{
  48. path.Join(setting.CustomPath, "templates"),
  49. },
  50. HTMLContentType: "application/javascript",
  51. })
  52. }
  53. // Mailer provides the templates required for sending notification mails.
  54. func Mailer() (*texttmpl.Template, *template.Template) {
  55. for _, funcs := range NewTextFuncMap() {
  56. subjectTemplates.Funcs(funcs)
  57. }
  58. for _, funcs := range NewFuncMap() {
  59. bodyTemplates.Funcs(funcs)
  60. }
  61. staticDir := path.Join(setting.StaticRootPath, "templates", "mail")
  62. if com.IsDir(staticDir) {
  63. files, err := com.StatDir(staticDir)
  64. if err != nil {
  65. log.Warn("Failed to read %s templates dir. %v", staticDir, err)
  66. } else {
  67. for _, filePath := range files {
  68. if !strings.HasSuffix(filePath, ".tmpl") {
  69. continue
  70. }
  71. content, err := ioutil.ReadFile(path.Join(staticDir, filePath))
  72. if err != nil {
  73. log.Warn("Failed to read static %s template. %v", filePath, err)
  74. continue
  75. }
  76. buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
  77. }
  78. }
  79. }
  80. customDir := path.Join(setting.CustomPath, "templates", "mail")
  81. if com.IsDir(customDir) {
  82. files, err := com.StatDir(customDir)
  83. if err != nil {
  84. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  85. } else {
  86. for _, filePath := range files {
  87. if !strings.HasSuffix(filePath, ".tmpl") {
  88. continue
  89. }
  90. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  91. if err != nil {
  92. log.Warn("Failed to read custom %s template. %v", filePath, err)
  93. continue
  94. }
  95. buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, strings.TrimSuffix(filePath, ".tmpl"), content)
  96. }
  97. }
  98. }
  99. return subjectTemplates, bodyTemplates
  100. }