2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-02-05 00:45:07 +01:00
|
|
|
|
2023-08-10 06:15:55 +02:00
|
|
|
package template // import "miniflux.app/v2/template"
|
2018-02-05 00:45:07 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-02-19 06:33:29 +01:00
|
|
|
"embed"
|
2018-02-05 00:45:07 +01:00
|
|
|
"html/template"
|
2021-02-19 06:33:29 +01:00
|
|
|
"strings"
|
2018-04-28 07:07:46 +02:00
|
|
|
"time"
|
2018-02-05 00:45:07 +01:00
|
|
|
|
2023-08-10 06:15:55 +02:00
|
|
|
"miniflux.app/v2/errors"
|
|
|
|
"miniflux.app/v2/locale"
|
|
|
|
"miniflux.app/v2/logger"
|
2018-02-05 00:45:07 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2021-02-19 06:33:29 +01:00
|
|
|
//go:embed templates/common/*.html
|
|
|
|
var commonTemplateFiles embed.FS
|
|
|
|
|
|
|
|
//go:embed templates/views/*.html
|
|
|
|
var viewTemplateFiles embed.FS
|
|
|
|
|
2021-03-08 00:25:34 +01:00
|
|
|
//go:embed templates/standalone/*.html
|
|
|
|
var standaloneTemplateFiles embed.FS
|
|
|
|
|
2018-02-05 00:45:07 +01:00
|
|
|
// Engine handles the templating system.
|
|
|
|
type Engine struct {
|
2018-09-23 00:04:55 +02:00
|
|
|
templates map[string]*template.Template
|
|
|
|
funcMap *funcMap
|
2018-02-05 00:45:07 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 06:33:29 +01: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-05 00:45:07 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 06:33:29 +01: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-05 00:45:07 +01:00
|
|
|
}
|
2021-02-19 06:33:29 +01: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-08 00:25:34 +01: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 06:33:29 +01:00
|
|
|
return nil
|
2018-02-05 00:45:07 +01:00
|
|
|
}
|
|
|
|
|
2018-09-09 23:25:56 +02:00
|
|
|
// Render process a template.
|
2021-05-31 22:19:37 +02:00
|
|
|
func (e *Engine) Render(name string, data map[string]interface{}) []byte {
|
2018-02-05 00:45:07 +01:00
|
|
|
tpl, ok := e.templates[name]
|
|
|
|
if !ok {
|
|
|
|
logger.Fatal("[Template] The template %s does not exists", name)
|
|
|
|
}
|
|
|
|
|
2021-05-31 22:19:37 +02:00
|
|
|
printer := locale.NewPrinter(data["language"].(string))
|
2018-09-21 04:45:56 +02:00
|
|
|
|
|
|
|
// Functions that need to be declared at runtime.
|
2018-04-28 07:07:46 +02:00
|
|
|
tpl.Funcs(template.FuncMap{
|
|
|
|
"elapsed": func(timezone string, t time.Time) string {
|
2018-09-23 00:04:55 +02:00
|
|
|
return elapsedTime(printer, timezone, t)
|
2018-04-28 07:07:46 +02:00
|
|
|
},
|
|
|
|
"t": func(key interface{}, args ...interface{}) string {
|
2018-09-23 00:04:55 +02:00
|
|
|
switch k := key.(type) {
|
2018-04-28 07:07:46 +02:00
|
|
|
case string:
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Printf(k, args...)
|
2018-04-28 07:07:46 +02:00
|
|
|
case errors.LocalizedError:
|
2018-09-23 00:04:55 +02:00
|
|
|
return k.Localize(printer)
|
2018-04-28 07:07:46 +02:00
|
|
|
case *errors.LocalizedError:
|
2018-09-23 00:04:55 +02:00
|
|
|
return k.Localize(printer)
|
2018-04-28 07:07:46 +02:00
|
|
|
case error:
|
2018-09-23 00:04:55 +02:00
|
|
|
return k.Error()
|
2018-04-28 07:07:46 +02:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"plural": func(key string, n int, args ...interface{}) string {
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Plural(key, n, args...)
|
2018-04-28 07:07:46 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-02-05 00:45:07 +01: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-30 01:35:04 +02:00
|
|
|
return b.Bytes()
|
2018-02-05 00:45:07 +01:00
|
|
|
}
|