template.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package base
  5. import (
  6. "container/list"
  7. "fmt"
  8. "html/template"
  9. "time"
  10. )
  11. func Str2html(raw string) template.HTML {
  12. return template.HTML(raw)
  13. }
  14. func Range(l int) []int {
  15. return make([]int, l)
  16. }
  17. func List(l *list.List) chan interface{} {
  18. e := l.Front()
  19. c := make(chan interface{})
  20. go func() {
  21. for e != nil {
  22. c <- e.Value
  23. e = e.Next()
  24. }
  25. close(c)
  26. }()
  27. return c
  28. }
  29. var TemplateFuncs template.FuncMap = map[string]interface{}{
  30. "AppName": func() string {
  31. return AppName
  32. },
  33. "AppVer": func() string {
  34. return AppVer
  35. },
  36. "AppDomain": func() string {
  37. return Domain
  38. },
  39. "LoadTimes": func(startTime time.Time) string {
  40. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  41. },
  42. "AvatarLink": AvatarLink,
  43. "str2html": Str2html,
  44. "TimeSince": TimeSince,
  45. "FileSize": FileSize,
  46. "Subtract": Subtract,
  47. "ActionIcon": ActionIcon,
  48. "ActionDesc": ActionDesc,
  49. "DateFormat": DateFormat,
  50. "List": List,
  51. }