miniflux/server/template/template.go

151 lines
3.8 KiB
Go
Raw Normal View History

2017-11-22 04:37:47 +01:00
// Copyright 2017 Frédéric Guilloe. All rights reserved.
2017-11-20 06:10:04 +01: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"
"log"
2017-11-26 02:08:04 +01:00
"net/mail"
2017-11-20 06:10:04 +01:00
"strings"
"time"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/config"
"github.com/miniflux/miniflux/errors"
"github.com/miniflux/miniflux/locale"
"github.com/miniflux/miniflux/server/route"
"github.com/miniflux/miniflux/server/template/helper"
"github.com/miniflux/miniflux/server/ui/filter"
"github.com/miniflux/miniflux/url"
2017-11-20 23:35:11 +01:00
2017-11-20 06:10:04 +01:00
"github.com/gorilla/mux"
)
2017-11-22 04:37:47 +01:00
// Engine handles the templating system.
type Engine struct {
2017-11-20 06:10:04 +01:00
templates map[string]*template.Template
router *mux.Router
translator *locale.Translator
currentLocale *locale.Language
2017-11-22 04:37:47 +01:00
cfg *config.Config
2017-11-20 06:10:04 +01:00
}
2017-11-22 04:37:47 +01:00
func (e *Engine) parseAll() {
2017-11-20 06:10:04 +01:00
funcMap := template.FuncMap{
2017-11-22 04:37:47 +01:00
"baseURL": func() string {
return e.cfg.Get("BASE_URL", config.DefaultBaseURL)
},
2017-11-23 07:22:33 +01:00
"hasOAuth2Provider": func(provider string) bool {
return e.cfg.Get("OAUTH2_PROVIDER", "") == provider
},
2017-11-25 01:09:10 +01:00
"hasKey": func(dict map[string]string, key string) bool {
log.Println(dict)
if value, found := dict[key]; found {
return value != ""
}
return false
},
2017-11-20 06:10:04 +01:00
"route": func(name string, args ...interface{}) string {
2017-11-28 06:30:04 +01:00
return route.Path(e.router, name, args...)
2017-11-20 06:10:04 +01:00
},
"noescape": func(str string) template.HTML {
return template.HTML(str)
},
"proxyFilter": func(data string) string {
2017-11-22 04:37:47 +01:00
return filter.ImageProxyFilter(e.router, data)
2017-11-20 06:10:04 +01:00
},
2017-12-02 07:29:18 +01:00
"proxyURL": func(link string) string {
if url.IsHTTPS(link) {
return link
2017-11-20 06:10:04 +01:00
}
2017-12-02 07:29:18 +01:00
return filter.Proxify(e.router, link)
},
"domain": func(websiteURL string) string {
return url.Domain(websiteURL)
2017-11-20 06:10:04 +01:00
},
2017-11-26 02:08:04 +01:00
"isEmail": func(str string) bool {
_, err := mail.ParseAddress(str)
if err != nil {
return false
}
return true
},
2017-11-20 06:10:04 +01: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 {
2017-11-22 04:37:47 +01:00
return helper.GetElapsedTime(e.currentLocale, ts)
2017-11-20 06:10:04 +01:00
},
"t": func(key interface{}, args ...interface{}) string {
switch key.(type) {
2017-11-20 23:35:11 +01:00
case string:
2017-11-22 04:37:47 +01:00
return e.currentLocale.Get(key.(string), args...)
2017-11-20 06:10:04 +01:00
case errors.LocalizedError:
err := key.(errors.LocalizedError)
2017-11-22 04:37:47 +01:00
return err.Localize(e.currentLocale)
2017-11-20 23:35:11 +01:00
case error:
return key.(error).Error()
2017-11-20 06:10:04 +01:00
default:
return ""
}
},
"plural": func(key string, n int, args ...interface{}) string {
2017-11-22 04:37:47 +01:00
return e.currentLocale.Plural(key, n, args...)
2017-11-20 06:10:04 +01:00
},
}
commonTemplates := ""
for _, content := range templateCommonMap {
commonTemplates += content
}
for name, content := range templateViewsMap {
log.Println("Parsing template:", name)
2017-11-22 04:37:47 +01:00
e.templates[name] = template.Must(template.New("main").Funcs(funcMap).Parse(commonTemplates + content))
2017-11-20 06:10:04 +01:00
}
}
2017-11-22 04:37:47 +01:00
// SetLanguage change the language for template processing.
func (e *Engine) SetLanguage(language string) {
e.currentLocale = e.translator.GetLanguage(language)
2017-11-20 06:10:04 +01:00
}
2017-11-22 04:37:47 +01:00
// Execute process a template.
func (e *Engine) Execute(w io.Writer, name string, data interface{}) {
tpl, ok := e.templates[name]
2017-11-20 06:10:04 +01:00
if !ok {
log.Fatalf("The template %s does not exists.\n", name)
}
var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "base", data)
if err != nil {
log.Fatalf("Unable to render template: %v\n", err)
}
b.WriteTo(w)
}
2017-11-22 04:37:47 +01:00
// NewEngine returns a new template Engine.
func NewEngine(cfg *config.Config, router *mux.Router, translator *locale.Translator) *Engine {
tpl := &Engine{
2017-11-20 06:10:04 +01:00
templates: make(map[string]*template.Template),
router: router,
translator: translator,
2017-11-22 04:37:47 +01:00
cfg: cfg,
2017-11-20 06:10:04 +01:00
}
2017-11-22 04:37:47 +01:00
tpl.parseAll()
2017-11-20 06:10:04 +01:00
return tpl
}