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.

static.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. texttmpl "text/template"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "gitea.com/macaron/macaron"
  18. "github.com/unknwon/com"
  19. )
  20. var (
  21. subjectTemplates = texttmpl.New("")
  22. bodyTemplates = template.New("")
  23. )
  24. type templateFileSystem struct {
  25. files []macaron.TemplateFile
  26. }
  27. func (templates templateFileSystem) ListFiles() []macaron.TemplateFile {
  28. return templates.files
  29. }
  30. func (templates templateFileSystem) Get(name string) (io.Reader, error) {
  31. for i := range templates.files {
  32. if templates.files[i].Name()+templates.files[i].Ext() == name {
  33. return bytes.NewReader(templates.files[i].Data()), nil
  34. }
  35. }
  36. return nil, fmt.Errorf("file '%s' not found", name)
  37. }
  38. func NewTemplateFileSystem() templateFileSystem {
  39. fs := templateFileSystem{}
  40. fs.files = make([]macaron.TemplateFile, 0, 10)
  41. for _, assetPath := range AssetNames() {
  42. if strings.HasPrefix(assetPath, "mail/") {
  43. continue
  44. }
  45. if !strings.HasSuffix(assetPath, ".tmpl") {
  46. continue
  47. }
  48. content, err := Asset(assetPath)
  49. if err != nil {
  50. log.Warn("Failed to read embedded %s template. %v", assetPath, err)
  51. continue
  52. }
  53. fs.files = append(fs.files, macaron.NewTplFile(
  54. strings.TrimSuffix(
  55. assetPath,
  56. ".tmpl",
  57. ),
  58. content,
  59. ".tmpl",
  60. ))
  61. }
  62. customDir := path.Join(setting.CustomPath, "templates")
  63. if com.IsDir(customDir) {
  64. files, err := com.StatDir(customDir)
  65. if err != nil {
  66. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  67. } else {
  68. for _, filePath := range files {
  69. if strings.HasPrefix(filePath, "mail/") {
  70. continue
  71. }
  72. if !strings.HasSuffix(filePath, ".tmpl") {
  73. continue
  74. }
  75. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  76. if err != nil {
  77. log.Warn("Failed to read custom %s template. %v", filePath, err)
  78. continue
  79. }
  80. fs.files = append(fs.files, macaron.NewTplFile(
  81. strings.TrimSuffix(
  82. filePath,
  83. ".tmpl",
  84. ),
  85. content,
  86. ".tmpl",
  87. ))
  88. }
  89. }
  90. }
  91. return fs
  92. }
  93. // HTMLRenderer implements the macaron handler for serving HTML templates.
  94. func HTMLRenderer() macaron.Handler {
  95. return macaron.Renderer(macaron.RenderOptions{
  96. Funcs: NewFuncMap(),
  97. TemplateFileSystem: NewTemplateFileSystem(),
  98. })
  99. }
  100. // JSONRenderer implements the macaron handler for serving JSON templates.
  101. func JSONRenderer() macaron.Handler {
  102. return macaron.Renderer(macaron.RenderOptions{
  103. Funcs: NewFuncMap(),
  104. TemplateFileSystem: NewTemplateFileSystem(),
  105. HTMLContentType: "application/json",
  106. })
  107. }
  108. // JSRenderer implements the macaron handler for serving JS templates.
  109. func JSRenderer() macaron.Handler {
  110. return macaron.Renderer(macaron.RenderOptions{
  111. Funcs: NewFuncMap(),
  112. TemplateFileSystem: NewTemplateFileSystem(),
  113. HTMLContentType: "application/javascript",
  114. })
  115. }
  116. // Mailer provides the templates required for sending notification mails.
  117. func Mailer() (*texttmpl.Template, *template.Template) {
  118. for _, funcs := range NewTextFuncMap() {
  119. subjectTemplates.Funcs(funcs)
  120. }
  121. for _, funcs := range NewFuncMap() {
  122. bodyTemplates.Funcs(funcs)
  123. }
  124. for _, assetPath := range AssetNames() {
  125. if !strings.HasPrefix(assetPath, "mail/") {
  126. continue
  127. }
  128. if !strings.HasSuffix(assetPath, ".tmpl") {
  129. continue
  130. }
  131. content, err := Asset(assetPath)
  132. if err != nil {
  133. log.Warn("Failed to read embedded %s template. %v", assetPath, err)
  134. continue
  135. }
  136. buildSubjectBodyTemplate(subjectTemplates,
  137. bodyTemplates,
  138. strings.TrimPrefix(
  139. strings.TrimSuffix(
  140. assetPath,
  141. ".tmpl",
  142. ),
  143. "mail/",
  144. ),
  145. content)
  146. }
  147. customDir := path.Join(setting.CustomPath, "templates", "mail")
  148. if com.IsDir(customDir) {
  149. files, err := com.StatDir(customDir)
  150. if err != nil {
  151. log.Warn("Failed to read %s templates dir. %v", customDir, err)
  152. } else {
  153. for _, filePath := range files {
  154. if !strings.HasSuffix(filePath, ".tmpl") {
  155. continue
  156. }
  157. content, err := ioutil.ReadFile(path.Join(customDir, filePath))
  158. if err != nil {
  159. log.Warn("Failed to read custom %s template. %v", filePath, err)
  160. continue
  161. }
  162. buildSubjectBodyTemplate(subjectTemplates,
  163. bodyTemplates,
  164. strings.TrimSuffix(
  165. filePath,
  166. ".tmpl",
  167. ),
  168. content)
  169. }
  170. }
  171. }
  172. return subjectTemplates, bodyTemplates
  173. }
  174. func Asset(name string) ([]byte, error) {
  175. f, err := Assets.Open("/" + name)
  176. if err != nil {
  177. return nil, err
  178. }
  179. defer f.Close()
  180. return ioutil.ReadAll(f)
  181. }
  182. func AssetNames() []string {
  183. realFS := Assets.(vfsgen۰FS)
  184. var results = make([]string, 0, len(realFS))
  185. for k := range realFS {
  186. results = append(results, k[1:])
  187. }
  188. return results
  189. }
  190. func AssetIsDir(name string) (bool, error) {
  191. if f, err := Assets.Open("/" + name); err != nil {
  192. return false, err
  193. } else {
  194. defer f.Close()
  195. if fi, err := f.Stat(); err != nil {
  196. return false, err
  197. } else {
  198. return fi.IsDir(), nil
  199. }
  200. }
  201. }