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.

103 lines
2.2 KiB

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