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

template.go 5.0 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "bytes"
  7. "container/list"
  8. "encoding/json"
  9. "fmt"
  10. "html/template"
  11. "strings"
  12. "time"
  13. )
  14. func Str2html(raw string) template.HTML {
  15. return template.HTML(raw)
  16. }
  17. func Range(l int) []int {
  18. return make([]int, l)
  19. }
  20. func List(l *list.List) chan interface{} {
  21. e := l.Front()
  22. c := make(chan interface{})
  23. go func() {
  24. for e != nil {
  25. c <- e.Value
  26. e = e.Next()
  27. }
  28. close(c)
  29. }()
  30. return c
  31. }
  32. func ShortSha(sha1 string) string {
  33. if len(sha1) == 40 {
  34. return sha1[:10]
  35. }
  36. return sha1
  37. }
  38. var mailDomains = map[string]string{
  39. "gmail.com": "gmail.com",
  40. }
  41. var TemplateFuncs template.FuncMap = map[string]interface{}{
  42. "AppName": func() string {
  43. return AppName
  44. },
  45. "AppVer": func() string {
  46. return AppVer
  47. },
  48. "AppDomain": func() string {
  49. return Domain
  50. },
  51. "LoadTimes": func(startTime time.Time) string {
  52. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  53. },
  54. "AvatarLink": AvatarLink,
  55. "str2html": Str2html,
  56. "TimeSince": TimeSince,
  57. "FileSize": FileSize,
  58. "Subtract": Subtract,
  59. "ActionIcon": ActionIcon,
  60. "ActionDesc": ActionDesc,
  61. "DateFormat": DateFormat,
  62. "List": List,
  63. "Mail2Domain": func(mail string) string {
  64. if !strings.Contains(mail, "@") {
  65. return "try.gogits.org"
  66. }
  67. suffix := strings.SplitN(mail, "@", 2)[1]
  68. domain, ok := mailDomains[suffix]
  69. if !ok {
  70. return "mail." + suffix
  71. }
  72. return domain
  73. },
  74. "SubStr": func(str string, start, length int) string {
  75. return str[start : start+length]
  76. },
  77. "DiffTypeToStr": DiffTypeToStr,
  78. "DiffLineTypeToStr": DiffLineTypeToStr,
  79. "ShortSha": ShortSha,
  80. }
  81. type Actioner interface {
  82. GetOpType() int
  83. GetActUserName() string
  84. GetActEmail() string
  85. GetRepoName() string
  86. GetBranch() string
  87. GetContent() string
  88. }
  89. // ActionIcon accepts a int that represents action operation type
  90. // and returns a icon class name.
  91. func ActionIcon(opType int) string {
  92. switch opType {
  93. case 1: // Create repository.
  94. return "plus-circle"
  95. case 5: // Commit repository.
  96. return "arrow-circle-o-right"
  97. case 6: // Create issue.
  98. return "exclamation-circle"
  99. case 8: // Transfer repository.
  100. return "share"
  101. default:
  102. return "invalid type"
  103. }
  104. }
  105. const (
  106. TPL_CREATE_REPO = `<a href="/user/%s">%s</a> created repository <a href="/%s">%s</a>`
  107. TPL_COMMIT_REPO = `<a href="/user/%s">%s</a> pushed to <a href="/%s/src/%s">%s</a> at <a href="/%s">%s</a>%s`
  108. TPL_COMMIT_REPO_LI = `<div><img src="%s?s=16" alt="user-avatar"/> <a href="/%s/commit/%s">%s</a> %s</div>`
  109. TPL_CREATE_ISSUE = `<a href="/user/%s">%s</a> opened issue <a href="/%s/issues/%s">%s#%s</a>
  110. <div><img src="%s?s=16" alt="user-avatar"/> %s</div>`
  111. TPL_TRANSFER_REPO = `<a href="/user/%s">%s</a> transfered repository <code>%s</code> to <a href="/%s">%s</a>`
  112. )
  113. type PushCommit struct {
  114. Sha1 string
  115. Message string
  116. AuthorEmail string
  117. AuthorName string
  118. }
  119. type PushCommits struct {
  120. Len int
  121. Commits []*PushCommit
  122. }
  123. // ActionDesc accepts int that represents action operation type
  124. // and returns the description.
  125. func ActionDesc(act Actioner) string {
  126. actUserName := act.GetActUserName()
  127. email := act.GetActEmail()
  128. repoName := act.GetRepoName()
  129. repoLink := actUserName + "/" + repoName
  130. branch := act.GetBranch()
  131. content := act.GetContent()
  132. switch act.GetOpType() {
  133. case 1: // Create repository.
  134. return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName)
  135. case 5: // Commit repository.
  136. var push *PushCommits
  137. if err := json.Unmarshal([]byte(content), &push); err != nil {
  138. return err.Error()
  139. }
  140. buf := bytes.NewBuffer([]byte("\n"))
  141. for _, commit := range push.Commits {
  142. buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, AvatarLink(commit.AuthorEmail), repoLink, commit.Sha1, commit.Sha1[:7], commit.Message) + "\n")
  143. }
  144. if push.Len > 3 {
  145. buf.WriteString(fmt.Sprintf(`<div><a href="/%s/%s/commits/%s">%d other commits >></a></div>`, actUserName, repoName, branch, push.Len))
  146. }
  147. return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink,
  148. buf.String())
  149. case 6: // Create issue.
  150. infos := strings.SplitN(content, "|", 2)
  151. return fmt.Sprintf(TPL_CREATE_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0],
  152. AvatarLink(email), infos[1])
  153. case 8: // Transfer repository.
  154. newRepoLink := content + "/" + repoName
  155. return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink)
  156. default:
  157. return "invalid type"
  158. }
  159. }
  160. func DiffTypeToStr(diffType int) string {
  161. diffTypes := map[int]string{
  162. 1: "add", 2: "modify", 3: "del",
  163. }
  164. return diffTypes[diffType]
  165. }
  166. func DiffLineTypeToStr(diffType int) string {
  167. switch diffType {
  168. case 2:
  169. return "add"
  170. case 3:
  171. return "del"
  172. case 4:
  173. return "tag"
  174. }
  175. return "same"
  176. }