template.go 6.7 KB

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