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 4.7 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.gogs.io"
  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. if len(str) == 0 {
  111. return ""
  112. }
  113. end := start + length
  114. if length == -1 {
  115. end = len(str)
  116. }
  117. if len(str) < end {
  118. return str
  119. }
  120. return str[start:end]
  121. },
  122. "DiffTypeToStr": DiffTypeToStr,
  123. "DiffLineTypeToStr": DiffLineTypeToStr,
  124. "ShortSha": ShortSha,
  125. "Md5": EncodeMd5,
  126. "ActionContent2Commits": ActionContent2Commits,
  127. "Oauth2Icon": Oauth2Icon,
  128. "Oauth2Name": Oauth2Name,
  129. "ToUtf8": ToUtf8,
  130. }
  131. type Actioner interface {
  132. GetOpType() int
  133. GetActUserName() string
  134. GetActEmail() string
  135. GetRepoUserName() string
  136. GetRepoName() string
  137. GetBranch() string
  138. GetContent() string
  139. }
  140. // ActionIcon accepts a int that represents action operation type
  141. // and returns a icon class name.
  142. func ActionIcon(opType int) string {
  143. switch opType {
  144. case 1, 8: // Create, transfer repository.
  145. return "repo"
  146. case 5, 9: // Commit repository.
  147. return "git-commit"
  148. case 6: // Create issue.
  149. return "issue-opened"
  150. case 10: // Comment issue.
  151. return "comment"
  152. default:
  153. return "invalid type"
  154. }
  155. }
  156. type PushCommit struct {
  157. Sha1 string
  158. Message string
  159. AuthorEmail string
  160. AuthorName string
  161. }
  162. type PushCommits struct {
  163. Len int
  164. Commits []*PushCommit
  165. CompareUrl string
  166. }
  167. func ActionContent2Commits(act Actioner) *PushCommits {
  168. var push *PushCommits
  169. if err := json.Unmarshal([]byte(act.GetContent()), &push); err != nil {
  170. return nil
  171. }
  172. return push
  173. }
  174. func DiffTypeToStr(diffType int) string {
  175. diffTypes := map[int]string{
  176. 1: "add", 2: "modify", 3: "del",
  177. }
  178. return diffTypes[diffType]
  179. }
  180. func DiffLineTypeToStr(diffType int) string {
  181. switch diffType {
  182. case 2:
  183. return "add"
  184. case 3:
  185. return "del"
  186. case 4:
  187. return "tag"
  188. }
  189. return "same"
  190. }
  191. func Oauth2Icon(t int) string {
  192. switch t {
  193. case 1:
  194. return "fa-github-square"
  195. case 2:
  196. return "fa-google-plus-square"
  197. case 3:
  198. return "fa-twitter-square"
  199. case 4:
  200. return "fa-qq"
  201. case 5:
  202. return "fa-weibo"
  203. }
  204. return ""
  205. }
  206. func Oauth2Name(t int) string {
  207. switch t {
  208. case 1:
  209. return "GitHub"
  210. case 2:
  211. return "Google+"
  212. case 3:
  213. return "Twitter"
  214. case 4:
  215. return "腾讯 QQ"
  216. case 5:
  217. return "Weibo"
  218. }
  219. return ""
  220. }