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.

280 lines
5.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
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. "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/chardet"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. func Safe(raw string) template.HTML {
  19. return template.HTML(raw)
  20. }
  21. func Str2html(raw string) template.HTML {
  22. return template.HTML(Sanitizer.Sanitize(raw))
  23. }
  24. func Range(l int) []int {
  25. return make([]int, l)
  26. }
  27. func List(l *list.List) chan interface{} {
  28. e := l.Front()
  29. c := make(chan interface{})
  30. go func() {
  31. for e != nil {
  32. c <- e.Value
  33. e = e.Next()
  34. }
  35. close(c)
  36. }()
  37. return c
  38. }
  39. func Sha1(str string) string {
  40. return EncodeSha1(str)
  41. }
  42. func ShortSha(sha1 string) string {
  43. if len(sha1) == 40 {
  44. return sha1[:10]
  45. }
  46. return sha1
  47. }
  48. func DetectEncoding(content []byte) (string, error) {
  49. detector := chardet.NewTextDetector()
  50. result, err := detector.DetectBest(content)
  51. if result.Charset != "UTF-8" && len(setting.AnsiCharset) > 0 {
  52. return setting.AnsiCharset, err
  53. }
  54. return result.Charset, err
  55. }
  56. func ToUtf8WithErr(content []byte) (error, string) {
  57. charsetLabel, err := DetectEncoding(content)
  58. if err != nil {
  59. return err, ""
  60. }
  61. if charsetLabel == "UTF-8" {
  62. return nil, string(content)
  63. }
  64. encoding, _ := charset.Lookup(charsetLabel)
  65. if encoding == nil {
  66. return fmt.Errorf("unknown char decoder %s", charsetLabel), string(content)
  67. }
  68. result, n, err := transform.String(encoding.NewDecoder(), string(content))
  69. // If there is an error, we concatenate the nicely decoded part and the
  70. // original left over. This way we won't loose data.
  71. if err != nil {
  72. result = result + string(content[n:])
  73. }
  74. return err, result
  75. }
  76. func ToUtf8(content string) string {
  77. _, res := ToUtf8WithErr([]byte(content))
  78. return res
  79. }
  80. // RenderCommitMessage renders commit message with XSS-safe and special links.
  81. func RenderCommitMessage(msg, urlPrefix string) template.HTML {
  82. return template.HTML(string(RenderIssueIndexPattern([]byte(template.HTMLEscapeString(msg)), urlPrefix)))
  83. }
  84. var TemplateFuncs template.FuncMap = map[string]interface{}{
  85. "GoVer": func() string {
  86. return strings.Title(runtime.Version())
  87. },
  88. "AppName": func() string {
  89. return setting.AppName
  90. },
  91. "AppSubUrl": func() string {
  92. return setting.AppSubUrl
  93. },
  94. "AppVer": func() string {
  95. return setting.AppVer
  96. },
  97. "AppDomain": func() string {
  98. return setting.Domain
  99. },
  100. "DisableGravatar": func() bool {
  101. return setting.DisableGravatar
  102. },
  103. "LoadTimes": func(startTime time.Time) string {
  104. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  105. },
  106. "AvatarLink": AvatarLink,
  107. "Safe": Safe,
  108. "Str2html": Str2html,
  109. "TimeSince": TimeSince,
  110. "RawTimeSince": RawTimeSince,
  111. "FileSize": FileSize,
  112. "Subtract": Subtract,
  113. "Add": func(a, b int) int {
  114. return a + b
  115. },
  116. "ActionIcon": ActionIcon,
  117. "DateFmtLong": func(t time.Time) string {
  118. return t.Format(time.RFC1123Z)
  119. },
  120. "DateFmtShort": func(t time.Time) string {
  121. return t.Format("Jan 02, 2006")
  122. },
  123. "List": List,
  124. "Mail2Domain": func(mail string) string {
  125. if !strings.Contains(mail, "@") {
  126. return "try.gogs.io"
  127. }
  128. return strings.SplitN(mail, "@", 2)[1]
  129. },
  130. "SubStr": func(str string, start, length int) string {
  131. if len(str) == 0 {
  132. return ""
  133. }
  134. end := start + length
  135. if length == -1 {
  136. end = len(str)
  137. }
  138. if len(str) < end {
  139. return str
  140. }
  141. return str[start:end]
  142. },
  143. "DiffTypeToStr": DiffTypeToStr,
  144. "DiffLineTypeToStr": DiffLineTypeToStr,
  145. "Sha1": Sha1,
  146. "ShortSha": ShortSha,
  147. "Md5": EncodeMd5,
  148. "ActionContent2Commits": ActionContent2Commits,
  149. "Oauth2Icon": Oauth2Icon,
  150. "Oauth2Name": Oauth2Name,
  151. "ToUtf8": ToUtf8,
  152. "EscapePound": func(str string) string {
  153. return strings.Replace(strings.Replace(str, "%", "%25", -1), "#", "%23", -1)
  154. },
  155. "RenderCommitMessage": RenderCommitMessage,
  156. }
  157. type Actioner interface {
  158. GetOpType() int
  159. GetActUserName() string
  160. GetActEmail() string
  161. GetRepoUserName() string
  162. GetRepoName() string
  163. GetRepoPath() string
  164. GetRepoLink() string
  165. GetBranch() string
  166. GetContent() string
  167. GetCreate() time.Time
  168. GetIssueInfos() []string
  169. }
  170. // ActionIcon accepts a int that represents action operation type
  171. // and returns a icon class name.
  172. func ActionIcon(opType int) string {
  173. switch opType {
  174. case 1, 8: // Create, transfer repository.
  175. return "repo"
  176. case 5, 9: // Commit repository.
  177. return "git-commit"
  178. case 6: // Create issue.
  179. return "issue-opened"
  180. case 10: // Comment issue.
  181. return "comment"
  182. default:
  183. return "invalid type"
  184. }
  185. }
  186. type PushCommit struct {
  187. Sha1 string
  188. Message string
  189. AuthorEmail string
  190. AuthorName string
  191. }
  192. type PushCommits struct {
  193. Len int
  194. Commits []*PushCommit
  195. CompareUrl string
  196. }
  197. func ActionContent2Commits(act Actioner) *PushCommits {
  198. var push *PushCommits
  199. if err := json.Unmarshal([]byte(act.GetContent()), &push); err != nil {
  200. return nil
  201. }
  202. return push
  203. }
  204. func DiffTypeToStr(diffType int) string {
  205. diffTypes := map[int]string{
  206. 1: "add", 2: "modify", 3: "del",
  207. }
  208. return diffTypes[diffType]
  209. }
  210. func DiffLineTypeToStr(diffType int) string {
  211. switch diffType {
  212. case 2:
  213. return "add"
  214. case 3:
  215. return "del"
  216. case 4:
  217. return "tag"
  218. }
  219. return "same"
  220. }
  221. func Oauth2Icon(t int) string {
  222. switch t {
  223. case 1:
  224. return "fa-github-square"
  225. case 2:
  226. return "fa-google-plus-square"
  227. case 3:
  228. return "fa-twitter-square"
  229. case 4:
  230. return "fa-qq"
  231. case 5:
  232. return "fa-weibo"
  233. }
  234. return ""
  235. }
  236. func Oauth2Name(t int) string {
  237. switch t {
  238. case 1:
  239. return "GitHub"
  240. case 2:
  241. return "Google+"
  242. case 3:
  243. return "Twitter"
  244. case 4:
  245. return "腾讯 QQ"
  246. case 5:
  247. return "Weibo"
  248. }
  249. return ""
  250. }