template.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. "container/list"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "html/template"
  11. "runtime"
  12. "strings"
  13. "time"
  14. "github.com/gogits/gogs/modules/mahonia"
  15. "github.com/gogits/gogs/modules/setting"
  16. "github.com/saintfish/chardet"
  17. )
  18. func Str2html(raw string) template.HTML {
  19. return template.HTML(raw)
  20. }
  21. func Range(l int) []int {
  22. return make([]int, l)
  23. }
  24. func List(l *list.List) chan interface{} {
  25. e := l.Front()
  26. c := make(chan interface{})
  27. go func() {
  28. for e != nil {
  29. c <- e.Value
  30. e = e.Next()
  31. }
  32. close(c)
  33. }()
  34. return c
  35. }
  36. func ShortSha(sha1 string) string {
  37. if len(sha1) == 40 {
  38. return sha1[:10]
  39. }
  40. return sha1
  41. }
  42. func ToUtf8WithErr(content []byte) (error, string) {
  43. detector := chardet.NewTextDetector()
  44. result, err := detector.DetectBest(content)
  45. if err != nil {
  46. return err, ""
  47. }
  48. if result.Charset == "utf8" {
  49. return nil, string(content)
  50. }
  51. decoder := mahonia.NewDecoder(result.Charset)
  52. if decoder != nil {
  53. return nil, decoder.ConvertString(string(content))
  54. }
  55. return errors.New("unknow char decoder"), string(content)
  56. }
  57. func ToUtf8(content string) string {
  58. _, res := ToUtf8WithErr([]byte(content))
  59. return res
  60. }
  61. var mailDomains = map[string]string{
  62. "gmail.com": "gmail.com",
  63. }
  64. var TemplateFuncs template.FuncMap = map[string]interface{}{
  65. "GoVer": func() string {
  66. return strings.Title(runtime.Version())
  67. },
  68. "AppName": func() string {
  69. return setting.AppName
  70. },
  71. "AppSubUrl": func() string {
  72. return setting.AppSubUrl
  73. },
  74. "AppVer": func() string {
  75. return setting.AppVer
  76. },
  77. "AppDomain": func() string {
  78. return setting.Domain
  79. },
  80. "CdnMode": func() bool {
  81. return setting.ProdMode && !setting.OfflineMode
  82. },
  83. "LoadTimes": func(startTime time.Time) string {
  84. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  85. },
  86. "AvatarLink": AvatarLink,
  87. "str2html": Str2html, // TODO: Legacy
  88. "Str2html": Str2html,
  89. "TimeSince": TimeSince,
  90. "FileSize": FileSize,
  91. "Subtract": Subtract,
  92. "Add": func(a, b int) int {
  93. return a + b
  94. },
  95. "ActionIcon": ActionIcon,
  96. "DateFormat": DateFormat,
  97. "List": List,
  98. "Mail2Domain": func(mail string) string {
  99. if !strings.Contains(mail, "@") {
  100. return "try.gogits.org"
  101. }
  102. suffix := strings.SplitN(mail, "@", 2)[1]
  103. domain, ok := mailDomains[suffix]
  104. if !ok {
  105. return "mail." + suffix
  106. }
  107. return domain
  108. },
  109. "SubStr": func(str string, start, length int) string {
  110. return str[start : start+length]
  111. },
  112. "DiffTypeToStr": DiffTypeToStr,
  113. "DiffLineTypeToStr": DiffLineTypeToStr,
  114. "ShortSha": ShortSha,
  115. "Md5": EncodeMd5,
  116. "ActionContent2Commits": ActionContent2Commits,
  117. "Oauth2Icon": Oauth2Icon,
  118. "Oauth2Name": Oauth2Name,
  119. "ToUtf8": ToUtf8,
  120. }
  121. type Actioner interface {
  122. GetOpType() int
  123. GetActUserName() string
  124. GetActEmail() string
  125. GetRepoUserName() string
  126. GetRepoName() string
  127. GetBranch() string
  128. GetContent() string
  129. }
  130. // ActionIcon accepts a int that represents action operation type
  131. // and returns a icon class name.
  132. func ActionIcon(opType int) string {
  133. switch opType {
  134. case 1, 8: // Create, transfer repository.
  135. return "repo"
  136. case 5, 9: // Commit repository.
  137. return "git-commit"
  138. case 6: // Create issue.
  139. return "issue-opened"
  140. case 10: // Comment issue.
  141. return "comment"
  142. default:
  143. return "invalid type"
  144. }
  145. }
  146. type PushCommit struct {
  147. Sha1 string
  148. Message string
  149. AuthorEmail string
  150. AuthorName string
  151. }
  152. type PushCommits struct {
  153. Len int
  154. Commits []*PushCommit
  155. CompareUrl string
  156. }
  157. func ActionContent2Commits(act Actioner) *PushCommits {
  158. var push *PushCommits
  159. if err := json.Unmarshal([]byte(act.GetContent()), &push); err != nil {
  160. return nil
  161. }
  162. return push
  163. }
  164. func DiffTypeToStr(diffType int) string {
  165. diffTypes := map[int]string{
  166. 1: "add", 2: "modify", 3: "del",
  167. }
  168. return diffTypes[diffType]
  169. }
  170. func DiffLineTypeToStr(diffType int) string {
  171. switch diffType {
  172. case 2:
  173. return "add"
  174. case 3:
  175. return "del"
  176. case 4:
  177. return "tag"
  178. }
  179. return "same"
  180. }
  181. func Oauth2Icon(t int) string {
  182. switch t {
  183. case 1:
  184. return "fa-github-square"
  185. case 2:
  186. return "fa-google-plus-square"
  187. case 3:
  188. return "fa-twitter-square"
  189. case 4:
  190. return "fa-qq"
  191. case 5:
  192. return "fa-weibo"
  193. }
  194. return ""
  195. }
  196. func Oauth2Name(t int) string {
  197. switch t {
  198. case 1:
  199. return "GitHub"
  200. case 2:
  201. return "Google+"
  202. case 3:
  203. return "Twitter"
  204. case 4:
  205. return "腾讯 QQ"
  206. case 5:
  207. return "Weibo"
  208. }
  209. return ""
  210. }