template.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 mailDomains = map[string]string{
  31. "gmail.com": "gmail.com",
  32. }
  33. var TemplateFuncs template.FuncMap = map[string]interface{}{
  34. "AppName": func() string {
  35. return AppName
  36. },
  37. "AppVer": func() string {
  38. return AppVer
  39. },
  40. "AppDomain": func() string {
  41. return Domain
  42. },
  43. "LoadTimes": func(startTime time.Time) string {
  44. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  45. },
  46. "AvatarLink": AvatarLink,
  47. "str2html": Str2html,
  48. "TimeSince": TimeSince,
  49. "FileSize": FileSize,
  50. "Subtract": Subtract,
  51. "ActionIcon": ActionIcon,
  52. "ActionDesc": ActionDesc,
  53. "DateFormat": DateFormat,
  54. "List": List,
  55. "Mail2Domain": func(mail string) string {
  56. suffix := strings.SplitN(mail, "@", 2)[1]
  57. domain, ok := mailDomains[suffix]
  58. if !ok {
  59. return "mail." + suffix
  60. }
  61. return domain
  62. },
  63. "SubStr": func(str string, start, length int) string {
  64. return str[start : start+length]
  65. },
  66. "DiffTypeToStr": DiffTypeToStr,
  67. }