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.

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