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