mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 09:44:21 +00:00 
			
		
		
		
	Fixed custom templates for static builds (#1087)
This commit is contained in:
		
				
					committed by
					
						
						Lunny Xiao
					
				
			
			
				
	
			
			
			
						parent
						
							d21d5fd736
						
					
				
				
					commit
					db6777d369
				
			@@ -7,7 +7,10 @@
 | 
			
		||||
package templates
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"html/template"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"path"
 | 
			
		||||
	"strings"
 | 
			
		||||
@@ -15,7 +18,6 @@ import (
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"github.com/Unknwon/com"
 | 
			
		||||
	"github.com/go-macaron/bindata"
 | 
			
		||||
	"gopkg.in/macaron.v1"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -23,22 +25,94 @@ var (
 | 
			
		||||
	templates = template.New("")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type templateFileSystem struct {
 | 
			
		||||
	files []macaron.TemplateFile
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (templates templateFileSystem) ListFiles() []macaron.TemplateFile {
 | 
			
		||||
	return templates.files
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (templates templateFileSystem) Get(name string) (io.Reader, error) {
 | 
			
		||||
	for i := range templates.files {
 | 
			
		||||
		if templates.files[i].Name()+templates.files[i].Ext() == name {
 | 
			
		||||
			return bytes.NewReader(templates.files[i].Data()), nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil, fmt.Errorf("file '%s' not found", name)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Renderer implements the macaron handler for serving the templates.
 | 
			
		||||
func Renderer() macaron.Handler {
 | 
			
		||||
	fs := templateFileSystem{}
 | 
			
		||||
	fs.files = make([]macaron.TemplateFile, 0, 10)
 | 
			
		||||
 | 
			
		||||
	for _, assetPath := range AssetNames() {
 | 
			
		||||
		if strings.HasPrefix(assetPath, "mail/") {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if !strings.HasSuffix(assetPath, ".tmpl") {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		content, err := Asset(assetPath)
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Warn("Failed to read embedded %s template. %v", assetPath, err)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		fs.files = append(fs.files, macaron.NewTplFile(
 | 
			
		||||
			strings.TrimSuffix(
 | 
			
		||||
				assetPath,
 | 
			
		||||
				".tmpl",
 | 
			
		||||
			),
 | 
			
		||||
			content,
 | 
			
		||||
			".tmpl",
 | 
			
		||||
		))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	customDir := path.Join(setting.CustomPath, "templates")
 | 
			
		||||
 | 
			
		||||
	if com.IsDir(customDir) {
 | 
			
		||||
		files, err := com.StatDir(customDir)
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Warn("Failed to read %s templates dir. %v", customDir, err)
 | 
			
		||||
		} else {
 | 
			
		||||
			for _, filePath := range files {
 | 
			
		||||
				if strings.HasPrefix(filePath, "mail/") {
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				if !strings.HasSuffix(filePath, ".tmpl") {
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				content, err := ioutil.ReadFile(path.Join(customDir, filePath))
 | 
			
		||||
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Warn("Failed to read custom %s template. %v", filePath, err)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fs.files = append(fs.files, macaron.NewTplFile(
 | 
			
		||||
					strings.TrimSuffix(
 | 
			
		||||
						filePath,
 | 
			
		||||
						".tmpl",
 | 
			
		||||
					),
 | 
			
		||||
					content,
 | 
			
		||||
					".tmpl",
 | 
			
		||||
				))
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return macaron.Renderer(macaron.RenderOptions{
 | 
			
		||||
		Funcs:              NewFuncMap(),
 | 
			
		||||
		AppendDirectories: []string{
 | 
			
		||||
			path.Join(setting.CustomPath, "templates"),
 | 
			
		||||
		},
 | 
			
		||||
		TemplateFileSystem: bindata.Templates(
 | 
			
		||||
			bindata.Options{
 | 
			
		||||
				Asset:      Asset,
 | 
			
		||||
				AssetDir:   AssetDir,
 | 
			
		||||
				AssetInfo:  AssetInfo,
 | 
			
		||||
				AssetNames: AssetNames,
 | 
			
		||||
				Prefix:     "",
 | 
			
		||||
			},
 | 
			
		||||
		),
 | 
			
		||||
		TemplateFileSystem: fs,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user