2018-02-05 00:45:07 +01:00
|
|
|
// Copyright 2018 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 06:51:50 +02:00
|
|
|
package template // import "miniflux.app/template"
|
2018-02-05 00:45:07 +01:00
|
|
|
|
|
|
|
import (
|
2018-12-03 06:09:53 +01:00
|
|
|
"encoding/base64"
|
2018-09-21 04:45:56 +02:00
|
|
|
"fmt"
|
2018-02-05 00:45:07 +01:00
|
|
|
"html/template"
|
2019-06-02 03:18:09 +02:00
|
|
|
"math"
|
2018-02-05 00:45:07 +01:00
|
|
|
"net/mail"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/config"
|
|
|
|
"miniflux.app/http/route"
|
2018-09-22 03:53:29 +02:00
|
|
|
"miniflux.app/locale"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/model"
|
2018-09-22 03:53:29 +02:00
|
|
|
"miniflux.app/timezone"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/url"
|
2018-12-03 06:09:53 +01:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2019-06-02 03:18:09 +02:00
|
|
|
"github.com/gorilla/mux"
|
2018-02-05 00:45:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type funcMap struct {
|
2018-04-28 07:07:46 +02:00
|
|
|
router *mux.Router
|
2018-02-05 00:45:07 +01:00
|
|
|
}
|
|
|
|
|
2018-09-21 04:45:56 +02:00
|
|
|
// Map returns a map of template functions that are compiled during template parsing.
|
2018-02-05 00:45:07 +01:00
|
|
|
func (f *funcMap) Map() template.FuncMap {
|
|
|
|
return template.FuncMap{
|
2019-11-29 19:27:25 +01:00
|
|
|
"formatFileSize": formatFileSize,
|
|
|
|
"dict": dict,
|
|
|
|
"hasKey": hasKey,
|
|
|
|
"truncate": truncate,
|
|
|
|
"isEmail": isEmail,
|
2018-02-05 00:45:07 +01:00
|
|
|
"baseURL": func() string {
|
2019-06-02 03:18:09 +02:00
|
|
|
return config.Opts.BaseURL()
|
2018-02-05 00:45:07 +01:00
|
|
|
},
|
|
|
|
"rootURL": func() string {
|
2019-06-02 03:18:09 +02:00
|
|
|
return config.Opts.RootURL()
|
2018-02-05 00:45:07 +01:00
|
|
|
},
|
|
|
|
"hasOAuth2Provider": func(provider string) bool {
|
2019-06-02 03:18:09 +02:00
|
|
|
return config.Opts.OAuth2Provider() == provider
|
2018-02-05 00:45:07 +01:00
|
|
|
},
|
|
|
|
"route": func(name string, args ...interface{}) string {
|
|
|
|
return route.Path(f.router, name, args...)
|
|
|
|
},
|
|
|
|
"noescape": func(str string) template.HTML {
|
|
|
|
return template.HTML(str)
|
|
|
|
},
|
|
|
|
"proxyFilter": func(data string) string {
|
2019-06-02 03:18:09 +02:00
|
|
|
return imageProxyFilter(f.router, data)
|
2018-02-05 00:45:07 +01:00
|
|
|
},
|
|
|
|
"proxyURL": func(link string) string {
|
2019-06-02 03:18:09 +02:00
|
|
|
proxyImages := config.Opts.ProxyImages()
|
2018-07-13 02:41:09 +02:00
|
|
|
|
|
|
|
if proxyImages == "all" || (proxyImages != "none" && !url.IsHTTPS(link)) {
|
2018-12-03 06:09:53 +01:00
|
|
|
return proxify(f.router, link)
|
2018-02-05 00:45:07 +01:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:41:09 +02:00
|
|
|
return link
|
2018-02-05 00:45:07 +01:00
|
|
|
},
|
|
|
|
"domain": func(websiteURL string) string {
|
|
|
|
return url.Domain(websiteURL)
|
|
|
|
},
|
|
|
|
"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")
|
|
|
|
},
|
2018-07-19 07:30:05 +02:00
|
|
|
"theme_color": func(theme string) string {
|
|
|
|
return model.ThemeColor(theme)
|
|
|
|
},
|
2018-04-28 07:07:46 +02:00
|
|
|
|
|
|
|
// These functions are overrided at runtime after the parsing.
|
2018-02-05 00:45:07 +01:00
|
|
|
"elapsed": func(timezone string, t time.Time) string {
|
2018-04-28 07:07:46 +02:00
|
|
|
return ""
|
2018-02-05 00:45:07 +01:00
|
|
|
},
|
|
|
|
"t": func(key interface{}, args ...interface{}) string {
|
2018-04-28 07:07:46 +02:00
|
|
|
return ""
|
2018-02-05 00:45:07 +01:00
|
|
|
},
|
|
|
|
"plural": func(key string, n int, args ...interface{}) string {
|
2018-04-28 07:07:46 +02:00
|
|
|
return ""
|
2018-02-05 00:45:07 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 04:45:56 +02:00
|
|
|
func dict(values ...interface{}) (map[string]interface{}, error) {
|
|
|
|
if len(values)%2 != 0 {
|
|
|
|
return nil, fmt.Errorf("dict expects an even number of arguments")
|
|
|
|
}
|
|
|
|
dict := make(map[string]interface{}, len(values)/2)
|
|
|
|
for i := 0; i < len(values); i += 2 {
|
|
|
|
key, ok := values[i].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("dict keys must be strings")
|
|
|
|
}
|
|
|
|
dict[key] = values[i+1]
|
|
|
|
}
|
|
|
|
return dict, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasKey(dict map[string]string, key string) bool {
|
|
|
|
if value, found := dict[key]; found {
|
|
|
|
return value != ""
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func truncate(str string, max int) string {
|
|
|
|
runes := 0
|
|
|
|
for i := range str {
|
|
|
|
runes++
|
|
|
|
if runes > max {
|
|
|
|
return str[:i] + "…"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEmail(str string) bool {
|
|
|
|
_, err := mail.ParseAddress(str)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-09-22 03:53:29 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 00:04:55 +02:00
|
|
|
func elapsedTime(printer *locale.Printer, tz string, t time.Time) string {
|
2018-09-22 03:53:29 +02:00
|
|
|
if t.IsZero() {
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Printf("time_elapsed.not_yet")
|
2018-09-22 03:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
now := timezone.Now(tz)
|
|
|
|
t = timezone.Convert(tz, t)
|
|
|
|
if now.Before(t) {
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Printf("time_elapsed.not_yet")
|
2018-09-22 03:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
diff := now.Sub(t)
|
|
|
|
// Duration in seconds
|
|
|
|
s := diff.Seconds()
|
|
|
|
// Duration in days
|
|
|
|
d := int(s / 86400)
|
|
|
|
switch {
|
|
|
|
case s < 60:
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Printf("time_elapsed.now")
|
2018-09-22 03:53:29 +02:00
|
|
|
case s < 3600:
|
|
|
|
minutes := int(diff.Minutes())
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Plural("time_elapsed.minutes", minutes, minutes)
|
2018-09-22 03:53:29 +02:00
|
|
|
case s < 86400:
|
|
|
|
hours := int(diff.Hours())
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Plural("time_elapsed.hours", hours, hours)
|
2018-09-22 03:53:29 +02:00
|
|
|
case d == 1:
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Printf("time_elapsed.yesterday")
|
2018-11-11 12:43:42 +01:00
|
|
|
case d < 21:
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Plural("time_elapsed.days", d, d)
|
2018-09-22 03:53:29 +02:00
|
|
|
case d < 31:
|
2018-11-11 12:43:42 +01:00
|
|
|
weeks := int(math.Round(float64(d) / 7))
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Plural("time_elapsed.weeks", weeks, weeks)
|
2018-09-22 03:53:29 +02:00
|
|
|
case d < 365:
|
2018-11-11 12:43:42 +01:00
|
|
|
months := int(math.Round(float64(d) / 30))
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Plural("time_elapsed.months", months, months)
|
2018-09-22 03:53:29 +02:00
|
|
|
default:
|
2018-11-11 12:43:42 +01:00
|
|
|
years := int(math.Round(float64(d) / 365))
|
2018-09-23 00:04:55 +02:00
|
|
|
return printer.Plural("time_elapsed.years", years, years)
|
2018-09-22 03:53:29 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-03 06:09:53 +01:00
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
func imageProxyFilter(router *mux.Router, data string) string {
|
|
|
|
proxyImages := config.Opts.ProxyImages()
|
2018-12-03 06:09:53 +01:00
|
|
|
if proxyImages == "none" {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
doc.Find("img").Each(func(i int, img *goquery.Selection) {
|
|
|
|
if srcAttr, ok := img.Attr("src"); ok {
|
|
|
|
if proxyImages == "all" || !url.IsHTTPS(srcAttr) {
|
|
|
|
img.SetAttr("src", proxify(router, srcAttr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
output, _ := doc.Find("body").First().Html()
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func proxify(router *mux.Router, link string) string {
|
|
|
|
// We use base64 url encoding to avoid slash in the URL.
|
|
|
|
return route.Path(router, "proxy", "encodedURL", base64.URLEncoding.EncodeToString([]byte(link)))
|
|
|
|
}
|
2019-11-29 19:27:25 +01:00
|
|
|
|
|
|
|
func formatFileSize(b int64) string {
|
|
|
|
const unit = 1024
|
|
|
|
if b < unit {
|
|
|
|
return fmt.Sprintf("%d B", b)
|
|
|
|
}
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.1f %ciB",
|
|
|
|
float64(b)/float64(div), "KMGTPE"[exp])
|
|
|
|
}
|