You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template.go 6.7KB

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