template.go 5.8 KB

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