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 (
|
2018-09-20 22:45:56 -04:00
|
|
|
"fmt"
|
2018-02-04 18:45:07 -05:00
|
|
|
"html/template"
|
2019-06-01 21:18:09 -04:00
|
|
|
"math"
|
2018-02-04 18:45:07 -05:00
|
|
|
"net/mail"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/crypto"
|
|
|
|
"miniflux.app/v2/internal/http/route"
|
|
|
|
"miniflux.app/v2/internal/locale"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/proxy"
|
|
|
|
"miniflux.app/v2/internal/timezone"
|
2023-08-13 22:09:01 -04:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2018-12-03 00:09:53 -05:00
|
|
|
|
2019-06-01 21:18:09 -04:00
|
|
|
"github.com/gorilla/mux"
|
2018-02-04 18:45:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type funcMap struct {
|
2018-04-28 01:07:46 -04:00
|
|
|
router *mux.Router
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:45:56 -04:00
|
|
|
// Map returns a map of template functions that are compiled during template parsing.
|
2018-02-04 18:45:07 -05:00
|
|
|
func (f *funcMap) Map() template.FuncMap {
|
|
|
|
return template.FuncMap{
|
2019-11-29 13:27:25 -05:00
|
|
|
"formatFileSize": formatFileSize,
|
|
|
|
"dict": dict,
|
|
|
|
"hasKey": hasKey,
|
|
|
|
"truncate": truncate,
|
|
|
|
"isEmail": isEmail,
|
2018-02-04 18:45:07 -05:00
|
|
|
"baseURL": func() string {
|
2019-06-01 21:18:09 -04:00
|
|
|
return config.Opts.BaseURL()
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
|
|
|
"rootURL": func() string {
|
2019-06-01 21:18:09 -04:00
|
|
|
return config.Opts.RootURL()
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
|
|
|
"hasOAuth2Provider": func(provider string) bool {
|
2019-06-01 21:18:09 -04:00
|
|
|
return config.Opts.OAuth2Provider() == provider
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
2021-10-08 08:13:37 -04:00
|
|
|
"hasAuthProxy": func() bool {
|
|
|
|
return config.Opts.AuthProxyHeader() != ""
|
|
|
|
},
|
2018-02-04 18:45:07 -05:00
|
|
|
"route": func(name string, args ...interface{}) string {
|
|
|
|
return route.Path(f.router, name, args...)
|
|
|
|
},
|
2020-01-02 14:06:57 -05:00
|
|
|
"safeURL": func(url string) template.URL {
|
|
|
|
return template.URL(url)
|
|
|
|
},
|
2021-05-31 17:16:50 -04:00
|
|
|
"safeCSS": func(str string) template.CSS {
|
|
|
|
return template.CSS(str)
|
|
|
|
},
|
2018-02-04 18:45:07 -05:00
|
|
|
"noescape": func(str string) template.HTML {
|
|
|
|
return template.HTML(str)
|
|
|
|
},
|
|
|
|
"proxyFilter": func(data string) string {
|
2023-02-25 03:36:19 -05:00
|
|
|
return proxy.ProxyRewriter(f.router, data)
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
|
|
|
"proxyURL": func(link string) string {
|
2023-02-25 03:36:19 -05:00
|
|
|
proxyOption := config.Opts.ProxyOption()
|
2018-07-12 20:41:09 -04:00
|
|
|
|
2023-08-13 22:09:01 -04:00
|
|
|
if proxyOption == "all" || (proxyOption != "none" && !urllib.IsHTTPS(link)) {
|
2020-10-08 01:21:45 -04:00
|
|
|
return proxy.ProxifyURL(f.router, link)
|
2018-02-04 18:45:07 -05:00
|
|
|
}
|
|
|
|
|
2018-07-12 20:41:09 -04:00
|
|
|
return link
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
2023-02-25 03:36:19 -05:00
|
|
|
"mustBeProxyfied": func(mediaType string) bool {
|
|
|
|
for _, t := range config.Opts.ProxyMediaTypes() {
|
|
|
|
if t == mediaType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2018-02-04 18:45:07 -05:00
|
|
|
"domain": func(websiteURL string) string {
|
2023-08-13 22:09:01 -04:00
|
|
|
return urllib.Domain(websiteURL)
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
|
|
|
"hasPrefix": func(str, prefix string) bool {
|
|
|
|
return strings.HasPrefix(str, prefix)
|
|
|
|
},
|
|
|
|
"contains": func(str, substr string) bool {
|
|
|
|
return strings.Contains(str, substr)
|
|
|
|
},
|
2021-05-31 16:44:34 -04:00
|
|
|
"replace": func(str, old, new string) string {
|
2021-05-31 16:19:37 -04:00
|
|
|
return strings.Replace(str, old, new, 1)
|
|
|
|
},
|
2018-02-04 18:45:07 -05:00
|
|
|
"isodate": func(ts time.Time) string {
|
|
|
|
return ts.Format("2006-01-02 15:04:05")
|
|
|
|
},
|
2021-11-28 10:20:51 -05:00
|
|
|
"theme_color": func(theme, colorScheme string) string {
|
|
|
|
return model.ThemeColor(theme, colorScheme)
|
2018-07-19 01:30:05 -04:00
|
|
|
},
|
2021-03-07 14:02:52 -05:00
|
|
|
"icon": func(iconName string) template.HTML {
|
|
|
|
return template.HTML(fmt.Sprintf(
|
2021-05-31 16:44:34 -04:00
|
|
|
`<svg class="icon" aria-hidden="true"><use xlink:href="%s#icon-%s"/></svg>`,
|
2021-03-07 14:02:52 -05:00
|
|
|
route.Path(f.router, "appIcon", "filename", "sprite.svg"),
|
|
|
|
iconName,
|
|
|
|
))
|
|
|
|
},
|
2021-05-31 17:16:50 -04:00
|
|
|
"nonce": func() string {
|
|
|
|
return crypto.GenerateRandomStringHex(16)
|
2021-05-21 16:57:11 -04:00
|
|
|
},
|
2023-10-17 20:56:17 -04:00
|
|
|
"deRef": func(i *int) int { return *i },
|
|
|
|
"duration": duration,
|
2018-04-28 01:07:46 -04:00
|
|
|
|
2021-05-13 09:38:32 -04:00
|
|
|
// These functions are overrode at runtime after the parsing.
|
2018-02-04 18:45:07 -05:00
|
|
|
"elapsed": func(timezone string, t time.Time) string {
|
2018-04-28 01:07:46 -04:00
|
|
|
return ""
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
|
|
|
"t": func(key interface{}, args ...interface{}) string {
|
2018-04-28 01:07:46 -04:00
|
|
|
return ""
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
|
|
|
"plural": func(key string, n int, args ...interface{}) string {
|
2018-04-28 01:07:46 -04:00
|
|
|
return ""
|
2018-02-04 18:45:07 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:45:56 -04: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)
|
2021-03-23 00:04:10 -04:00
|
|
|
return err == nil
|
2018-09-21 21:53:29 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 20:56:17 -04:00
|
|
|
// Returns the duration in human readable format (hours and minutes).
|
|
|
|
func duration(t time.Time) string {
|
2023-11-08 00:46:15 -05:00
|
|
|
return durationImpl(t, time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accepts now argument for easy testing
|
|
|
|
func durationImpl(t time.Time, now time.Time) string {
|
2023-10-17 20:56:17 -04:00
|
|
|
if t.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-11-08 00:46:15 -05:00
|
|
|
diff := t.Sub(now)
|
2023-10-17 20:56:17 -04:00
|
|
|
|
|
|
|
if diff < 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-11-08 00:46:15 -05:00
|
|
|
// Round to nearest second to get e.g. "14m56s" rather than "14m56.245483933s"
|
|
|
|
return diff.Round(time.Second).String()
|
2023-10-17 20:56:17 -04:00
|
|
|
}
|
|
|
|
|
2018-09-22 18:04:55 -04:00
|
|
|
func elapsedTime(printer *locale.Printer, tz string, t time.Time) string {
|
2018-09-21 21:53:29 -04:00
|
|
|
if t.IsZero() {
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Printf("time_elapsed.not_yet")
|
2018-09-21 21:53:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
now := timezone.Now(tz)
|
|
|
|
t = timezone.Convert(tz, t)
|
|
|
|
if now.Before(t) {
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Printf("time_elapsed.not_yet")
|
2018-09-21 21:53:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
diff := now.Sub(t)
|
|
|
|
// Duration in seconds
|
|
|
|
s := diff.Seconds()
|
|
|
|
// Duration in days
|
|
|
|
d := int(s / 86400)
|
|
|
|
switch {
|
|
|
|
case s < 60:
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Printf("time_elapsed.now")
|
2018-09-21 21:53:29 -04:00
|
|
|
case s < 3600:
|
|
|
|
minutes := int(diff.Minutes())
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Plural("time_elapsed.minutes", minutes, minutes)
|
2018-09-21 21:53:29 -04:00
|
|
|
case s < 86400:
|
|
|
|
hours := int(diff.Hours())
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Plural("time_elapsed.hours", hours, hours)
|
2018-09-21 21:53:29 -04:00
|
|
|
case d == 1:
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Printf("time_elapsed.yesterday")
|
2018-11-11 06:43:42 -05:00
|
|
|
case d < 21:
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Plural("time_elapsed.days", d, d)
|
2018-09-21 21:53:29 -04:00
|
|
|
case d < 31:
|
2018-11-11 06:43:42 -05:00
|
|
|
weeks := int(math.Round(float64(d) / 7))
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Plural("time_elapsed.weeks", weeks, weeks)
|
2018-09-21 21:53:29 -04:00
|
|
|
case d < 365:
|
2018-11-11 06:43:42 -05:00
|
|
|
months := int(math.Round(float64(d) / 30))
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Plural("time_elapsed.months", months, months)
|
2018-09-21 21:53:29 -04:00
|
|
|
default:
|
2018-11-11 06:43:42 -05:00
|
|
|
years := int(math.Round(float64(d) / 365))
|
2018-09-22 18:04:55 -04:00
|
|
|
return printer.Plural("time_elapsed.years", years, years)
|
2018-09-21 21:53:29 -04:00
|
|
|
}
|
|
|
|
}
|
2018-12-03 00:09:53 -05:00
|
|
|
|
2019-11-29 13:27:25 -05: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])
|
|
|
|
}
|