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 6.0 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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("unknow 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 mailDomains = map[string]string{
  85. "gmail.com": "gmail.com",
  86. }
  87. var TemplateFuncs template.FuncMap = map[string]interface{}{
  88. "GoVer": func() string {
  89. return strings.Title(runtime.Version())
  90. },
  91. "AppName": func() string {
  92. return setting.AppName
  93. },
  94. "AppSubUrl": func() string {
  95. return setting.AppSubUrl
  96. },
  97. "AppVer": func() string {
  98. return setting.AppVer
  99. },
  100. "AppDomain": func() string {
  101. return setting.Domain
  102. },
  103. "CdnMode": func() bool {
  104. return setting.ProdMode && !setting.OfflineMode
  105. },
  106. "DisableGravatar": func() bool {
  107. return setting.DisableGravatar
  108. },
  109. "LoadTimes": func(startTime time.Time) string {
  110. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  111. },
  112. "AvatarLink": AvatarLink,
  113. "Safe": Safe,
  114. "Str2html": Str2html,
  115. "TimeSince": TimeSince,
  116. "FileSize": FileSize,
  117. "Subtract": Subtract,
  118. "Add": func(a, b int) int {
  119. return a + b
  120. },
  121. "ActionIcon": ActionIcon,
  122. "DateFmtLong": func(t time.Time) string {
  123. return t.Format(time.RFC1123Z)
  124. },
  125. "DateFmtShort": func(t time.Time) string {
  126. return t.Format("Jan 02, 2006")
  127. },
  128. "List": List,
  129. "Mail2Domain": func(mail string) string {
  130. if !strings.Contains(mail, "@") {
  131. return "try.gogs.io"
  132. }
  133. suffix := strings.SplitN(mail, "@", 2)[1]
  134. domain, ok := mailDomains[suffix]
  135. if !ok {
  136. return "mail." + suffix
  137. }
  138. return domain
  139. },
  140. "SubStr": func(str string, start, length int) string {
  141. if len(str) == 0 {
  142. return ""
  143. }
  144. end := start + length
  145. if length == -1 {
  146. end = len(str)
  147. }
  148. if len(str) < end {
  149. return str
  150. }
  151. return str[start:end]
  152. },
  153. "DiffTypeToStr": DiffTypeToStr,
  154. "DiffLineTypeToStr": DiffLineTypeToStr,
  155. "Sha1": Sha1,
  156. "ShortSha": ShortSha,
  157. "Md5": EncodeMd5,
  158. "ActionContent2Commits": ActionContent2Commits,
  159. "Oauth2Icon": Oauth2Icon,
  160. "Oauth2Name": Oauth2Name,
  161. "ToUtf8": ToUtf8,
  162. "EscapePound": func(str string) string {
  163. return strings.Replace(strings.Replace(str, "%", "%25", -1), "#", "%23", -1)
  164. },
  165. "RenderCommitMessage": RenderCommitMessage,
  166. }
  167. type Actioner interface {
  168. GetOpType() int
  169. GetActUserName() string
  170. GetActEmail() string
  171. GetRepoUserName() string
  172. GetRepoName() string
  173. GetBranch() string
  174. GetContent() string
  175. }
  176. // ActionIcon accepts a int that represents action operation type
  177. // and returns a icon class name.
  178. func ActionIcon(opType int) string {
  179. switch opType {
  180. case 1, 8: // Create, transfer repository.
  181. return "repo"
  182. case 5, 9: // Commit repository.
  183. return "git-commit"
  184. case 6: // Create issue.
  185. return "issue-opened"
  186. case 10: // Comment issue.
  187. return "comment"
  188. default:
  189. return "invalid type"
  190. }
  191. }
  192. type PushCommit struct {
  193. Sha1 string
  194. Message string
  195. AuthorEmail string
  196. AuthorName string
  197. }
  198. type PushCommits struct {
  199. Len int
  200. Commits []*PushCommit
  201. CompareUrl string
  202. }
  203. func ActionContent2Commits(act Actioner) *PushCommits {
  204. var push *PushCommits
  205. if err := json.Unmarshal([]byte(act.GetContent()), &push); err != nil {
  206. return nil
  207. }
  208. return push
  209. }
  210. func DiffTypeToStr(diffType int) string {
  211. diffTypes := map[int]string{
  212. 1: "add", 2: "modify", 3: "del",
  213. }
  214. return diffTypes[diffType]
  215. }
  216. func DiffLineTypeToStr(diffType int) string {
  217. switch diffType {
  218. case 2:
  219. return "add"
  220. case 3:
  221. return "del"
  222. case 4:
  223. return "tag"
  224. }
  225. return "same"
  226. }
  227. func Oauth2Icon(t int) string {
  228. switch t {
  229. case 1:
  230. return "fa-github-square"
  231. case 2:
  232. return "fa-google-plus-square"
  233. case 3:
  234. return "fa-twitter-square"
  235. case 4:
  236. return "fa-qq"
  237. case 5:
  238. return "fa-weibo"
  239. }
  240. return ""
  241. }
  242. func Oauth2Name(t int) string {
  243. switch t {
  244. case 1:
  245. return "GitHub"
  246. case 2:
  247. return "Google+"
  248. case 3:
  249. return "Twitter"
  250. case 4:
  251. return "腾讯 QQ"
  252. case 5:
  253. return "Weibo"
  254. }
  255. return ""
  256. }