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 5.2 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
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. "fmt"
  9. "html/template"
  10. "runtime"
  11. "strings"
  12. "time"
  13. "golang.org/x/net/html/charset"
  14. "golang.org/x/text/transform"
  15. "github.com/gogits/gogs/modules/setting"
  16. "github.com/gogits/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 DetectEncoding(content []byte) (string, error) {
  43. detector := chardet.NewTextDetector()
  44. result, err := detector.DetectBest(content)
  45. return result.Charset, err
  46. }
  47. func ToUtf8WithErr(content []byte) (error, string) {
  48. charsetLabel, err := DetectEncoding(content)
  49. if err != nil {
  50. return err, ""
  51. }
  52. if charsetLabel == "utf8" {
  53. return nil, string(content)
  54. }
  55. encoding, _ := charset.Lookup(charsetLabel)
  56. if encoding == nil {
  57. return fmt.Errorf("unknow char decoder %s", charsetLabel), string(content)
  58. }
  59. result, n, err := transform.String(encoding.NewDecoder(), string(content))
  60. // If there is an error, we concatenate the nicely decoded part and the
  61. // original left over. This way we won't loose data.
  62. if err != nil {
  63. result = result + string(content[n:])
  64. }
  65. return err, result
  66. }
  67. func ToUtf8(content string) string {
  68. _, res := ToUtf8WithErr([]byte(content))
  69. return res
  70. }
  71. var mailDomains = map[string]string{
  72. "gmail.com": "gmail.com",
  73. }
  74. var TemplateFuncs template.FuncMap = map[string]interface{}{
  75. "GoVer": func() string {
  76. return strings.Title(runtime.Version())
  77. },
  78. "AppName": func() string {
  79. return setting.AppName
  80. },
  81. "AppSubUrl": func() string {
  82. return setting.AppSubUrl
  83. },
  84. "AppVer": func() string {
  85. return setting.AppVer
  86. },
  87. "AppDomain": func() string {
  88. return setting.Domain
  89. },
  90. "CdnMode": func() bool {
  91. return setting.ProdMode && !setting.OfflineMode
  92. },
  93. "LoadTimes": func(startTime time.Time) string {
  94. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  95. },
  96. "AvatarLink": AvatarLink,
  97. "str2html": Str2html, // TODO: Legacy
  98. "Str2html": Str2html,
  99. "TimeSince": TimeSince,
  100. "FileSize": FileSize,
  101. "Subtract": Subtract,
  102. "Add": func(a, b int) int {
  103. return a + b
  104. },
  105. "ActionIcon": ActionIcon,
  106. "DateFormat": DateFormat,
  107. "List": List,
  108. "Mail2Domain": func(mail string) string {
  109. if !strings.Contains(mail, "@") {
  110. return "try.gogs.io"
  111. }
  112. suffix := strings.SplitN(mail, "@", 2)[1]
  113. domain, ok := mailDomains[suffix]
  114. if !ok {
  115. return "mail." + suffix
  116. }
  117. return domain
  118. },
  119. "SubStr": func(str string, start, length int) string {
  120. if len(str) == 0 {
  121. return ""
  122. }
  123. end := start + length
  124. if length == -1 {
  125. end = len(str)
  126. }
  127. if len(str) < end {
  128. return str
  129. }
  130. return str[start:end]
  131. },
  132. "DiffTypeToStr": DiffTypeToStr,
  133. "DiffLineTypeToStr": DiffLineTypeToStr,
  134. "ShortSha": ShortSha,
  135. "Md5": EncodeMd5,
  136. "ActionContent2Commits": ActionContent2Commits,
  137. "Oauth2Icon": Oauth2Icon,
  138. "Oauth2Name": Oauth2Name,
  139. "ToUtf8": ToUtf8,
  140. "EscapePound": func(str string) string {
  141. return strings.Replace(str, "#", "%23", -1)
  142. },
  143. }
  144. type Actioner interface {
  145. GetOpType() int
  146. GetActUserName() string
  147. GetActEmail() string
  148. GetRepoUserName() string
  149. GetRepoName() string
  150. GetBranch() string
  151. GetContent() string
  152. }
  153. // ActionIcon accepts a int that represents action operation type
  154. // and returns a icon class name.
  155. func ActionIcon(opType int) string {
  156. switch opType {
  157. case 1, 8: // Create, transfer repository.
  158. return "repo"
  159. case 5, 9: // Commit repository.
  160. return "git-commit"
  161. case 6: // Create issue.
  162. return "issue-opened"
  163. case 10: // Comment issue.
  164. return "comment"
  165. default:
  166. return "invalid type"
  167. }
  168. }
  169. type PushCommit struct {
  170. Sha1 string
  171. Message string
  172. AuthorEmail string
  173. AuthorName string
  174. }
  175. type PushCommits struct {
  176. Len int
  177. Commits []*PushCommit
  178. CompareUrl string
  179. }
  180. func ActionContent2Commits(act Actioner) *PushCommits {
  181. var push *PushCommits
  182. if err := json.Unmarshal([]byte(act.GetContent()), &push); err != nil {
  183. return nil
  184. }
  185. return push
  186. }
  187. func DiffTypeToStr(diffType int) string {
  188. diffTypes := map[int]string{
  189. 1: "add", 2: "modify", 3: "del",
  190. }
  191. return diffTypes[diffType]
  192. }
  193. func DiffLineTypeToStr(diffType int) string {
  194. switch diffType {
  195. case 2:
  196. return "add"
  197. case 3:
  198. return "del"
  199. case 4:
  200. return "tag"
  201. }
  202. return "same"
  203. }
  204. func Oauth2Icon(t int) string {
  205. switch t {
  206. case 1:
  207. return "fa-github-square"
  208. case 2:
  209. return "fa-google-plus-square"
  210. case 3:
  211. return "fa-twitter-square"
  212. case 4:
  213. return "fa-qq"
  214. case 5:
  215. return "fa-weibo"
  216. }
  217. return ""
  218. }
  219. func Oauth2Name(t int) string {
  220. switch t {
  221. case 1:
  222. return "GitHub"
  223. case 2:
  224. return "Google+"
  225. case 3:
  226. return "Twitter"
  227. case 4:
  228. return "腾讯 QQ"
  229. case 5:
  230. return "Weibo"
  231. }
  232. return ""
  233. }