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.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. "EscapePound": func(str string) string {
  131. return strings.Replace(str, "#", "%23", -1)
  132. },
  133. }
  134. type Actioner interface {
  135. GetOpType() int
  136. GetActUserName() string
  137. GetActEmail() string
  138. GetRepoUserName() string
  139. GetRepoName() string
  140. GetBranch() string
  141. GetContent() string
  142. }
  143. // ActionIcon accepts a int that represents action operation type
  144. // and returns a icon class name.
  145. func ActionIcon(opType int) string {
  146. switch opType {
  147. case 1, 8: // Create, transfer repository.
  148. return "repo"
  149. case 5, 9: // Commit repository.
  150. return "git-commit"
  151. case 6: // Create issue.
  152. return "issue-opened"
  153. case 10: // Comment issue.
  154. return "comment"
  155. default:
  156. return "invalid type"
  157. }
  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. CompareUrl string
  169. }
  170. func ActionContent2Commits(act Actioner) *PushCommits {
  171. var push *PushCommits
  172. if err := json.Unmarshal([]byte(act.GetContent()), &push); err != nil {
  173. return nil
  174. }
  175. return push
  176. }
  177. func DiffTypeToStr(diffType int) string {
  178. diffTypes := map[int]string{
  179. 1: "add", 2: "modify", 3: "del",
  180. }
  181. return diffTypes[diffType]
  182. }
  183. func DiffLineTypeToStr(diffType int) string {
  184. switch diffType {
  185. case 2:
  186. return "add"
  187. case 3:
  188. return "del"
  189. case 4:
  190. return "tag"
  191. }
  192. return "same"
  193. }
  194. func Oauth2Icon(t int) string {
  195. switch t {
  196. case 1:
  197. return "fa-github-square"
  198. case 2:
  199. return "fa-google-plus-square"
  200. case 3:
  201. return "fa-twitter-square"
  202. case 4:
  203. return "fa-qq"
  204. case 5:
  205. return "fa-weibo"
  206. }
  207. return ""
  208. }
  209. func Oauth2Name(t int) string {
  210. switch t {
  211. case 1:
  212. return "GitHub"
  213. case 2:
  214. return "Google+"
  215. case 3:
  216. return "Twitter"
  217. case 4:
  218. return "腾讯 QQ"
  219. case 5:
  220. return "Weibo"
  221. }
  222. return ""
  223. }