template.go 5.5 KB

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