template.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "strings"
  10. "time"
  11. )
  12. func Str2html(raw string) template.HTML {
  13. return template.HTML(raw)
  14. }
  15. func Range(l int) []int {
  16. return make([]int, l)
  17. }
  18. func List(l *list.List) chan interface{} {
  19. e := l.Front()
  20. c := make(chan interface{})
  21. go func() {
  22. for e != nil {
  23. c <- e.Value
  24. e = e.Next()
  25. }
  26. close(c)
  27. }()
  28. return c
  29. }
  30. var TemplateFuncs template.FuncMap = map[string]interface{}{
  31. "AppName": func() string {
  32. return AppName
  33. },
  34. "AppVer": func() string {
  35. return AppVer
  36. },
  37. "AppDomain": func() string {
  38. return Domain
  39. },
  40. "LoadTimes": func(startTime time.Time) string {
  41. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  42. },
  43. "AvatarLink": AvatarLink,
  44. "str2html": Str2html,
  45. "TimeSince": TimeSince,
  46. "FileSize": FileSize,
  47. "Subtract": Subtract,
  48. "ActionIcon": ActionIcon,
  49. "ActionDesc": ActionDesc,
  50. "DateFormat": DateFormat,
  51. "List": List,
  52. "Mail2Domain": func(mail string) string {
  53. return "mail." + strings.Split(mail, "@")[1]
  54. },
  55. "SubStr": func(str string, start, length int) string {
  56. return str[start : start+length]
  57. },
  58. }