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

251 lines
4.8 KiB

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