functions.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package template
  5. import (
  6. "html/template"
  7. "net/mail"
  8. "strings"
  9. "time"
  10. "github.com/gorilla/mux"
  11. "github.com/miniflux/miniflux/config"
  12. "github.com/miniflux/miniflux/filter"
  13. "github.com/miniflux/miniflux/http/route"
  14. "github.com/miniflux/miniflux/url"
  15. )
  16. type funcMap struct {
  17. cfg *config.Config
  18. router *mux.Router
  19. }
  20. func (f *funcMap) Map() template.FuncMap {
  21. return template.FuncMap{
  22. "baseURL": func() string {
  23. return f.cfg.BaseURL()
  24. },
  25. "rootURL": func() string {
  26. return f.cfg.RootURL()
  27. },
  28. "hasOAuth2Provider": func(provider string) bool {
  29. return f.cfg.OAuth2Provider() == provider
  30. },
  31. "hasKey": func(dict map[string]string, key string) bool {
  32. if value, found := dict[key]; found {
  33. return value != ""
  34. }
  35. return false
  36. },
  37. "route": func(name string, args ...interface{}) string {
  38. return route.Path(f.router, name, args...)
  39. },
  40. "noescape": func(str string) template.HTML {
  41. return template.HTML(str)
  42. },
  43. "proxyFilter": func(data string) string {
  44. return filter.ImageProxyFilter(f.router, f.cfg, data)
  45. },
  46. "proxyURL": func(link string) string {
  47. proxyImages := f.cfg.ProxyImages()
  48. if proxyImages == "all" || (proxyImages != "none" && !url.IsHTTPS(link)) {
  49. return filter.Proxify(f.router, link)
  50. }
  51. return link
  52. },
  53. "domain": func(websiteURL string) string {
  54. return url.Domain(websiteURL)
  55. },
  56. "isEmail": func(str string) bool {
  57. _, err := mail.ParseAddress(str)
  58. if err != nil {
  59. return false
  60. }
  61. return true
  62. },
  63. "hasPrefix": func(str, prefix string) bool {
  64. return strings.HasPrefix(str, prefix)
  65. },
  66. "contains": func(str, substr string) bool {
  67. return strings.Contains(str, substr)
  68. },
  69. "isodate": func(ts time.Time) string {
  70. return ts.Format("2006-01-02 15:04:05")
  71. },
  72. "dict": dict,
  73. // These functions are overrided at runtime after the parsing.
  74. "elapsed": func(timezone string, t time.Time) string {
  75. return ""
  76. },
  77. "t": func(key interface{}, args ...interface{}) string {
  78. return ""
  79. },
  80. "plural": func(key string, n int, args ...interface{}) string {
  81. return ""
  82. },
  83. }
  84. }
  85. func newFuncMap(cfg *config.Config, router *mux.Router) *funcMap {
  86. return &funcMap{cfg, router}
  87. }