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.

115 lines
2.6 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. // HTMLRenderer implements the macaron handler for serving HTML templates.
  20. func HTMLRenderer() 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. // JSONRenderer implements the macaron handler for serving JSON templates.
  30. func JSONRenderer() macaron.Handler {
  31. return macaron.Renderer(macaron.RenderOptions{
  32. Funcs: NewFuncMap(),
  33. Directory: path.Join(setting.StaticRootPath, "templates"),
  34. AppendDirectories: []string{
  35. path.Join(setting.CustomPath, "templates"),
  36. },
  37. HTMLContentType: "application/json",
  38. })
  39. }
  40. // Mailer provides the templates required for sending notification mails.
  41. func Mailer() *template.Template {
  42. for _, funcs := range NewFuncMap() {
  43. templates.Funcs(funcs)
  44. }
  45. staticDir := path.Join(setting.StaticRootPath, "templates", "mail")
  46. if com.IsDir(staticDir) {
  47. files, err := com.StatDir(staticDir)
  48. if err != nil {
  49. log.Warn("Failed to read %s templates dir. %v", staticDir, err)
  50. } else {
  51. for _, filePath := range files {
  52. if !strings.HasSuffix(filePath, ".tmpl") {
  53. continue
  54. }
  55. content, err := ioutil.ReadFile(path.Join(staticDir, filePath))
  56. if err != nil {
  57. log.Warn("Failed to read static %s template. %v", filePath, err)
  58. continue
  59. }
  60. templates.New(
  61. strings.TrimSuffix(
  62. filePath,
  63. ".tmpl",
  64. ),
  65. ).Parse(string(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. templates.New(
  85. strings.TrimSuffix(
  86. filePath,
  87. ".tmpl",
  88. ),
  89. ).Parse(string(content))
  90. }
  91. }
  92. }
  93. return templates
  94. }