template.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. "runtime"
  12. "strings"
  13. "time"
  14. )
  15. func Str2html(raw string) template.HTML {
  16. return template.HTML(raw)
  17. }
  18. func Range(l int) []int {
  19. return make([]int, l)
  20. }
  21. func List(l *list.List) chan interface{} {
  22. e := l.Front()
  23. c := make(chan interface{})
  24. go func() {
  25. for e != nil {
  26. c <- e.Value
  27. e = e.Next()
  28. }
  29. close(c)
  30. }()
  31. return c
  32. }
  33. func ShortSha(sha1 string) string {
  34. if len(sha1) == 40 {
  35. return sha1[:10]
  36. }
  37. return sha1
  38. }
  39. var mailDomains = map[string]string{
  40. "gmail.com": "gmail.com",
  41. }
  42. var TemplateFuncs template.FuncMap = map[string]interface{}{
  43. "GoVer": func() string {
  44. return runtime.Version()
  45. },
  46. "AppName": func() string {
  47. return AppName
  48. },
  49. "AppVer": func() string {
  50. return AppVer
  51. },
  52. "AppDomain": func() string {
  53. return Domain
  54. },
  55. "CdnMode": func() bool {
  56. return ProdMode && !OfflineMode
  57. },
  58. "LoadTimes": func(startTime time.Time) string {
  59. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  60. },
  61. "AvatarLink": AvatarLink,
  62. "str2html": Str2html,
  63. "TimeSince": TimeSince,
  64. "FileSize": FileSize,
  65. "Subtract": Subtract,
  66. "Add": func(a, b int) int {
  67. return a + b
  68. },
  69. "ActionIcon": ActionIcon,
  70. "ActionDesc": ActionDesc,
  71. "DateFormat": DateFormat,
  72. "List": List,
  73. "Mail2Domain": func(mail string) string {
  74. if !strings.Contains(mail, "@") {
  75. return "try.gogits.org"
  76. }
  77. suffix := strings.SplitN(mail, "@", 2)[1]
  78. domain, ok := mailDomains[suffix]
  79. if !ok {
  80. return "mail." + suffix
  81. }
  82. return domain
  83. },
  84. "SubStr": func(str string, start, length int) string {
  85. return str[start : start+length]
  86. },
  87. "DiffTypeToStr": DiffTypeToStr,
  88. "DiffLineTypeToStr": DiffLineTypeToStr,
  89. "ShortSha": ShortSha,
  90. "Oauth2Icon": Oauth2Icon,
  91. "Oauth2Name": Oauth2Name,
  92. }
  93. type Actioner interface {
  94. GetOpType() int
  95. GetActUserName() string
  96. GetActEmail() string
  97. GetRepoUserName() string
  98. GetRepoName() string
  99. GetBranch() string
  100. GetContent() string
  101. }
  102. // ActionIcon accepts a int that represents action operation type
  103. // and returns a icon class name.
  104. func ActionIcon(opType int) string {
  105. switch opType {
  106. case 1: // Create repository.
  107. return "plus-circle"
  108. case 5, 9: // Commit repository.
  109. return "arrow-circle-o-right"
  110. case 6: // Create issue.
  111. return "exclamation-circle"
  112. case 8: // Transfer repository.
  113. return "share"
  114. case 10: // Comment issue.
  115. return "comment"
  116. default:
  117. return "invalid type"
  118. }
  119. }
  120. const (
  121. TPL_CREATE_REPO = `<a href="/user/%s">%s</a> created repository <a href="/%s">%s</a>`
  122. 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`
  123. TPL_COMMIT_REPO_LI = `<div><img src="%s?s=16" alt="user-avatar"/> <a href="/%s/commit/%s" rel="nofollow">%s</a> %s</div>`
  124. TPL_CREATE_ISSUE = `<a href="/user/%s">%s</a> opened issue <a href="/%s/issues/%s">%s#%s</a>
  125. <div><img src="%s?s=16" alt="user-avatar"/> %s</div>`
  126. TPL_TRANSFER_REPO = `<a href="/user/%s">%s</a> transfered repository <code>%s</code> to <a href="/%s">%s</a>`
  127. 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>`
  128. TPL_COMMENT_ISSUE = `<a href="/user/%s">%s</a> commented on issue <a href="/%s/issues/%s">%s#%s</a>
  129. <div><img src="%s?s=16" alt="user-avatar"/> %s</div>`
  130. )
  131. type PushCommit struct {
  132. Sha1 string
  133. Message string
  134. AuthorEmail string
  135. AuthorName string
  136. }
  137. type PushCommits struct {
  138. Len int
  139. Commits []*PushCommit
  140. }
  141. // ActionDesc accepts int that represents action operation type
  142. // and returns the description.
  143. func ActionDesc(act Actioner) string {
  144. actUserName := act.GetActUserName()
  145. email := act.GetActEmail()
  146. repoUserName := act.GetRepoUserName()
  147. repoName := act.GetRepoName()
  148. repoLink := repoUserName + "/" + repoName
  149. branch := act.GetBranch()
  150. content := act.GetContent()
  151. switch act.GetOpType() {
  152. case 1: // Create repository.
  153. return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName)
  154. case 5: // Commit repository.
  155. var push *PushCommits
  156. if err := json.Unmarshal([]byte(content), &push); err != nil {
  157. return err.Error()
  158. }
  159. buf := bytes.NewBuffer([]byte("\n"))
  160. for _, commit := range push.Commits {
  161. buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, AvatarLink(commit.AuthorEmail), repoLink, commit.Sha1, commit.Sha1[:7], commit.Message) + "\n")
  162. }
  163. if push.Len > 3 {
  164. buf.WriteString(fmt.Sprintf(`<div><a href="/%s/%s/commits/%s" rel="nofollow">%d other commits >></a></div>`, actUserName, repoName, branch, push.Len))
  165. }
  166. return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink,
  167. buf.String())
  168. case 6: // Create issue.
  169. infos := strings.SplitN(content, "|", 2)
  170. return fmt.Sprintf(TPL_CREATE_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0],
  171. AvatarLink(email), infos[1])
  172. case 8: // Transfer repository.
  173. newRepoLink := content + "/" + repoName
  174. return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink)
  175. case 9: // Push tag.
  176. return fmt.Sprintf(TPL_PUSH_TAG, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink)
  177. case 10: // Comment issue.
  178. infos := strings.SplitN(content, "|", 2)
  179. return fmt.Sprintf(TPL_COMMENT_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0],
  180. AvatarLink(email), infos[1])
  181. default:
  182. return "invalid type"
  183. }
  184. }
  185. func DiffTypeToStr(diffType int) string {
  186. diffTypes := map[int]string{
  187. 1: "add", 2: "modify", 3: "del",
  188. }
  189. return diffTypes[diffType]
  190. }
  191. func DiffLineTypeToStr(diffType int) string {
  192. switch diffType {
  193. case 2:
  194. return "add"
  195. case 3:
  196. return "del"
  197. case 4:
  198. return "tag"
  199. }
  200. return "same"
  201. }
  202. func Oauth2Icon(t int) string {
  203. switch t {
  204. case 1:
  205. return "fa-github-square"
  206. case 2:
  207. return "fa-google-plus-square"
  208. case 3:
  209. return "fa-twitter-square"
  210. case 4:
  211. return "fa-linux"
  212. case 5:
  213. return "fa-weibo"
  214. }
  215. return ""
  216. }
  217. func Oauth2Name(t int) string {
  218. switch t {
  219. case 1:
  220. return "GitHub"
  221. case 2:
  222. return "Google"
  223. case 3:
  224. return "Twitter"
  225. case 4:
  226. return "Tencent QQ"
  227. case 5:
  228. return "Weibo"
  229. }
  230. return ""
  231. }