You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template.go 1.6KB

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