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.

template.go 7.4 kB

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