functions.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 // import "miniflux.app/template"
  5. import (
  6. "fmt"
  7. "math"
  8. "html/template"
  9. "net/mail"
  10. "strings"
  11. "time"
  12. "github.com/gorilla/mux"
  13. "miniflux.app/config"
  14. "miniflux.app/filter"
  15. "miniflux.app/http/route"
  16. "miniflux.app/locale"
  17. "miniflux.app/model"
  18. "miniflux.app/timezone"
  19. "miniflux.app/url"
  20. )
  21. type funcMap struct {
  22. cfg *config.Config
  23. router *mux.Router
  24. }
  25. // Map returns a map of template functions that are compiled during template parsing.
  26. func (f *funcMap) Map() template.FuncMap {
  27. return template.FuncMap{
  28. "dict": dict,
  29. "hasKey": hasKey,
  30. "truncate": truncate,
  31. "isEmail": isEmail,
  32. "baseURL": func() string {
  33. return f.cfg.BaseURL()
  34. },
  35. "rootURL": func() string {
  36. return f.cfg.RootURL()
  37. },
  38. "hasOAuth2Provider": func(provider string) bool {
  39. return f.cfg.OAuth2Provider() == provider
  40. },
  41. "route": func(name string, args ...interface{}) string {
  42. return route.Path(f.router, name, args...)
  43. },
  44. "noescape": func(str string) template.HTML {
  45. return template.HTML(str)
  46. },
  47. "proxyFilter": func(data string) string {
  48. return filter.ImageProxyFilter(f.router, f.cfg, data)
  49. },
  50. "proxyURL": func(link string) string {
  51. proxyImages := f.cfg.ProxyImages()
  52. if proxyImages == "all" || (proxyImages != "none" && !url.IsHTTPS(link)) {
  53. return filter.Proxify(f.router, link)
  54. }
  55. return link
  56. },
  57. "domain": func(websiteURL string) string {
  58. return url.Domain(websiteURL)
  59. },
  60. "hasPrefix": func(str, prefix string) bool {
  61. return strings.HasPrefix(str, prefix)
  62. },
  63. "contains": func(str, substr string) bool {
  64. return strings.Contains(str, substr)
  65. },
  66. "isodate": func(ts time.Time) string {
  67. return ts.Format("2006-01-02 15:04:05")
  68. },
  69. "theme_color": func(theme string) string {
  70. return model.ThemeColor(theme)
  71. },
  72. // These functions are overrided at runtime after the parsing.
  73. "elapsed": func(timezone string, t time.Time) string {
  74. return ""
  75. },
  76. "t": func(key interface{}, args ...interface{}) string {
  77. return ""
  78. },
  79. "plural": func(key string, n int, args ...interface{}) string {
  80. return ""
  81. },
  82. }
  83. }
  84. func newFuncMap(cfg *config.Config, router *mux.Router) *funcMap {
  85. return &funcMap{cfg, router}
  86. }
  87. func dict(values ...interface{}) (map[string]interface{}, error) {
  88. if len(values)%2 != 0 {
  89. return nil, fmt.Errorf("dict expects an even number of arguments")
  90. }
  91. dict := make(map[string]interface{}, len(values)/2)
  92. for i := 0; i < len(values); i += 2 {
  93. key, ok := values[i].(string)
  94. if !ok {
  95. return nil, fmt.Errorf("dict keys must be strings")
  96. }
  97. dict[key] = values[i+1]
  98. }
  99. return dict, nil
  100. }
  101. func hasKey(dict map[string]string, key string) bool {
  102. if value, found := dict[key]; found {
  103. return value != ""
  104. }
  105. return false
  106. }
  107. func truncate(str string, max int) string {
  108. runes := 0
  109. for i := range str {
  110. runes++
  111. if runes > max {
  112. return str[:i] + "…"
  113. }
  114. }
  115. return str
  116. }
  117. func isEmail(str string) bool {
  118. _, err := mail.ParseAddress(str)
  119. if err != nil {
  120. return false
  121. }
  122. return true
  123. }
  124. func elapsedTime(printer *locale.Printer, tz string, t time.Time) string {
  125. if t.IsZero() {
  126. return printer.Printf("time_elapsed.not_yet")
  127. }
  128. now := timezone.Now(tz)
  129. t = timezone.Convert(tz, t)
  130. if now.Before(t) {
  131. return printer.Printf("time_elapsed.not_yet")
  132. }
  133. diff := now.Sub(t)
  134. // Duration in seconds
  135. s := diff.Seconds()
  136. // Duration in days
  137. d := int(s / 86400)
  138. switch {
  139. case s < 60:
  140. return printer.Printf("time_elapsed.now")
  141. case s < 3600:
  142. minutes := int(diff.Minutes())
  143. return printer.Plural("time_elapsed.minutes", minutes, minutes)
  144. case s < 86400:
  145. hours := int(diff.Hours())
  146. return printer.Plural("time_elapsed.hours", hours, hours)
  147. case d == 1:
  148. return printer.Printf("time_elapsed.yesterday")
  149. case d < 21:
  150. return printer.Plural("time_elapsed.days", d, d)
  151. case d < 31:
  152. weeks := int(math.Round(float64(d) / 7))
  153. return printer.Plural("time_elapsed.weeks", weeks, weeks)
  154. case d < 365:
  155. months := int(math.Round(float64(d) / 30))
  156. return printer.Plural("time_elapsed.months", months, months)
  157. default:
  158. years := int(math.Round(float64(d) / 365))
  159. return printer.Plural("time_elapsed.years", years, years)
  160. }
  161. }