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.9 kB

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