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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. templates.New(
  72. strings.TrimSuffix(
  73. filePath,
  74. ".tmpl",
  75. ),
  76. ).Parse(string(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. templates.New(
  96. strings.TrimSuffix(
  97. filePath,
  98. ".tmpl",
  99. ),
  100. ).Parse(string(content))
  101. }
  102. }
  103. }
  104. return templates
  105. }