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

helper.go 8.0KB

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