template.go 6.2 KB

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