2018-02-04 18:45:07 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package template // import "miniflux.app/template"
|
2018-02-04 18:45:07 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-02-19 00:33:29 -05:00
|
|
|
"embed"
|
2018-02-04 18:45:07 -05:00
|
|
|
"html/template"
|
2021-02-19 00:33:29 -05:00
|
|
|
"strings"
|
2018-04-28 01:07:46 -04:00
|
|
|
"time"
|
2018-02-04 18:45:07 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/errors"
|
|
|
|
"miniflux.app/locale"
|
|
|
|
"miniflux.app/logger"
|
2018-02-04 18:45:07 -05:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2021-02-19 00:33:29 -05:00
|
|
|
//go:embed templates/common/*.html
|
|
|
|
var commonTemplateFiles embed.FS
|
|
|
|
|
|
|
|
//go:embed templates/views/*.html
|
|
|
|
var viewTemplateFiles embed.FS
|
|
|
|
|
2021-03-07 18:25:34 -05:00
|
|
|
//go:embed templates/standalone/*.html
|
|
|
|
var standaloneTemplateFiles embed.FS
|
|
|
|
|
2018-02-04 18:45:07 -05:00
|
|
|
// Engine handles the templating system.
|
|
|
|
type Engine struct {
|
2018-09-22 18:04:55 -04:00
|
|
|
templates map[string]*template.Template
|
|
|
|
funcMap *funcMap
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|
|
|
|
|
2021-02-19 00:33:29 -05:00
|
|
|
// NewEngine returns a new template engine.
|
|
|
|
func NewEngine(router *mux.Router) *Engine {
|
|
|
|
return &Engine{
|
|
|
|
templates: make(map[string]*template.Template),
|
|
|
|
funcMap: &funcMap{router},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseTemplates parses template files embed into the application.
|
|
|
|
func (e *Engine) ParseTemplates() error {
|
|
|
|
var commonTemplateContents strings.Builder
|
|
|
|
|
|
|
|
dirEntries, err := commonTemplateFiles.ReadDir("templates/common")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|
|
|
|
|
2021-02-19 00:33:29 -05:00
|
|
|
for _, dirEntry := range dirEntries {
|
|
|
|
fileData, err := commonTemplateFiles.ReadFile("templates/common/" + dirEntry.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
commonTemplateContents.Write(fileData)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirEntries, err = viewTemplateFiles.ReadDir("templates/views")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|
2021-02-19 00:33:29 -05:00
|
|
|
|
|
|
|
for _, dirEntry := range dirEntries {
|
|
|
|
templateName := dirEntry.Name()
|
|
|
|
fileData, err := viewTemplateFiles.ReadFile("templates/views/" + dirEntry.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateContents strings.Builder
|
|
|
|
templateContents.WriteString(commonTemplateContents.String())
|
|
|
|
templateContents.Write(fileData)
|
|
|
|
|
|
|
|
logger.Debug("[Template] Parsing: %s", templateName)
|
|
|
|
e.templates[templateName] = template.Must(template.New("main").Funcs(e.funcMap.Map()).Parse(templateContents.String()))
|
|
|
|
}
|
|
|
|
|
2021-03-07 18:25:34 -05:00
|
|
|
dirEntries, err = standaloneTemplateFiles.ReadDir("templates/standalone")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dirEntry := range dirEntries {
|
|
|
|
templateName := dirEntry.Name()
|
|
|
|
fileData, err := standaloneTemplateFiles.ReadFile("templates/standalone/" + dirEntry.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("[Template] Parsing: %s", templateName)
|
|
|
|
e.templates[templateName] = template.Must(template.New("base").Funcs(e.funcMap.Map()).Parse(string(fileData)))
|
|
|
|
}
|
|
|
|
|
2021-02-19 00:33:29 -05:00
|
|
|
return nil
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|
|
|
|
|
2018-09-09 17:25:56 -04:00
|
|
|
// Render process a template.
|
2021-05-31 16:19:37 -04:00
|
|
|
func (e *Engine) Render(name string, data map[string]interface{}) []byte {
|
2018-02-04 18:45:07 -05:00
|
|
|
tpl, ok := e.templates[name]
|
|
|
|
if !ok {
|
|
|
|
logger.Fatal("[Template] The template %s does not exists", name)
|
|
|
|
}
|
|
|
|
|
2021-05-31 16:19:37 -04:00
|
|
|
printer := locale.NewPrinter(data["language"].(string))
|
2018-09-20 22:45:56 -04:00
|
|
|
|
|
|
|
// Functions that need to be declared at runtime.
|
2018-04-28 01:07:46 -04:00
|
|
|
tpl.Funcs(template.FuncMap{
|
|
|
|
"elapsed": func(timezone string, t time.Time) string {
|
2018-09-22 18:04:55 -04:00
|
|
|
return elapsedTime(printer, timezone, t)
|
2018-04-28 01:07:46 -04:00
|
|
|
},
|
|
|
|
"t": func(key interface{}, args ...interface{}) string {
|
2018-09-22 18:04:55 -04:00
|
|
|
switch k := key.(type) {
|
2018-04-28 01:07:46 -04:00
|
|
|
case string:
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Printf(k, args...)
|
2018-04-28 01:07:46 -04:00
|
|
|
case errors.LocalizedError:
|
2018-09-22 18:04:55 -04:00
|
|
|
return k.Localize(printer)
|
2018-04-28 01:07:46 -04:00
|
|
|
case *errors.LocalizedError:
|
2018-09-22 18:04:55 -04:00
|
|
|
return k.Localize(printer)
|
2018-04-28 01:07:46 -04:00
|
|
|
case error:
|
2018-09-22 18:04:55 -04:00
|
|
|
return k.Error()
|
2018-04-28 01:07:46 -04:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"plural": func(key string, n int, args ...interface{}) string {
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Plural(key, n, args...)
|
2018-04-28 01:07:46 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-02-04 18:45:07 -05:00
|
|
|
var b bytes.Buffer
|
|
|
|
err := tpl.ExecuteTemplate(&b, "base", data)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("[Template] Unable to render template: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
return b.Bytes()
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|