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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Copyright 2014 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package templates
  6. import (
  7. "bytes"
  8. "container/list"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "html"
  13. "html/template"
  14. "mime"
  15. "net/url"
  16. "path/filepath"
  17. "regexp"
  18. "runtime"
  19. "strings"
  20. texttmpl "text/template"
  21. "time"
  22. "unicode"
  23. "code.gitea.io/gitea/models"
  24. "code.gitea.io/gitea/modules/base"
  25. "code.gitea.io/gitea/modules/emoji"
  26. "code.gitea.io/gitea/modules/log"
  27. "code.gitea.io/gitea/modules/markup"
  28. "code.gitea.io/gitea/modules/repository"
  29. "code.gitea.io/gitea/modules/setting"
  30. "code.gitea.io/gitea/modules/svg"
  31. "code.gitea.io/gitea/modules/timeutil"
  32. "code.gitea.io/gitea/modules/util"
  33. "code.gitea.io/gitea/services/gitdiff"
  34. mirror_service "code.gitea.io/gitea/services/mirror"
  35. "github.com/editorconfig/editorconfig-core-go/v2"
  36. )
  37. // Used from static.go && dynamic.go
  38. var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}[\s]*$`)
  39. // NewFuncMap returns functions for injecting to templates
  40. func NewFuncMap() []template.FuncMap {
  41. return []template.FuncMap{map[string]interface{}{
  42. "GoVer": func() string {
  43. return strings.Title(runtime.Version())
  44. },
  45. "UseHTTPS": func() bool {
  46. return strings.HasPrefix(setting.AppURL, "https")
  47. },
  48. "AppName": func() string {
  49. return setting.AppName
  50. },
  51. "AppSubUrl": func() string {
  52. return setting.AppSubURL
  53. },
  54. "StaticUrlPrefix": func() string {
  55. return setting.StaticURLPrefix
  56. },
  57. "AppUrl": func() string {
  58. return setting.AppURL
  59. },
  60. "AppVer": func() string {
  61. return setting.AppVer
  62. },
  63. "AppBuiltWith": func() string {
  64. return setting.AppBuiltWith
  65. },
  66. "AppDomain": func() string {
  67. return setting.Domain
  68. },
  69. "DisableGravatar": func() bool {
  70. return setting.DisableGravatar
  71. },
  72. "DefaultShowFullName": func() bool {
  73. return setting.UI.DefaultShowFullName
  74. },
  75. "ShowFooterTemplateLoadTime": func() bool {
  76. return setting.ShowFooterTemplateLoadTime
  77. },
  78. "LoadTimes": func(startTime time.Time) string {
  79. return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
  80. },
  81. "AllowedReactions": func() []string {
  82. return setting.UI.Reactions
  83. },
  84. "AvatarLink": models.AvatarLink,
  85. "Safe": Safe,
  86. "SafeJS": SafeJS,
  87. "Str2html": Str2html,
  88. "TimeSince": timeutil.TimeSince,
  89. "TimeSinceUnix": timeutil.TimeSinceUnix,
  90. "RawTimeSince": timeutil.RawTimeSince,
  91. "FileSize": base.FileSize,
  92. "PrettyNumber": base.PrettyNumber,
  93. "Subtract": base.Subtract,
  94. "EntryIcon": base.EntryIcon,
  95. "MigrationIcon": MigrationIcon,
  96. "Add": func(a ...int) int {
  97. sum := 0
  98. for _, val := range a {
  99. sum += val
  100. }
  101. return sum
  102. },
  103. "Mul": func(a ...int) int {
  104. sum := 1
  105. for _, val := range a {
  106. sum *= val
  107. }
  108. return sum
  109. },
  110. "ActionIcon": ActionIcon,
  111. "DateFmtLong": func(t time.Time) string {
  112. return t.Format(time.RFC1123Z)
  113. },
  114. "DateFmtShort": func(t time.Time) string {
  115. return t.Format("Jan 02, 2006")
  116. },
  117. "SizeFmt": base.FileSize,
  118. "CountFmt": base.FormatNumberSI,
  119. "List": List,
  120. "SubStr": func(str string, start, length int) string {
  121. if len(str) == 0 {
  122. return ""
  123. }
  124. end := start + length
  125. if length == -1 {
  126. end = len(str)
  127. }
  128. if len(str) < end {
  129. return str
  130. }
  131. return str[start:end]
  132. },
  133. "EllipsisString": base.EllipsisString,
  134. "DiffTypeToStr": DiffTypeToStr,
  135. "DiffLineTypeToStr": DiffLineTypeToStr,
  136. "Sha1": Sha1,
  137. "ShortSha": base.ShortSha,
  138. "MD5": base.EncodeMD5,
  139. "ActionContent2Commits": ActionContent2Commits,
  140. "PathEscape": url.PathEscape,
  141. "EscapePound": func(str string) string {
  142. return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str)
  143. },
  144. "PathEscapeSegments": util.PathEscapeSegments,
  145. "URLJoin": util.URLJoin,
  146. "RenderCommitMessage": RenderCommitMessage,
  147. "RenderCommitMessageLink": RenderCommitMessageLink,
  148. "RenderCommitMessageLinkSubject": RenderCommitMessageLinkSubject,
  149. "RenderCommitBody": RenderCommitBody,
  150. "RenderEmoji": RenderEmoji,
  151. "RenderEmojiPlain": emoji.ReplaceAliases,
  152. "ReactionToEmoji": ReactionToEmoji,
  153. "RenderNote": RenderNote,
  154. "IsMultilineCommitMessage": IsMultilineCommitMessage,
  155. "ThemeColorMetaTag": func() string {
  156. return setting.UI.ThemeColorMetaTag
  157. },
  158. "MetaAuthor": func() string {
  159. return setting.UI.Meta.Author
  160. },
  161. "MetaDescription": func() string {
  162. return setting.UI.Meta.Description
  163. },
  164. "MetaKeywords": func() string {
  165. return setting.UI.Meta.Keywords
  166. },
  167. "UseServiceWorker": func() bool {
  168. return setting.UI.UseServiceWorker
  169. },
  170. "FilenameIsImage": func(filename string) bool {
  171. mimeType := mime.TypeByExtension(filepath.Ext(filename))
  172. return strings.HasPrefix(mimeType, "image/")
  173. },
  174. "TabSizeClass": func(ec interface{}, filename string) string {
  175. var (
  176. value *editorconfig.Editorconfig
  177. ok bool
  178. )
  179. if ec != nil {
  180. if value, ok = ec.(*editorconfig.Editorconfig); !ok || value == nil {
  181. return "tab-size-8"
  182. }
  183. def, err := value.GetDefinitionForFilename(filename)
  184. if err != nil {
  185. log.Error("tab size class: getting definition for filename: %v", err)
  186. return "tab-size-8"
  187. }
  188. if def.TabWidth > 0 {
  189. return fmt.Sprintf("tab-size-%d", def.TabWidth)
  190. }
  191. }
  192. return "tab-size-8"
  193. },
  194. "SubJumpablePath": func(str string) []string {
  195. var path []string
  196. index := strings.LastIndex(str, "/")
  197. if index != -1 && index != len(str) {
  198. path = append(path, str[0:index+1], str[index+1:])
  199. } else {
  200. path = append(path, str)
  201. }
  202. return path
  203. },
  204. "Json": func(in interface{}) string {
  205. out, err := json.Marshal(in)
  206. if err != nil {
  207. return ""
  208. }
  209. return string(out)
  210. },
  211. "JsonPrettyPrint": func(in string) string {
  212. var out bytes.Buffer
  213. err := json.Indent(&out, []byte(in), "", " ")
  214. if err != nil {
  215. return ""
  216. }
  217. return out.String()
  218. },
  219. "DisableGitHooks": func() bool {
  220. return setting.DisableGitHooks
  221. },
  222. "DisableImportLocal": func() bool {
  223. return !setting.ImportLocalPaths
  224. },
  225. "TrN": TrN,
  226. "Dict": func(values ...interface{}) (map[string]interface{}, error) {
  227. if len(values)%2 != 0 {
  228. return nil, errors.New("invalid dict call")
  229. }
  230. dict := make(map[string]interface{}, len(values)/2)
  231. for i := 0; i < len(values); i += 2 {
  232. key, ok := values[i].(string)
  233. if !ok {
  234. return nil, errors.New("dict keys must be strings")
  235. }
  236. dict[key] = values[i+1]
  237. }
  238. return dict, nil
  239. },
  240. "Printf": fmt.Sprintf,
  241. "Escape": Escape,
  242. "Sec2Time": models.SecToTime,
  243. "ParseDeadline": func(deadline string) []string {
  244. return strings.Split(deadline, "|")
  245. },
  246. "DefaultTheme": func() string {
  247. return setting.UI.DefaultTheme
  248. },
  249. "dict": func(values ...interface{}) (map[string]interface{}, error) {
  250. if len(values) == 0 {
  251. return nil, errors.New("invalid dict call")
  252. }
  253. dict := make(map[string]interface{})
  254. for i := 0; i < len(values); i++ {
  255. switch key := values[i].(type) {
  256. case string:
  257. i++
  258. if i == len(values) {
  259. return nil, errors.New("specify the key for non array values")
  260. }
  261. dict[key] = values[i]
  262. case map[string]interface{}:
  263. m := values[i].(map[string]interface{})
  264. for i, v := range m {
  265. dict[i] = v
  266. }
  267. default:
  268. return nil, errors.New("dict values must be maps")
  269. }
  270. }
  271. return dict, nil
  272. },
  273. "percentage": func(n int, values ...int) float32 {
  274. var sum = 0
  275. for i := 0; i < len(values); i++ {
  276. sum += values[i]
  277. }
  278. return float32(n) * 100 / float32(sum)
  279. },
  280. "CommentMustAsDiff": gitdiff.CommentMustAsDiff,
  281. "MirrorAddress": mirror_service.Address,
  282. "MirrorFullAddress": mirror_service.AddressNoCredentials,
  283. "MirrorUserName": mirror_service.Username,
  284. "MirrorPassword": mirror_service.Password,
  285. "CommitType": func(commit interface{}) string {
  286. switch commit.(type) {
  287. case models.SignCommitWithStatuses:
  288. return "SignCommitWithStatuses"
  289. case models.SignCommit:
  290. return "SignCommit"
  291. case models.UserCommit:
  292. return "UserCommit"
  293. default:
  294. return ""
  295. }
  296. },
  297. "NotificationSettings": func() map[string]interface{} {
  298. return map[string]interface{}{
  299. "MinTimeout": int(setting.UI.Notification.MinTimeout / time.Millisecond),
  300. "TimeoutStep": int(setting.UI.Notification.TimeoutStep / time.Millisecond),
  301. "MaxTimeout": int(setting.UI.Notification.MaxTimeout / time.Millisecond),
  302. "EventSourceUpdateTime": int(setting.UI.Notification.EventSourceUpdateTime / time.Millisecond),
  303. }
  304. },
  305. "contain": func(s []int64, id int64) bool {
  306. for i := 0; i < len(s); i++ {
  307. if s[i] == id {
  308. return true
  309. }
  310. }
  311. return false
  312. },
  313. "svg": SVG,
  314. "SortArrow": func(normSort, revSort, urlSort string, isDefault bool) template.HTML {
  315. // if needed
  316. if len(normSort) == 0 || len(urlSort) == 0 {
  317. return ""
  318. }
  319. if len(urlSort) == 0 && isDefault {
  320. // if sort is sorted as default add arrow tho this table header
  321. if isDefault {
  322. return SVG("octicon-triangle-down", 16)
  323. }
  324. } else {
  325. // if sort arg is in url test if it correlates with column header sort arguments
  326. if urlSort == normSort {
  327. // the table is sorted with this header normal
  328. return SVG("octicon-triangle-down", 16)
  329. } else if urlSort == revSort {
  330. // the table is sorted with this header reverse
  331. return SVG("octicon-triangle-up", 16)
  332. }
  333. }
  334. // the table is NOT sorted with this header
  335. return ""
  336. },
  337. "RenderLabels": func(labels []*models.Label) template.HTML {
  338. html := ""
  339. for _, label := range labels {
  340. html = fmt.Sprintf("%s<div class='ui label' style='color: %s; background-color: %s'>%s</div>",
  341. html, label.ForegroundColor(), label.Color, RenderEmoji(label.Name))
  342. }
  343. return template.HTML(html)
  344. },
  345. }}
  346. }
  347. // NewTextFuncMap returns functions for injecting to text templates
  348. // It's a subset of those used for HTML and other templates
  349. func NewTextFuncMap() []texttmpl.FuncMap {
  350. return []texttmpl.FuncMap{map[string]interface{}{
  351. "GoVer": func() string {
  352. return strings.Title(runtime.Version())
  353. },
  354. "AppName": func() string {
  355. return setting.AppName
  356. },
  357. "AppSubUrl": func() string {
  358. return setting.AppSubURL
  359. },
  360. "AppUrl": func() string {
  361. return setting.AppURL
  362. },
  363. "AppVer": func() string {
  364. return setting.AppVer
  365. },
  366. "AppBuiltWith": func() string {
  367. return setting.AppBuiltWith
  368. },
  369. "AppDomain": func() string {
  370. return setting.Domain
  371. },
  372. "TimeSince": timeutil.TimeSince,
  373. "TimeSinceUnix": timeutil.TimeSinceUnix,
  374. "RawTimeSince": timeutil.RawTimeSince,
  375. "DateFmtLong": func(t time.Time) string {
  376. return t.Format(time.RFC1123Z)
  377. },
  378. "DateFmtShort": func(t time.Time) string {
  379. return t.Format("Jan 02, 2006")
  380. },
  381. "List": List,
  382. "SubStr": func(str string, start, length int) string {
  383. if len(str) == 0 {
  384. return ""
  385. }
  386. end := start + length
  387. if length == -1 {
  388. end = len(str)
  389. }
  390. if len(str) < end {
  391. return str
  392. }
  393. return str[start:end]
  394. },
  395. "EllipsisString": base.EllipsisString,
  396. "URLJoin": util.URLJoin,
  397. "Dict": func(values ...interface{}) (map[string]interface{}, error) {
  398. if len(values)%2 != 0 {
  399. return nil, errors.New("invalid dict call")
  400. }
  401. dict := make(map[string]interface{}, len(values)/2)
  402. for i := 0; i < len(values); i += 2 {
  403. key, ok := values[i].(string)
  404. if !ok {
  405. return nil, errors.New("dict keys must be strings")
  406. }
  407. dict[key] = values[i+1]
  408. }
  409. return dict, nil
  410. },
  411. "Printf": fmt.Sprintf,
  412. "Escape": Escape,
  413. "Sec2Time": models.SecToTime,
  414. "ParseDeadline": func(deadline string) []string {
  415. return strings.Split(deadline, "|")
  416. },
  417. "dict": func(values ...interface{}) (map[string]interface{}, error) {
  418. if len(values) == 0 {
  419. return nil, errors.New("invalid dict call")
  420. }
  421. dict := make(map[string]interface{})
  422. for i := 0; i < len(values); i++ {
  423. switch key := values[i].(type) {
  424. case string:
  425. i++
  426. if i == len(values) {
  427. return nil, errors.New("specify the key for non array values")
  428. }
  429. dict[key] = values[i]
  430. case map[string]interface{}:
  431. m := values[i].(map[string]interface{})
  432. for i, v := range m {
  433. dict[i] = v
  434. }
  435. default:
  436. return nil, errors.New("dict values must be maps")
  437. }
  438. }
  439. return dict, nil
  440. },
  441. "percentage": func(n int, values ...int) float32 {
  442. var sum = 0
  443. for i := 0; i < len(values); i++ {
  444. sum += values[i]
  445. }
  446. return float32(n) * 100 / float32(sum)
  447. },
  448. "Add": func(a ...int) int {
  449. sum := 0
  450. for _, val := range a {
  451. sum += val
  452. }
  453. return sum
  454. },
  455. "Mul": func(a ...int) int {
  456. sum := 1
  457. for _, val := range a {
  458. sum *= val
  459. }
  460. return sum
  461. },
  462. }}
  463. }
  464. var widthRe = regexp.MustCompile(`width="[0-9]+?"`)
  465. var heightRe = regexp.MustCompile(`height="[0-9]+?"`)
  466. // SVG render icons - arguments icon name (string), size (int), class (string)
  467. func SVG(icon string, others ...interface{}) template.HTML {
  468. size := 16
  469. if len(others) > 0 && others[0].(int) != 0 {
  470. size = others[0].(int)
  471. }
  472. class := ""
  473. if len(others) > 1 && others[1].(string) != "" {
  474. class = others[1].(string)
  475. }
  476. if svgStr, ok := svg.SVGs[icon]; ok {
  477. if size != 16 {
  478. svgStr = widthRe.ReplaceAllString(svgStr, fmt.Sprintf(`width="%d"`, size))
  479. svgStr = heightRe.ReplaceAllString(svgStr, fmt.Sprintf(`height="%d"`, size))
  480. }
  481. if class != "" {
  482. svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
  483. }
  484. return template.HTML(svgStr)
  485. }
  486. return template.HTML("")
  487. }
  488. // Safe render raw as HTML
  489. func Safe(raw string) template.HTML {
  490. return template.HTML(raw)
  491. }
  492. // SafeJS renders raw as JS
  493. func SafeJS(raw string) template.JS {
  494. return template.JS(raw)
  495. }
  496. // Str2html render Markdown text to HTML
  497. func Str2html(raw string) template.HTML {
  498. return template.HTML(markup.Sanitize(raw))
  499. }
  500. // Escape escapes a HTML string
  501. func Escape(raw string) string {
  502. return html.EscapeString(raw)
  503. }
  504. // List traversings the list
  505. func List(l *list.List) chan interface{} {
  506. e := l.Front()
  507. c := make(chan interface{})
  508. go func() {
  509. for e != nil {
  510. c <- e.Value
  511. e = e.Next()
  512. }
  513. close(c)
  514. }()
  515. return c
  516. }
  517. // Sha1 returns sha1 sum of string
  518. func Sha1(str string) string {
  519. return base.EncodeSha1(str)
  520. }
  521. // RenderCommitMessage renders commit message with XSS-safe and special links.
  522. func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML {
  523. return RenderCommitMessageLink(msg, urlPrefix, "", metas)
  524. }
  525. // RenderCommitMessageLink renders commit message as a XXS-safe link to the provided
  526. // default url, handling for special links.
  527. func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
  528. cleanMsg := template.HTMLEscapeString(msg)
  529. // we can safely assume that it will not return any error, since there
  530. // shouldn't be any special HTML.
  531. fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, urlDefault, metas)
  532. if err != nil {
  533. log.Error("RenderCommitMessage: %v", err)
  534. return ""
  535. }
  536. msgLines := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
  537. if len(msgLines) == 0 {
  538. return template.HTML("")
  539. }
  540. return template.HTML(msgLines[0])
  541. }
  542. // RenderCommitMessageLinkSubject renders commit message as a XXS-safe link to
  543. // the provided default url, handling for special links without email to links.
  544. func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
  545. msgLine := strings.TrimLeftFunc(msg, unicode.IsSpace)
  546. lineEnd := strings.IndexByte(msgLine, '\n')
  547. if lineEnd > 0 {
  548. msgLine = msgLine[:lineEnd]
  549. }
  550. msgLine = strings.TrimRightFunc(msgLine, unicode.IsSpace)
  551. if len(msgLine) == 0 {
  552. return template.HTML("")
  553. }
  554. // we can safely assume that it will not return any error, since there
  555. // shouldn't be any special HTML.
  556. renderedMessage, err := markup.RenderCommitMessageSubject([]byte(template.HTMLEscapeString(msgLine)), urlPrefix, urlDefault, metas)
  557. if err != nil {
  558. log.Error("RenderCommitMessageSubject: %v", err)
  559. return template.HTML("")
  560. }
  561. return template.HTML(renderedMessage)
  562. }
  563. // RenderCommitBody extracts the body of a commit message without its title.
  564. func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.HTML {
  565. msgLine := strings.TrimRightFunc(msg, unicode.IsSpace)
  566. lineEnd := strings.IndexByte(msgLine, '\n')
  567. if lineEnd > 0 {
  568. msgLine = msgLine[lineEnd+1:]
  569. } else {
  570. return template.HTML("")
  571. }
  572. msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace)
  573. if len(msgLine) == 0 {
  574. return template.HTML("")
  575. }
  576. renderedMessage, err := markup.RenderCommitMessage([]byte(template.HTMLEscapeString(msgLine)), urlPrefix, "", metas)
  577. if err != nil {
  578. log.Error("RenderCommitMessage: %v", err)
  579. return ""
  580. }
  581. return template.HTML(renderedMessage)
  582. }
  583. // RenderEmoji renders html text with emoji post processors
  584. func RenderEmoji(text string) template.HTML {
  585. renderedText, err := markup.RenderEmoji([]byte(template.HTMLEscapeString(text)))
  586. if err != nil {
  587. log.Error("RenderEmoji: %v", err)
  588. return template.HTML("")
  589. }
  590. return template.HTML(renderedText)
  591. }
  592. //ReactionToEmoji renders emoji for use in reactions
  593. func ReactionToEmoji(reaction string) template.HTML {
  594. val := emoji.FromCode(reaction)
  595. if val != nil {
  596. return template.HTML(val.Emoji)
  597. }
  598. val = emoji.FromAlias(reaction)
  599. if val != nil {
  600. return template.HTML(val.Emoji)
  601. }
  602. return template.HTML(fmt.Sprintf(`<img alt=":%s:" src="%s/img/emoji/%s.png"></img>`, reaction, setting.StaticURLPrefix, reaction))
  603. }
  604. // RenderNote renders the contents of a git-notes file as a commit message.
  605. func RenderNote(msg, urlPrefix string, metas map[string]string) template.HTML {
  606. cleanMsg := template.HTMLEscapeString(msg)
  607. fullMessage, err := markup.RenderCommitMessage([]byte(cleanMsg), urlPrefix, "", metas)
  608. if err != nil {
  609. log.Error("RenderNote: %v", err)
  610. return ""
  611. }
  612. return template.HTML(string(fullMessage))
  613. }
  614. // IsMultilineCommitMessage checks to see if a commit message contains multiple lines.
  615. func IsMultilineCommitMessage(msg string) bool {
  616. return strings.Count(strings.TrimSpace(msg), "\n") >= 1
  617. }
  618. // Actioner describes an action
  619. type Actioner interface {
  620. GetOpType() models.ActionType
  621. GetActUserName() string
  622. GetRepoUserName() string
  623. GetRepoName() string
  624. GetRepoPath() string
  625. GetRepoLink() string
  626. GetBranch() string
  627. GetContent() string
  628. GetCreate() time.Time
  629. GetIssueInfos() []string
  630. }
  631. // ActionIcon accepts an action operation type and returns an icon class name.
  632. func ActionIcon(opType models.ActionType) string {
  633. switch opType {
  634. case models.ActionCreateRepo, models.ActionTransferRepo:
  635. return "repo"
  636. case models.ActionCommitRepo, models.ActionPushTag, models.ActionDeleteTag, models.ActionDeleteBranch:
  637. return "git-commit"
  638. case models.ActionCreateIssue:
  639. return "issue-opened"
  640. case models.ActionCreatePullRequest:
  641. return "git-pull-request"
  642. case models.ActionCommentIssue, models.ActionCommentPull:
  643. return "comment-discussion"
  644. case models.ActionMergePullRequest:
  645. return "git-merge"
  646. case models.ActionCloseIssue, models.ActionClosePullRequest:
  647. return "issue-closed"
  648. case models.ActionReopenIssue, models.ActionReopenPullRequest:
  649. return "issue-reopened"
  650. case models.ActionMirrorSyncPush, models.ActionMirrorSyncCreate, models.ActionMirrorSyncDelete:
  651. return "repo-clone"
  652. case models.ActionApprovePullRequest:
  653. return "check"
  654. case models.ActionRejectPullRequest:
  655. return "diff"
  656. case models.ActionPublishRelease:
  657. return "tag"
  658. default:
  659. return "question"
  660. }
  661. }
  662. // ActionContent2Commits converts action content to push commits
  663. func ActionContent2Commits(act Actioner) *repository.PushCommits {
  664. push := repository.NewPushCommits()
  665. if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
  666. log.Error("json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err)
  667. }
  668. return push
  669. }
  670. // DiffTypeToStr returns diff type name
  671. func DiffTypeToStr(diffType int) string {
  672. diffTypes := map[int]string{
  673. 1: "add", 2: "modify", 3: "del", 4: "rename", 5: "copy",
  674. }
  675. return diffTypes[diffType]
  676. }
  677. // DiffLineTypeToStr returns diff line type name
  678. func DiffLineTypeToStr(diffType int) string {
  679. switch diffType {
  680. case 2:
  681. return "add"
  682. case 3:
  683. return "del"
  684. case 4:
  685. return "tag"
  686. }
  687. return "same"
  688. }
  689. // Language specific rules for translating plural texts
  690. var trNLangRules = map[string]func(int64) int{
  691. "en-US": func(cnt int64) int {
  692. if cnt == 1 {
  693. return 0
  694. }
  695. return 1
  696. },
  697. "lv-LV": func(cnt int64) int {
  698. if cnt%10 == 1 && cnt%100 != 11 {
  699. return 0
  700. }
  701. return 1
  702. },
  703. "ru-RU": func(cnt int64) int {
  704. if cnt%10 == 1 && cnt%100 != 11 {
  705. return 0
  706. }
  707. return 1
  708. },
  709. "zh-CN": func(cnt int64) int {
  710. return 0
  711. },
  712. "zh-HK": func(cnt int64) int {
  713. return 0
  714. },
  715. "zh-TW": func(cnt int64) int {
  716. return 0
  717. },
  718. "fr-FR": func(cnt int64) int {
  719. if cnt > -2 && cnt < 2 {
  720. return 0
  721. }
  722. return 1
  723. },
  724. }
  725. // TrN returns key to be used for plural text translation
  726. func TrN(lang string, cnt interface{}, key1, keyN string) string {
  727. var c int64
  728. if t, ok := cnt.(int); ok {
  729. c = int64(t)
  730. } else if t, ok := cnt.(int16); ok {
  731. c = int64(t)
  732. } else if t, ok := cnt.(int32); ok {
  733. c = int64(t)
  734. } else if t, ok := cnt.(int64); ok {
  735. c = t
  736. } else {
  737. return keyN
  738. }
  739. ruleFunc, ok := trNLangRules[lang]
  740. if !ok {
  741. ruleFunc = trNLangRules["en-US"]
  742. }
  743. if ruleFunc(c) == 0 {
  744. return key1
  745. }
  746. return keyN
  747. }
  748. // MigrationIcon returns a Font Awesome name matching the service an issue/comment was migrated from
  749. func MigrationIcon(hostname string) string {
  750. switch hostname {
  751. case "github.com":
  752. return "fa-github"
  753. default:
  754. return "fa-git-alt"
  755. }
  756. }
  757. func buildSubjectBodyTemplate(stpl *texttmpl.Template, btpl *template.Template, name string, content []byte) {
  758. // Split template into subject and body
  759. var subjectContent []byte
  760. bodyContent := content
  761. loc := mailSubjectSplit.FindIndex(content)
  762. if loc != nil {
  763. subjectContent = content[0:loc[0]]
  764. bodyContent = content[loc[1]:]
  765. }
  766. if _, err := stpl.New(name).
  767. Parse(string(subjectContent)); err != nil {
  768. log.Warn("Failed to parse template [%s/subject]: %v", name, err)
  769. }
  770. if _, err := btpl.New(name).
  771. Parse(string(bodyContent)); err != nil {
  772. log.Warn("Failed to parse template [%s/body]: %v", name, err)
  773. }
  774. }