template.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "CdnMode": func() bool {
  52. return ProdMode && !OfflineMode
  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. "Oauth2Name": Oauth2Name,
  88. }
  89. type Actioner interface {
  90. GetOpType() int
  91. GetActUserName() string
  92. GetActEmail() string
  93. GetRepoName() string
  94. GetBranch() string
  95. GetContent() string
  96. }
  97. // ActionIcon accepts a int that represents action operation type
  98. // and returns a icon class name.
  99. func ActionIcon(opType int) string {
  100. switch opType {
  101. case 1: // Create repository.
  102. return "plus-circle"
  103. case 5, 9: // Commit repository.
  104. return "arrow-circle-o-right"
  105. case 6: // Create issue.
  106. return "exclamation-circle"
  107. case 8: // Transfer repository.
  108. return "share"
  109. default:
  110. return "invalid type"
  111. }
  112. }
  113. const (
  114. TPL_CREATE_REPO = `<a href="/user/%s">%s</a> created repository <a href="/%s">%s</a>`
  115. 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`
  116. TPL_COMMIT_REPO_LI = `<div><img src="%s?s=16" alt="user-avatar"/> <a href="/%s/commit/%s" rel="nofollow">%s</a> %s</div>`
  117. TPL_CREATE_ISSUE = `<a href="/user/%s">%s</a> opened issue <a href="/%s/issues/%s">%s#%s</a>
  118. <div><img src="%s?s=16" alt="user-avatar"/> %s</div>`
  119. TPL_TRANSFER_REPO = `<a href="/user/%s">%s</a> transfered repository <code>%s</code> to <a href="/%s">%s</a>`
  120. TPL_PUSH_TAG = `<a href="/user/%s">%s</a> pushed tag <a href="/%s/src/%s" rel="nofollow">%s</a> at <a href="/%s">%s</a>`
  121. )
  122. type PushCommit struct {
  123. Sha1 string
  124. Message string
  125. AuthorEmail string
  126. AuthorName string
  127. }
  128. type PushCommits struct {
  129. Len int
  130. Commits []*PushCommit
  131. }
  132. // ActionDesc accepts int that represents action operation type
  133. // and returns the description.
  134. func ActionDesc(act Actioner) string {
  135. actUserName := act.GetActUserName()
  136. email := act.GetActEmail()
  137. repoName := act.GetRepoName()
  138. repoLink := actUserName + "/" + repoName
  139. branch := act.GetBranch()
  140. content := act.GetContent()
  141. switch act.GetOpType() {
  142. case 1: // Create repository.
  143. return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName)
  144. case 5: // Commit repository.
  145. var push *PushCommits
  146. if err := json.Unmarshal([]byte(content), &push); err != nil {
  147. return err.Error()
  148. }
  149. buf := bytes.NewBuffer([]byte("\n"))
  150. for _, commit := range push.Commits {
  151. buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, AvatarLink(commit.AuthorEmail), repoLink, commit.Sha1, commit.Sha1[:7], commit.Message) + "\n")
  152. }
  153. if push.Len > 3 {
  154. buf.WriteString(fmt.Sprintf(`<div><a href="/%s/%s/commits/%s" rel="nofollow">%d other commits >></a></div>`, actUserName, repoName, branch, push.Len))
  155. }
  156. return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink,
  157. buf.String())
  158. case 6: // Create issue.
  159. infos := strings.SplitN(content, "|", 2)
  160. return fmt.Sprintf(TPL_CREATE_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0],
  161. AvatarLink(email), infos[1])
  162. case 8: // Transfer repository.
  163. newRepoLink := content + "/" + repoName
  164. return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink)
  165. case 9: // Push tag.
  166. return fmt.Sprintf(TPL_PUSH_TAG, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink)
  167. default:
  168. return "invalid type"
  169. }
  170. }
  171. func DiffTypeToStr(diffType int) string {
  172. diffTypes := map[int]string{
  173. 1: "add", 2: "modify", 3: "del",
  174. }
  175. return diffTypes[diffType]
  176. }
  177. func DiffLineTypeToStr(diffType int) string {
  178. switch diffType {
  179. case 2:
  180. return "add"
  181. case 3:
  182. return "del"
  183. case 4:
  184. return "tag"
  185. }
  186. return "same"
  187. }
  188. func Oauth2Icon(t int) string {
  189. switch t {
  190. case 1:
  191. return "fa-github-square"
  192. case 2:
  193. return "fa-google-plus-square"
  194. case 3:
  195. return "fa-twitter-square"
  196. case 4:
  197. return "fa-linux"
  198. case 5:
  199. return "fa-weibo"
  200. }
  201. return ""
  202. }
  203. func Oauth2Name(t int) string {
  204. switch t {
  205. case 1:
  206. return "GitHub"
  207. case 2:
  208. return "Google"
  209. case 3:
  210. return "Twitter"
  211. case 4:
  212. return "Tencent QQ"
  213. case 5:
  214. return "Weibo"
  215. }
  216. return ""
  217. }