Skip to content

Commit db6777d

Browse files
tboergerlunny
authored andcommitted
Fixed custom templates for static builds (#1087)
1 parent d21d5fd commit db6777d

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

modules/templates/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewFuncMap() []template.FuncMap {
4949
return setting.AppVer
5050
},
5151
"AppBuiltWith": func() string {
52-
return setting.AppBuiltWith
52+
return setting.AppBuiltWith
5353
},
5454
"AppDomain": func() string {
5555
return setting.Domain

modules/templates/static.go

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,112 @@
77
package templates
88

99
import (
10+
"bytes"
11+
"fmt"
1012
"html/template"
13+
"io"
1114
"io/ioutil"
1215
"path"
1316
"strings"
1417

1518
"code.gitea.io/gitea/modules/log"
1619
"code.gitea.io/gitea/modules/setting"
1720
"github.com/Unknwon/com"
18-
"github.com/go-macaron/bindata"
1921
"gopkg.in/macaron.v1"
2022
)
2123

2224
var (
2325
templates = template.New("")
2426
)
2527

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+
2646
// Renderer implements the macaron handler for serving the templates.
2747
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+
28113
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,
42116
})
43117
}
44118

0 commit comments

Comments
 (0)