template.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 template
  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/gogs/models"
  16. "github.com/gogits/gogs/modules/base"
  17. "github.com/gogits/gogs/modules/setting"
  18. )
  19. func Safe(raw string) template.HTML {
  20. return template.HTML(raw)
  21. }
  22. func Str2html(raw string) template.HTML {
  23. return template.HTML(base.Sanitizer.Sanitize(raw))
  24. }
  25. func Range(l int) []int {
  26. return make([]int, l)
  27. }
  28. func List(l *list.List) chan interface{} {
  29. e := l.Front()
  30. c := make(chan interface{})
  31. go func() {
  32. for e != nil {
  33. c <- e.Value
  34. e = e.Next()
  35. }
  36. close(c)
  37. }()
  38. return c
  39. }
  40. func Sha1(str string) string {
  41. return base.EncodeSha1(str)
  42. }
  43. func ToUtf8WithErr(content []byte) (error, string) {
  44. charsetLabel, err := base.DetectEncoding(content)
  45. if err != nil {
  46. return err, ""
  47. }
  48. if charsetLabel == "UTF-8" {
  49. return nil, string(content)
  50. }
  51. encoding, _ := charset.Lookup(charsetLabel)
  52. if encoding == nil {
  53. return fmt.Errorf("unknown char decoder %s", charsetLabel), string(content)
  54. }
  55. result, n, err := transform.String(encoding.NewDecoder(), string(content))
  56. // If there is an error, we concatenate the nicely decoded part and the
  57. // original left over. This way we won't loose data.
  58. if err != nil {
  59. result = result + string(content[n:])
  60. }
  61. return err, result
  62. }
  63. func ToUtf8(content string) string {
  64. _, res := ToUtf8WithErr([]byte(content))
  65. return res
  66. }
  67. // Replaces all prefixes 'old' in 's' with 'new'.
  68. func ReplaceLeft(s, old, new string) string {
  69. old_len, new_len, i, n := len(old), len(new), 0, 0
  70. for ; i < len(s) && strings.HasPrefix(s[i:], old); n += 1 {
  71. i += old_len
  72. }
  73. // simple optimization
  74. if n == 0 {
  75. return s
  76. }
  77. // allocating space for the new string
  78. newLen := n*new_len + len(s[i:])
  79. replacement := make([]byte, newLen, newLen)
  80. j := 0
  81. for ; j < n*new_len; j += new_len {
  82. copy(replacement[j:j+new_len], new)
  83. }
  84. copy(replacement[j:], s[i:])
  85. return string(replacement)
  86. }
  87. // RenderCommitMessage renders commit message with XSS-safe and special links.
  88. func RenderCommitMessage(msg, urlPrefix string) template.HTML {
  89. cleanMsg := template.HTMLEscapeString(msg)
  90. fullMessage := string(base.RenderIssueIndexPattern([]byte(cleanMsg), urlPrefix))
  91. msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n")
  92. for i := range msgLines {
  93. msgLines[i] = ReplaceLeft(msgLines[i], " ", "&nbsp;")
  94. }
  95. fullMessage = strings.Join(msgLines, "<br>")
  96. return template.HTML(fullMessage)
  97. }
  98. var Funcs template.FuncMap = map[string]interface{}{
  99. "GoVer": func() string {
  100. return strings.Title(runtime.Version())
  101. },
  102. "AppName": func() string {
  103. return setting.AppName
  104. },
  105. "AppSubUrl": func() string {
  106. return setting.AppSubUrl
  107. },
  108. "AppVer": func() string {
  109. return setting.AppVer
  110. },
  111. "AppDomain": func() string {
  112. return setting.Domain
  113. },
  114. "DisableGravatar": func() bool {
  115. return setting.DisableGravatar
  116. },
  117. "LoadTimes": func(startTime time.Time) string {
  118. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  119. },
  120. "AvatarLink": base.AvatarLink,
  121. "Safe": Safe,
  122. "Str2html": Str2html,
  123. "TimeSince": base.TimeSince,
  124. "RawTimeSince": base.RawTimeSince,
  125. "FileSize": base.FileSize,
  126. "Subtract": base.Subtract,
  127. "Add": func(a, b int) int {
  128. return a + b
  129. },
  130. "ActionIcon": ActionIcon,
  131. "DateFmtLong": func(t time.Time) string {
  132. return t.Format(time.RFC1123Z)
  133. },
  134. "DateFmtShort": func(t time.Time) string {
  135. return t.Format("Jan 02, 2006")
  136. },
  137. "List": List,
  138. "Mail2Domain": func(mail string) string {
  139. if !strings.Contains(mail, "@") {
  140. return "try.gogs.io"
  141. }
  142. return strings.SplitN(mail, "@", 2)[1]
  143. },
  144. "SubStr": func(str string, start, length int) string {
  145. if len(str) == 0 {
  146. return ""
  147. }
  148. end := start + length
  149. if length == -1 {
  150. end = len(str)
  151. }
  152. if len(str) < end {
  153. return str
  154. }
  155. return str[start:end]
  156. },
  157. "DiffTypeToStr": DiffTypeToStr,
  158. "DiffLineTypeToStr": DiffLineTypeToStr,
  159. "Sha1": Sha1,
  160. "ShortSha": base.ShortSha,
  161. "Md5": base.EncodeMd5,
  162. "ActionContent2Commits": ActionContent2Commits,
  163. "Oauth2Icon": Oauth2Icon,
  164. "Oauth2Name": Oauth2Name,
  165. "ToUtf8": ToUtf8,
  166. "EscapePound": func(str string) string {
  167. return strings.Replace(strings.Replace(str, "%", "%25", -1), "#", "%23", -1)
  168. },
  169. "RenderCommitMessage": RenderCommitMessage,
  170. }
  171. type Actioner interface {
  172. GetOpType() int
  173. GetActUserName() string
  174. GetActEmail() string
  175. GetRepoUserName() string
  176. GetRepoName() string
  177. GetRepoPath() string
  178. GetRepoLink() string
  179. GetBranch() string
  180. GetContent() string
  181. GetCreate() time.Time
  182. GetIssueInfos() []string
  183. }
  184. // ActionIcon accepts a int that represents action operation type
  185. // and returns a icon class name.
  186. func ActionIcon(opType int) string {
  187. switch opType {
  188. case 1, 8: // Create, transfer repository.
  189. return "repo"
  190. case 5, 9: // Commit repository.
  191. return "git-commit"
  192. case 6: // Create issue.
  193. return "issue-opened"
  194. case 10: // Comment issue.
  195. return "comment"
  196. default:
  197. return "invalid type"
  198. }
  199. }
  200. func ActionContent2Commits(act Actioner) *models.PushCommits {
  201. push := models.NewPushCommits()
  202. if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
  203. return nil
  204. }
  205. return push
  206. }
  207. func DiffTypeToStr(diffType int) string {
  208. diffTypes := map[int]string{
  209. 1: "add", 2: "modify", 3: "del", 4: "rename",
  210. }
  211. return diffTypes[diffType]
  212. }
  213. func DiffLineTypeToStr(diffType int) string {
  214. switch diffType {
  215. case 2:
  216. return "add"
  217. case 3:
  218. return "del"
  219. case 4:
  220. return "tag"
  221. }
  222. return "same"
  223. }
  224. func Oauth2Icon(t int) string {
  225. switch t {
  226. case 1:
  227. return "fa-github-square"
  228. case 2:
  229. return "fa-google-plus-square"
  230. case 3:
  231. return "fa-twitter-square"
  232. case 4:
  233. return "fa-qq"
  234. case 5:
  235. return "fa-weibo"
  236. }
  237. return ""
  238. }
  239. func Oauth2Name(t int) string {
  240. switch t {
  241. case 1:
  242. return "GitHub"
  243. case 2:
  244. return "Google+"
  245. case 3:
  246. return "Twitter"
  247. case 4:
  248. return "腾讯 QQ"
  249. case 5:
  250. return "Weibo"
  251. }
  252. return ""
  253. }