functions.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/errors"
  13. "github.com/miniflux/miniflux/filter"
  14. "github.com/miniflux/miniflux/http/route"
  15. "github.com/miniflux/miniflux/locale"
  16. "github.com/miniflux/miniflux/url"
  17. )
  18. type funcMap struct {
  19. cfg *config.Config
  20. router *mux.Router
  21. Language *locale.Language
  22. }
  23. func (f *funcMap) Map() template.FuncMap {
  24. return template.FuncMap{
  25. "baseURL": func() string {
  26. return f.cfg.BaseURL()
  27. },
  28. "rootURL": func() string {
  29. return f.cfg.RootURL()
  30. },
  31. "hasOAuth2Provider": func(provider string) bool {
  32. return f.cfg.OAuth2Provider() == provider
  33. },
  34. "hasKey": func(dict map[string]string, key string) bool {
  35. if value, found := dict[key]; found {
  36. return value != ""
  37. }
  38. return false
  39. },
  40. "route": func(name string, args ...interface{}) string {
  41. return route.Path(f.router, name, args...)
  42. },
  43. "noescape": func(str string) template.HTML {
  44. return template.HTML(str)
  45. },
  46. "proxyFilter": func(data string) string {
  47. return filter.ImageProxyFilter(f.router, data)
  48. },
  49. "proxyURL": func(link string) string {
  50. if url.IsHTTPS(link) {
  51. return link
  52. }
  53. return filter.Proxify(f.router, link)
  54. },
  55. "domain": func(websiteURL string) string {
  56. return url.Domain(websiteURL)
  57. },
  58. "isEmail": func(str string) bool {
  59. _, err := mail.ParseAddress(str)
  60. if err != nil {
  61. return false
  62. }
  63. return true
  64. },
  65. "hasPrefix": func(str, prefix string) bool {
  66. return strings.HasPrefix(str, prefix)
  67. },
  68. "contains": func(str, substr string) bool {
  69. return strings.Contains(str, substr)
  70. },
  71. "isodate": func(ts time.Time) string {
  72. return ts.Format("2006-01-02 15:04:05")
  73. },
  74. "elapsed": func(timezone string, t time.Time) string {
  75. return elapsedTime(f.Language, timezone, t)
  76. },
  77. "t": func(key interface{}, args ...interface{}) string {
  78. switch key.(type) {
  79. case string:
  80. return f.Language.Get(key.(string), args...)
  81. case errors.LocalizedError:
  82. return key.(errors.LocalizedError).Localize(f.Language)
  83. case *errors.LocalizedError:
  84. return key.(*errors.LocalizedError).Localize(f.Language)
  85. case error:
  86. return key.(error).Error()
  87. default:
  88. return ""
  89. }
  90. },
  91. "plural": func(key string, n int, args ...interface{}) string {
  92. return f.Language.Plural(key, n, args...)
  93. },
  94. "dict": dict,
  95. }
  96. }
  97. func newFuncMap(cfg *config.Config, router *mux.Router, language *locale.Language) *funcMap {
  98. return &funcMap{cfg, router, language}
  99. }