1
0
Fork 0
miniflux/template/template.go

150 lines
3.8 KiB
Go
Raw Normal View History

2017-11-21 22:37:47 -05:00
// Copyright 2017 Frédéric Guilloe. All rights reserved.
2017-11-20 00:10:04 -05:00
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package template
import (
"bytes"
"html/template"
"io"
2017-11-25 20:08:04 -05:00
"net/mail"
2017-11-20 00:10:04 -05:00
"strings"
"time"
2017-12-13 00:48:13 -05:00
"github.com/miniflux/miniflux/config"
2018-01-02 22:15:08 -05:00
"github.com/miniflux/miniflux/duration"
2017-12-13 00:48:13 -05:00
"github.com/miniflux/miniflux/errors"
"github.com/miniflux/miniflux/filter"
"github.com/miniflux/miniflux/http/route"
2017-12-13 00:48:13 -05:00
"github.com/miniflux/miniflux/locale"
2017-12-15 21:55:57 -05:00
"github.com/miniflux/miniflux/logger"
2017-12-13 00:48:13 -05:00
"github.com/miniflux/miniflux/url"
2017-11-20 17:35:11 -05:00
2017-11-20 00:10:04 -05:00
"github.com/gorilla/mux"
)
2017-11-21 22:37:47 -05:00
// Engine handles the templating system.
type Engine struct {
2017-11-20 00:10:04 -05:00
templates map[string]*template.Template
router *mux.Router
translator *locale.Translator
currentLocale *locale.Language
2017-11-21 22:37:47 -05:00
cfg *config.Config
2017-11-20 00:10:04 -05:00
}
2017-11-21 22:37:47 -05:00
func (e *Engine) parseAll() {
2017-11-20 00:10:04 -05:00
funcMap := template.FuncMap{
2017-11-21 22:37:47 -05:00
"baseURL": func() string {
2018-01-15 21:08:30 -05:00
return e.cfg.BaseURL()
2017-11-21 22:37:47 -05:00
},
2017-11-23 01:22:33 -05:00
"hasOAuth2Provider": func(provider string) bool {
return e.cfg.Get("OAUTH2_PROVIDER", "") == provider
},
2017-11-24 19:09:10 -05:00
"hasKey": func(dict map[string]string, key string) bool {
if value, found := dict[key]; found {
return value != ""
}
return false
},
2017-11-20 00:10:04 -05:00
"route": func(name string, args ...interface{}) string {
2017-11-28 00:30:04 -05:00
return route.Path(e.router, name, args...)
2017-11-20 00:10:04 -05:00
},
"noescape": func(str string) template.HTML {
return template.HTML(str)
},
"proxyFilter": func(data string) string {
2017-11-21 22:37:47 -05:00
return filter.ImageProxyFilter(e.router, data)
2017-11-20 00:10:04 -05:00
},
2017-12-02 01:29:18 -05:00
"proxyURL": func(link string) string {
if url.IsHTTPS(link) {
return link
2017-11-20 00:10:04 -05:00
}
2017-12-02 01:29:18 -05:00
return filter.Proxify(e.router, link)
},
"domain": func(websiteURL string) string {
return url.Domain(websiteURL)
2017-11-20 00:10:04 -05:00
},
2017-11-25 20:08:04 -05:00
"isEmail": func(str string) bool {
_, err := mail.ParseAddress(str)
if err != nil {
return false
}
return true
},
2017-11-20 00:10:04 -05:00
"hasPrefix": func(str, prefix string) bool {
return strings.HasPrefix(str, prefix)
},
"contains": func(str, substr string) bool {
return strings.Contains(str, substr)
},
"isodate": func(ts time.Time) string {
return ts.Format("2006-01-02 15:04:05")
},
"elapsed": func(ts time.Time) string {
2018-01-02 22:15:08 -05:00
return duration.ElapsedTime(e.currentLocale, ts)
2017-11-20 00:10:04 -05:00
},
"t": func(key interface{}, args ...interface{}) string {
switch key.(type) {
2017-11-20 17:35:11 -05:00
case string:
2017-11-21 22:37:47 -05:00
return e.currentLocale.Get(key.(string), args...)
2017-11-20 00:10:04 -05:00
case errors.LocalizedError:
err := key.(errors.LocalizedError)
2017-11-21 22:37:47 -05:00
return err.Localize(e.currentLocale)
2017-11-20 17:35:11 -05:00
case error:
return key.(error).Error()
2017-11-20 00:10:04 -05:00
default:
return ""
}
},
"plural": func(key string, n int, args ...interface{}) string {
2017-11-21 22:37:47 -05:00
return e.currentLocale.Plural(key, n, args...)
2017-11-20 00:10:04 -05:00
},
}
commonTemplates := ""
for _, content := range templateCommonMap {
commonTemplates += content
}
for name, content := range templateViewsMap {
2017-12-15 21:55:57 -05:00
logger.Debug("[Template] Parsing: %s", name)
2017-11-21 22:37:47 -05:00
e.templates[name] = template.Must(template.New("main").Funcs(funcMap).Parse(commonTemplates + content))
2017-11-20 00:10:04 -05:00
}
}
2017-11-21 22:37:47 -05:00
// SetLanguage change the language for template processing.
func (e *Engine) SetLanguage(language string) {
e.currentLocale = e.translator.GetLanguage(language)
2017-11-20 00:10:04 -05:00
}
2017-11-21 22:37:47 -05:00
// Execute process a template.
func (e *Engine) Execute(w io.Writer, name string, data interface{}) {
tpl, ok := e.templates[name]
2017-11-20 00:10:04 -05:00
if !ok {
2017-12-15 21:55:57 -05:00
logger.Fatal("[Template] The template %s does not exists", name)
2017-11-20 00:10:04 -05:00
}
var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "base", data)
if err != nil {
2017-12-15 21:55:57 -05:00
logger.Fatal("[Template] Unable to render template: %v", err)
2017-11-20 00:10:04 -05:00
}
b.WriteTo(w)
}
2017-11-21 22:37:47 -05:00
// NewEngine returns a new template Engine.
func NewEngine(cfg *config.Config, router *mux.Router, translator *locale.Translator) *Engine {
tpl := &Engine{
2017-11-20 00:10:04 -05:00
templates: make(map[string]*template.Template),
router: router,
translator: translator,
2017-11-21 22:37:47 -05:00
cfg: cfg,
2017-11-20 00:10:04 -05:00
}
2017-11-21 22:37:47 -05:00
tpl.parseAll()
2017-11-20 00:10:04 -05:00
return tpl
}