You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

helper.go 8.3 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 templates
  5. import (
  6. "container/list"
  7. "encoding/json"
  8. "fmt"
  9. "html/template"
  10. "mime"
  11. "path/filepath"
  12. "runtime"
  13. "strings"
  14. "time"
  15. "github.com/microcosm-cc/bluemonday"
  16. "golang.org/x/net/html/charset"
  17. "golang.org/x/text/transform"
  18. "gopkg.in/editorconfig/editorconfig-core-go.v1"
  19. "code.gitea.io/gitea/models"
  20. "code.gitea.io/gitea/modules/base"
  21. "code.gitea.io/gitea/modules/log"
  22. "code.gitea.io/gitea/modules/markdown"
  23. "code.gitea.io/gitea/modules/setting"
  24. )
  25. // NewFuncMap returns functions for injecting to templates
  26. func NewFuncMap() []template.FuncMap {
  27. return []template.FuncMap{map[string]interface{}{
  28. "GoVer": func() string {
  29. return strings.Title(runtime.Version())
  30. },
  31. "UseHTTPS": func() bool {
  32. return strings.HasPrefix(setting.AppURL, "https")
  33. },
  34. "AppName": func() string {
  35. return setting.AppName
  36. },
  37. "AppSubUrl": func() string {
  38. return setting.AppSubURL
  39. },
  40. "AppUrl": func() string {
  41. return setting.AppURL
  42. },
  43. "AppVer": func() string {
  44. return setting.AppVer
  45. },
  46. "AppDomain": func() string {
  47. return setting.Domain
  48. },
  49. "DisableGravatar": func() bool {
  50. return setting.DisableGravatar
  51. },
  52. "ShowFooterTemplateLoadTime": func() bool {
  53. return setting.ShowFooterTemplateLoadTime
  54. },
  55. "LoadTimes": func(startTime time.Time) string {
  56. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  57. },
  58. "AvatarLink": base.AvatarLink,
  59. "Safe": Safe,
  60. "Sanitize": bluemonday.UGCPolicy().Sanitize,
  61. "Str2html": Str2html,
  62. "TimeSince": base.TimeSince,
  63. "RawTimeSince": base.RawTimeSince,
  64. "FileSize": base.FileSize,
  65. "Subtract": base.Subtract,
  66. "Add": func(a, b int) int {
  67. return a + b
  68. },
  69. "ActionIcon": ActionIcon,
  70. "DateFmtLong": func(t time.Time) string {
  71. return t.Format(time.RFC1123Z)
  72. },
  73. "DateFmtShort": func(t time.Time) string {
  74. return t.Format("Jan 02, 2006")
  75. },
  76. "List": List,
  77. "SubStr": func(str string, start, length int) string {
  78. if len(str) == 0 {
  79. return ""
  80. }
  81. end := start + length
  82. if length == -1 {
  83. end = len(str)
  84. }
  85. if len(str) < end {
  86. return str
  87. }
  88. return str[start:end]
  89. },
  90. "EllipsisString": base.EllipsisString,
  91. "DiffTypeToStr": DiffTypeToStr,
  92. "DiffLineTypeToStr": DiffLineTypeToStr,
  93. "Sha1": Sha1,
  94. "ShortSha": base.ShortSha,
  95. "MD5": base.EncodeMD5,
  96. "ActionContent2Commits": ActionContent2Commits,
  97. "EscapePound": func(str string) string {
  98. return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str)
  99. },
  100. "RenderCommitMessage": RenderCommitMessage,
  101. "ThemeColorMetaTag": func() string {
  102. return setting.UI.ThemeColorMetaTag
  103. },
  104. "FilenameIsImage": func(filename string) bool {
  105. mimeType := mime.TypeByExtension(filepath.Ext(filename))
  106. return strings.HasPrefix(mimeType, "image/")
  107. },
  108. "TabSizeClass": func(ec *editorconfig.Editorconfig, filename string) string {
  109. if ec != nil {
  110. def := ec.GetDefinitionForFilename(filename)
  111. if def.TabWidth > 0 {
  112. return fmt.Sprintf("tab-size-%d", def.TabWidth)
  113. }
  114. }
  115. return "tab-size-8"
  116. },
  117. "SubJumpablePath": func(str string) []string {
  118. var path []string
  119. index := strings.LastIndex(str, "/")
  120. if index != -1 && index != len(str) {
  121. path = append(path, str[0:index+1])
  122. path = append(path, str[index+1:])
  123. } else {
  124. path = append(path, str)
  125. }
  126. return path
  127. },
  128. }}
  129. }
  130. // Safe render raw as HTML
  131. func Safe(raw string) template.HTML {
  132. return template.HTML(raw)
  133. }
  134. // Str2html render Markdown text to HTML
  135. func Str2html(raw string) template.HTML {
  136. return template.HTML(markdown.Sanitizer.Sanitize(raw))
  137. }
  138. // List traversings the list
  139. func List(l *list.List) chan interface{} {
  140. e := l.Front()
  141. c := make(chan interface{})
  142. go func() {
  143. for e != nil {
  144. c <- e.Value
  145. e = e.Next()
  146. }
  147. close(c)
  148. }()
  149. return c
  150. }
  151. // Sha1 returns sha1 sum of string
  152. func Sha1(str string) string {
  153. return base.EncodeSha1(str)
  154. }
  155. // ToUTF8WithErr converts content to UTF8 encoding
  156. func ToUTF8WithErr(content []byte) (string, error) {
  157. charsetLabel, err := base.DetectEncoding(content)
  158. if err != nil {
  159. return "", err
  160. } else if charsetLabel == "UTF-8" {
  161. return string(content), nil
  162. }
  163. encoding, _ := charset.Lookup(charsetLabel)
  164. if encoding == nil {
  165. return string(content), fmt.Errorf("Unknown encoding: %s", charsetLabel)
  166. }
  167. // If there is an error, we concatenate the nicely decoded part and the
  168. // original left over. This way we won't loose data.
  169. result, n, err := transform.String(encoding.NewDecoder(), string(content))
  170. if err != nil {
  171. result = result + string(content[n:])
  172. }
  173. return result, err
  174. }
  175. // ToUTF8 converts content to UTF8 encoding and ignore error
  176. func ToUTF8(content string) string {
  177. res, _ := ToUTF8WithErr([]byte(content))
  178. return res
  179. }
  180. // ReplaceLeft replaces all prefixes 'old' in 's' with 'new'.
  181. func ReplaceLeft(s, old, new string) string {
  182. oldLen, newLen, i, n := len(old), len(new), 0, 0
  183. for ; i < len(s) && strings.HasPrefix(s[i:], old); n++ {
  184. i += oldLen
  185. }
  186. // simple optimization
  187. if n == 0 {
  188. return s
  189. }
  190. // allocating space for the new string
  191. curLen := n*newLen + len(s[i:])
  192. replacement := make([]byte, curLen, curLen)
  193. j := 0
  194. for ; j < n*newLen; j += newLen {
  195. copy(replacement[j:j+newLen], new)
  196. }
  197. copy(replacement[j:], s[i:])
  198. return string(replacement)
  199. }
  200. // RenderCommitMessage renders commit message with XSS-safe and special links.
  201. func RenderCommitMessage(full bool, msg, urlPrefix string, metas map[string]string) template.HTML {
  202. cleanMsg := template.HTMLEscapeString(msg)
  203. fullMessage := string(markdown.RenderIssueIndexPattern([]byte(cleanMsg), urlPrefix, metas))
  204. msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n")
  205. numLines := len(msgLines)
  206. if numLines == 0 {
  207. return template.HTML("")
  208. } else if !full {
  209. return template.HTML(msgLines[0])
  210. } else if numLines == 1 || (numLines >= 2 && len(msgLines[1]) == 0) {
  211. // First line is a header, standalone or followed by empty line
  212. header := fmt.Sprintf("<h3>%s</h3>", msgLines[0])
  213. if numLines >= 2 {
  214. fullMessage = header + fmt.Sprintf("\n<pre>%s</pre>", strings.Join(msgLines[2:], "\n"))
  215. } else {
  216. fullMessage = header
  217. }
  218. } else {
  219. // Non-standard git message, there is no header line
  220. fullMessage = fmt.Sprintf("<h4>%s</h4>", strings.Join(msgLines, "<br>"))
  221. }
  222. return template.HTML(fullMessage)
  223. }
  224. // Actioner describes an action
  225. type Actioner interface {
  226. GetOpType() int
  227. GetActUserName() string
  228. GetRepoUserName() string
  229. GetRepoName() string
  230. GetRepoPath() string
  231. GetRepoLink() string
  232. GetBranch() string
  233. GetContent() string
  234. GetCreate() time.Time
  235. GetIssueInfos() []string
  236. }
  237. // ActionIcon accepts a int that represents action operation type
  238. // and returns a icon class name.
  239. func ActionIcon(opType int) string {
  240. switch opType {
  241. case 1, 8: // Create and transfer repository
  242. return "repo"
  243. case 5, 9: // Commit repository
  244. return "git-commit"
  245. case 6: // Create issue
  246. return "issue-opened"
  247. case 7: // New pull request
  248. return "git-pull-request"
  249. case 10: // Comment issue
  250. return "comment-discussion"
  251. case 11: // Merge pull request
  252. return "git-merge"
  253. case 12, 14: // Close issue or pull request
  254. return "issue-closed"
  255. case 13, 15: // Reopen issue or pull request
  256. return "issue-reopened"
  257. default:
  258. return "invalid type"
  259. }
  260. }
  261. // ActionContent2Commits converts action content to push commits
  262. func ActionContent2Commits(act Actioner) *models.PushCommits {
  263. push := models.NewPushCommits()
  264. if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
  265. log.Error(4, "json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err)
  266. }
  267. return push
  268. }
  269. // DiffTypeToStr returns diff type name
  270. func DiffTypeToStr(diffType int) string {
  271. diffTypes := map[int]string{
  272. 1: "add", 2: "modify", 3: "del", 4: "rename",
  273. }
  274. return diffTypes[diffType]
  275. }
  276. // DiffLineTypeToStr returns diff line type name
  277. func DiffLineTypeToStr(diffType int) string {
  278. switch diffType {
  279. case 2:
  280. return "add"
  281. case 3:
  282. return "del"
  283. case 4:
  284. return "tag"
  285. }
  286. return "same"
  287. }