Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

static.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "bytes"
  8. "fmt"
  9. "html/template"
  10. "io"
  11. "io/ioutil"
  12. "path"
  13. "strings"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. "github.com/Unknwon/com"
  17. "gopkg.in/macaron.v1"
  18. )
  19. var (
  20. templates = template.New("")
  21. )
  22. type templateFileSystem struct {
  23. files []macaron.TemplateFile
  24. }
  25. func (templates templateFileSystem) ListFiles() []macaron.TemplateFile {
  26. return templates.files
  27. }
  28. func (templates templateFileSystem) Get(name string) (io.Reader, error) {
  29. for i := range templates.files {
  30. if templates.files[i].Name()+templates.files[i].Ext() == name {
  31. return bytes.NewReader(templates.files[i].Data()), nil
  32. }
  33. }
  34. return nil, fmt.Errorf("file '%s' not found", name)
  35. }
  36. // Renderer implements the macaron handler for serving the templates.
  37. func Renderer() macaron.Handler {
  38. fs := templateFileSystem{}
  39. fs.files = make([]macaron.TemplateFile, 0, 10)
  40. for _, assetPath := range AssetNames() {
  41. if strings.HasPrefix(assetPath, "mail/") {
  42. continue
  43. }
  44. if !strings.HasSuffix(assetPath, ".tmpl") {
  45. continue
  46. }
  47. content, err := Asset(assetPath)
  48. if err != nil {
  49. log.Warn("Failed to read embedded %s template. %v", assetPath, err)
  50. continue
  51. }
  52. fs.files = append(fs.files, macaron.NewTplFile(
  53. strings.TrimSuffix(
  54. assetPath,
  55. ".tmpl",
  56. ),
  57. content,
  58. ".tmpl",
  59. ))
  60. }
  61. customDir := path.Join(setting.CustomPath, "templates")
  62. if com.IsDir(customDir) {
  63. files, err := com.StatDir(customDir)
  64. if err != nil {
  65. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  66. } else {
  67. for _, filePath := range files {
  68. if strings.HasPrefix(filePath, "mail/") {
  69. continue
  70. }
  71. if !strings.HasSuffix(filePath, ".tmpl") {
  72. continue
  73. }
  74. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  75. if err != nil {
  76. log.Warn("Failed to read custom %s template. %v", filePath, err)
  77. continue
  78. }
  79. fs.files = append(fs.files, macaron.NewTplFile(
  80. strings.TrimSuffix(
  81. filePath,
  82. ".tmpl",
  83. ),
  84. content,
  85. ".tmpl",
  86. ))
  87. }
  88. }
  89. }
  90. return macaron.Renderer(macaron.RenderOptions{
  91. Funcs: NewFuncMap(),
  92. TemplateFileSystem: fs,
  93. })
  94. }
  95. // Mailer provides the templates required for sending notification mails.
  96. func Mailer() *template.Template {
  97. for _, funcs := range NewFuncMap() {
  98. templates.Funcs(funcs)
  99. }
  100. for _, assetPath := range AssetNames() {
  101. if !strings.HasPrefix(assetPath, "mail/") {
  102. continue
  103. }
  104. if !strings.HasSuffix(assetPath, ".tmpl") {
  105. continue
  106. }
  107. content, err := Asset(assetPath)
  108. if err != nil {
  109. log.Warn("Failed to read embedded %s template. %v", assetPath, err)
  110. continue
  111. }
  112. templates.New(
  113. strings.TrimPrefix(
  114. strings.TrimSuffix(
  115. assetPath,
  116. ".tmpl",
  117. ),
  118. "mail/",
  119. ),
  120. ).Parse(string(content))
  121. }
  122. customDir := path.Join(setting.CustomPath, "templates", "mail")
  123. if com.IsDir(customDir) {
  124. files, err := com.StatDir(customDir)
  125. if err != nil {
  126. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  127. } else {
  128. for _, filePath := range files {
  129. if !strings.HasSuffix(filePath, ".tmpl") {
  130. continue
  131. }
  132. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  133. if err != nil {
  134. log.Warn("Failed to read custom %s template. %v", filePath, err)
  135. continue
  136. }
  137. templates.New(
  138. strings.TrimSuffix(
  139. filePath,
  140. ".tmpl",
  141. ),
  142. ).Parse(string(content))
  143. }
  144. }
  145. }
  146. return templates
  147. }