template.go 5.9 KB

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