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

template.go 7.5 kB

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