template.go 5.6 KB

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