template.go 5.2 KB

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