template.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. "IsProdMode": func() bool {
  52. return IsProdMode
  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. }
  87. type Actioner interface {
  88. GetOpType() int
  89. GetActUserName() string
  90. GetActEmail() string
  91. GetRepoName() string
  92. GetBranch() string
  93. GetContent() string
  94. }
  95. // ActionIcon accepts a int that represents action operation type
  96. // and returns a icon class name.
  97. func ActionIcon(opType int) string {
  98. switch opType {
  99. case 1: // Create repository.
  100. return "plus-circle"
  101. case 5: // Commit repository.
  102. return "arrow-circle-o-right"
  103. case 6: // Create issue.
  104. return "exclamation-circle"
  105. case 8: // Transfer repository.
  106. return "share"
  107. default:
  108. return "invalid type"
  109. }
  110. }
  111. const (
  112. TPL_CREATE_REPO = `<a href="/user/%s">%s</a> created repository <a href="/%s">%s</a>`
  113. 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`
  114. TPL_COMMIT_REPO_LI = `<div><img src="%s?s=16" alt="user-avatar"/> <a href="/%s/commit/%s">%s</a> %s</div>`
  115. TPL_CREATE_ISSUE = `<a href="/user/%s">%s</a> opened issue <a href="/%s/issues/%s">%s#%s</a>
  116. <div><img src="%s?s=16" alt="user-avatar"/> %s</div>`
  117. TPL_TRANSFER_REPO = `<a href="/user/%s">%s</a> transfered repository <code>%s</code> to <a href="/%s">%s</a>`
  118. )
  119. type PushCommit struct {
  120. Sha1 string
  121. Message string
  122. AuthorEmail string
  123. AuthorName string
  124. }
  125. type PushCommits struct {
  126. Len int
  127. Commits []*PushCommit
  128. }
  129. // ActionDesc accepts int that represents action operation type
  130. // and returns the description.
  131. func ActionDesc(act Actioner) string {
  132. actUserName := act.GetActUserName()
  133. email := act.GetActEmail()
  134. repoName := act.GetRepoName()
  135. repoLink := actUserName + "/" + repoName
  136. branch := act.GetBranch()
  137. content := act.GetContent()
  138. switch act.GetOpType() {
  139. case 1: // Create repository.
  140. return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName)
  141. case 5: // Commit repository.
  142. var push *PushCommits
  143. if err := json.Unmarshal([]byte(content), &push); err != nil {
  144. return err.Error()
  145. }
  146. buf := bytes.NewBuffer([]byte("\n"))
  147. for _, commit := range push.Commits {
  148. buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, AvatarLink(commit.AuthorEmail), repoLink, commit.Sha1, commit.Sha1[:7], commit.Message) + "\n")
  149. }
  150. if push.Len > 3 {
  151. buf.WriteString(fmt.Sprintf(`<div><a href="/%s/%s/commits/%s">%d other commits >></a></div>`, actUserName, repoName, branch, push.Len))
  152. }
  153. return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink,
  154. buf.String())
  155. case 6: // Create issue.
  156. infos := strings.SplitN(content, "|", 2)
  157. return fmt.Sprintf(TPL_CREATE_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0],
  158. AvatarLink(email), infos[1])
  159. case 8: // Transfer repository.
  160. newRepoLink := content + "/" + repoName
  161. return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink)
  162. default:
  163. return "invalid type"
  164. }
  165. }
  166. func DiffTypeToStr(diffType int) string {
  167. diffTypes := map[int]string{
  168. 1: "add", 2: "modify", 3: "del",
  169. }
  170. return diffTypes[diffType]
  171. }
  172. func DiffLineTypeToStr(diffType int) string {
  173. switch diffType {
  174. case 2:
  175. return "add"
  176. case 3:
  177. return "del"
  178. case 4:
  179. return "tag"
  180. }
  181. return "same"
  182. }
  183. const (
  184. TPL_GO_GET_META = `<meta name="go-import" content="%s git %s">`
  185. )
  186. func GetGoGetMetaList() []byte {
  187. buf := bytes.NewBuffer([]byte(""))
  188. for meta := range GoGetMetas {
  189. buf.WriteString(fmt.Sprintf(TPL_GO_GET_META, Domain, meta))
  190. }
  191. return buf.Bytes()
  192. }