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