functions.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "bytes"
  7. "html/template"
  8. "net/mail"
  9. "strings"
  10. "time"
  11. "github.com/gorilla/mux"
  12. "github.com/miniflux/miniflux/config"
  13. "github.com/miniflux/miniflux/filter"
  14. "github.com/miniflux/miniflux/http/route"
  15. "github.com/miniflux/miniflux/url"
  16. )
  17. type funcMap struct {
  18. cfg *config.Config
  19. router *mux.Router
  20. }
  21. func (f *funcMap) Map() template.FuncMap {
  22. return template.FuncMap{
  23. "baseURL": func() string {
  24. return f.cfg.BaseURL()
  25. },
  26. "rootURL": func() string {
  27. return f.cfg.RootURL()
  28. },
  29. "hasOAuth2Provider": func(provider string) bool {
  30. return f.cfg.OAuth2Provider() == provider
  31. },
  32. "hasKey": func(dict map[string]string, key string) bool {
  33. if value, found := dict[key]; found {
  34. return value != ""
  35. }
  36. return false
  37. },
  38. "route": func(name string, args ...interface{}) string {
  39. return route.Path(f.router, name, args...)
  40. },
  41. "noescape": func(str string) template.HTML {
  42. return template.HTML(str)
  43. },
  44. "proxyFilter": func(data string) string {
  45. return filter.ImageProxyFilter(f.router, f.cfg, data)
  46. },
  47. "proxyURL": func(link string) string {
  48. proxyImages := f.cfg.ProxyImages()
  49. if proxyImages == "all" || (proxyImages != "none" && !url.IsHTTPS(link)) {
  50. return filter.Proxify(f.router, link)
  51. }
  52. return link
  53. },
  54. "domain": func(websiteURL string) string {
  55. return url.Domain(websiteURL)
  56. },
  57. "isEmail": func(str string) bool {
  58. _, err := mail.ParseAddress(str)
  59. if err != nil {
  60. return false
  61. }
  62. return true
  63. },
  64. "hasPrefix": func(str, prefix string) bool {
  65. return strings.HasPrefix(str, prefix)
  66. },
  67. "contains": func(str, substr string) bool {
  68. return strings.Contains(str, substr)
  69. },
  70. "isodate": func(ts time.Time) string {
  71. return ts.Format("2006-01-02 15:04:05")
  72. },
  73. "dict": dict,
  74. "truncate": func(str string, max int) string {
  75. if len(str) > max {
  76. var buffer bytes.Buffer
  77. buffer.WriteString(str[:max-1])
  78. buffer.WriteString("…")
  79. return buffer.String()
  80. }
  81. return str
  82. },
  83. // These functions are overrided at runtime after the parsing.
  84. "elapsed": func(timezone string, t time.Time) string {
  85. return ""
  86. },
  87. "t": func(key interface{}, args ...interface{}) string {
  88. return ""
  89. },
  90. "plural": func(key string, n int, args ...interface{}) string {
  91. return ""
  92. },
  93. }
  94. }
  95. func newFuncMap(cfg *config.Config, router *mux.Router) *funcMap {
  96. return &funcMap{cfg, router}
  97. }