|
7 | 7 | package templates
|
8 | 8 |
|
9 | 9 | import (
|
| 10 | + "bytes" |
| 11 | + "fmt" |
10 | 12 | "html/template"
|
| 13 | + "io" |
11 | 14 | "io/ioutil"
|
12 | 15 | "path"
|
13 | 16 | "strings"
|
14 | 17 |
|
15 | 18 | "code.gitea.io/gitea/modules/log"
|
16 | 19 | "code.gitea.io/gitea/modules/setting"
|
17 | 20 | "github.com/Unknwon/com"
|
18 |
| - "github.com/go-macaron/bindata" |
19 | 21 | "gopkg.in/macaron.v1"
|
20 | 22 | )
|
21 | 23 |
|
22 | 24 | var (
|
23 | 25 | templates = template.New("")
|
24 | 26 | )
|
25 | 27 |
|
| 28 | +type templateFileSystem struct { |
| 29 | + files []macaron.TemplateFile |
| 30 | +} |
| 31 | + |
| 32 | +func (templates templateFileSystem) ListFiles() []macaron.TemplateFile { |
| 33 | + return templates.files |
| 34 | +} |
| 35 | + |
| 36 | +func (templates templateFileSystem) Get(name string) (io.Reader, error) { |
| 37 | + for i := range templates.files { |
| 38 | + if templates.files[i].Name()+templates.files[i].Ext() == name { |
| 39 | + return bytes.NewReader(templates.files[i].Data()), nil |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return nil, fmt.Errorf("file '%s' not found", name) |
| 44 | +} |
| 45 | + |
26 | 46 | // Renderer implements the macaron handler for serving the templates.
|
27 | 47 | func Renderer() macaron.Handler {
|
| 48 | + fs := templateFileSystem{} |
| 49 | + fs.files = make([]macaron.TemplateFile, 0, 10) |
| 50 | + |
| 51 | + for _, assetPath := range AssetNames() { |
| 52 | + if strings.HasPrefix(assetPath, "mail/") { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + if !strings.HasSuffix(assetPath, ".tmpl") { |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + content, err := Asset(assetPath) |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + log.Warn("Failed to read embedded %s template. %v", assetPath, err) |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + fs.files = append(fs.files, macaron.NewTplFile( |
| 68 | + strings.TrimSuffix( |
| 69 | + assetPath, |
| 70 | + ".tmpl", |
| 71 | + ), |
| 72 | + content, |
| 73 | + ".tmpl", |
| 74 | + )) |
| 75 | + } |
| 76 | + |
| 77 | + customDir := path.Join(setting.CustomPath, "templates") |
| 78 | + |
| 79 | + if com.IsDir(customDir) { |
| 80 | + files, err := com.StatDir(customDir) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + log.Warn("Failed to read %s templates dir. %v", customDir, err) |
| 84 | + } else { |
| 85 | + for _, filePath := range files { |
| 86 | + if strings.HasPrefix(filePath, "mail/") { |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + if !strings.HasSuffix(filePath, ".tmpl") { |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + content, err := ioutil.ReadFile(path.Join(customDir, filePath)) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + log.Warn("Failed to read custom %s template. %v", filePath, err) |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + fs.files = append(fs.files, macaron.NewTplFile( |
| 102 | + strings.TrimSuffix( |
| 103 | + filePath, |
| 104 | + ".tmpl", |
| 105 | + ), |
| 106 | + content, |
| 107 | + ".tmpl", |
| 108 | + )) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
28 | 113 | return macaron.Renderer(macaron.RenderOptions{
|
29 |
| - Funcs: NewFuncMap(), |
30 |
| - AppendDirectories: []string{ |
31 |
| - path.Join(setting.CustomPath, "templates"), |
32 |
| - }, |
33 |
| - TemplateFileSystem: bindata.Templates( |
34 |
| - bindata.Options{ |
35 |
| - Asset: Asset, |
36 |
| - AssetDir: AssetDir, |
37 |
| - AssetInfo: AssetInfo, |
38 |
| - AssetNames: AssetNames, |
39 |
| - Prefix: "", |
40 |
| - }, |
41 |
| - ), |
| 114 | + Funcs: NewFuncMap(), |
| 115 | + TemplateFileSystem: fs, |
42 | 116 | })
|
43 | 117 | }
|
44 | 118 |
|
|
0 commit comments