template.go 6.7 KB

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