2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-02-04 18:45:07 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package template // import "miniflux.app/v2/internal/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"
|
2023-09-24 19:32:09 -04:00
|
|
|
"log/slog"
|
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
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/locale"
|
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)
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Parsing template",
|
|
|
|
slog.String("template_name", templateName),
|
|
|
|
)
|
|
|
|
|
2021-02-19 00:33:29 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Parsing template",
|
|
|
|
slog.String("template_name", templateName),
|
|
|
|
)
|
2021-03-07 18:25:34 -05:00
|
|
|
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 {
|
2023-09-24 19:32:09 -04:00
|
|
|
panic("This template does not exists: " + name)
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|
|
|
|
|
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 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 {
|
2023-09-24 19:32:09 -04:00
|
|
|
panic(err)
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
return b.Bytes()
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|