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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. "errors"
  9. "fmt"
  10. "html"
  11. "html/template"
  12. "mime"
  13. "net/url"
  14. "path/filepath"
  15. "reflect"
  16. "regexp"
  17. "runtime"
  18. "strings"
  19. texttmpl "text/template"
  20. "time"
  21. "unicode"
  22. "code.gitea.io/gitea/models"
  23. "code.gitea.io/gitea/modules/base"
  24. "code.gitea.io/gitea/modules/emoji"
  25. "code.gitea.io/gitea/modules/git"
  26. "code.gitea.io/gitea/modules/json"
  27. "code.gitea.io/gitea/modules/log"
  28. "code.gitea.io/gitea/modules/markup"
  29. "code.gitea.io/gitea/modules/repository"
  30. "code.gitea.io/gitea/modules/setting"
  31. "code.gitea.io/gitea/modules/svg"
  32. "code.gitea.io/gitea/modules/timeutil"
  33. "code.gitea.io/gitea/modules/util"
  34. "code.gitea.io/gitea/services/gitdiff"
  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. "AssetUrlPrefix": func() string {
  55. return setting.StaticURLPrefix + "/assets"
  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. "CustomEmojis": func() map[string]string {
  85. return setting.UI.CustomEmojisMap
  86. },
  87. "Safe": Safe,
  88. "SafeJS": SafeJS,
  89. "JSEscape": JSEscape,
  90. "Str2html": Str2html,
  91. "TimeSince": timeutil.TimeSince,
  92. "TimeSinceUnix": timeutil.TimeSinceUnix,
  93. "RawTimeSince": timeutil.RawTimeSince,
  94. "FileSize": base.FileSize,
  95. "PrettyNumber": base.PrettyNumber,
  96. "Subtract": base.Subtract,
  97. "EntryIcon": base.EntryIcon,
  98. "MigrationIcon": MigrationIcon,
  99. "Add": func(a ...int) int {
  100. sum := 0
  101. for _, val := range a {
  102. sum += val
  103. }
  104. return sum
  105. },
  106. "Mul": func(a ...int) int {
  107. sum := 1
  108. for _, val := range a {
  109. sum *= val
  110. }
  111. return sum
  112. },
  113. "ActionIcon": ActionIcon,
  114. "DateFmtLong": func(t time.Time) string {
  115. return t.Format(time.RFC1123Z)
  116. },
  117. "DateFmtShort": func(t time.Time) string {
  118. return t.Format("Jan 02, 2006")
  119. },
  120. "SizeFmt": base.FileSize,
  121. "CountFmt": base.FormatNumberSI,
  122. "SubStr": func(str string, start, length int) string {
  123. if len(str) == 0 {
  124. return ""
  125. }
  126. end := start + length
  127. if length == -1 {
  128. end = len(str)
  129. }
  130. if len(str) < end {
  131. return str
  132. }
  133. return str[start:end]
  134. },
  135. "EllipsisString": base.EllipsisString,
  136. "DiffTypeToStr": DiffTypeToStr,
  137. "DiffLineTypeToStr": DiffLineTypeToStr,
  138. "Sha1": Sha1,
  139. "ShortSha": base.ShortSha,
  140. "MD5": base.EncodeMD5,
  141. "ActionContent2Commits": ActionContent2Commits,
  142. "PathEscape": url.PathEscape,
  143. "EscapePound": func(str string) string {
  144. return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str)
  145. },
  146. "PathEscapeSegments": util.PathEscapeSegments,
  147. "URLJoin": util.URLJoin,
  148. "RenderCommitMessage": RenderCommitMessage,
  149. "RenderCommitMessageLink": RenderCommitMessageLink,
  150. "RenderCommitMessageLinkSubject": RenderCommitMessageLinkSubject,
  151. "RenderCommitBody": RenderCommitBody,
  152. "RenderIssueTitle": RenderIssueTitle,
  153. "RenderEmoji": RenderEmoji,
  154. "RenderEmojiPlain": emoji.ReplaceAliases,
  155. "ReactionToEmoji": ReactionToEmoji,
  156. "RenderNote": RenderNote,
  157. "IsMultilineCommitMessage": IsMultilineCommitMessage,
  158. "ThemeColorMetaTag": func() string {
  159. return setting.UI.ThemeColorMetaTag
  160. },
  161. "MetaAuthor": func() string {
  162. return setting.UI.Meta.Author
  163. },
  164. "MetaDescription": func() string {
  165. return setting.UI.Meta.Description
  166. },
  167. "MetaKeywords": func() string {
  168. return setting.UI.Meta.Keywords
  169. },
  170. "UseServiceWorker": func() bool {
  171. return setting.UI.UseServiceWorker
  172. },
  173. "EnableTimetracking": func() bool {
  174. return setting.Service.EnableTimetracking
  175. },
  176. "FilenameIsImage": func(filename string) bool {
  177. mimeType := mime.TypeByExtension(filepath.Ext(filename))
  178. return strings.HasPrefix(mimeType, "image/")
  179. },
  180. "TabSizeClass": func(ec interface{}, filename string) string {
  181. var (
  182. value *editorconfig.Editorconfig
  183. ok bool
  184. )
  185. if ec != nil {
  186. if value, ok = ec.(*editorconfig.Editorconfig); !ok || value == nil {
  187. return "tab-size-8"
  188. }
  189. def, err := value.GetDefinitionForFilename(filename)
  190. if err != nil {
  191. log.Error("tab size class: getting definition for filename: %v", err)
  192. return "tab-size-8"
  193. }
  194. if def.TabWidth > 0 {
  195. return fmt.Sprintf("tab-size-%d", def.TabWidth)
  196. }
  197. }
  198. return "tab-size-8"
  199. },
  200. "SubJumpablePath": func(str string) []string {
  201. var path []string
  202. index := strings.LastIndex(str, "/")
  203. if index != -1 && index != len(str) {
  204. path = append(path, str[0:index+1], str[index+1:])
  205. } else {
  206. path = append(path, str)
  207. }
  208. return path
  209. },
  210. "DiffStatsWidth": func(adds int, dels int) string {
  211. return fmt.Sprintf("%f", float64(adds)/(float64(adds)+float64(dels))*100)
  212. },
  213. "Json": func(in interface{}) string {
  214. out, err := json.Marshal(in)
  215. if err != nil {
  216. return ""
  217. }
  218. return string(out)
  219. },
  220. "JsonPrettyPrint": func(in string) string {
  221. var out bytes.Buffer
  222. err := json.Indent(&out, []byte(in), "", " ")
  223. if err != nil {
  224. return ""
  225. }
  226. return out.String()
  227. },
  228. "DisableGitHooks": func() bool {
  229. return setting.DisableGitHooks
  230. },
  231. "DisableWebhooks": func() bool {
  232. return setting.DisableWebhooks
  233. },
  234. "DisableImportLocal": func() bool {
  235. return !setting.ImportLocalPaths
  236. },
  237. "TrN": TrN,
  238. "Dict": func(values ...interface{}) (map[string]interface{}, error) {
  239. if len(values)%2 != 0 {
  240. return nil, errors.New("invalid dict call")
  241. }
  242. dict := make(map[string]interface{}, len(values)/2)
  243. for i := 0; i < len(values); i += 2 {
  244. key, ok := values[i].(string)
  245. if !ok {
  246. return nil, errors.New("dict keys must be strings")
  247. }
  248. dict[key] = values[i+1]
  249. }
  250. return dict, nil
  251. },
  252. "Printf": fmt.Sprintf,
  253. "Escape": Escape,
  254. "Sec2Time": models.SecToTime,
  255. "ParseDeadline": func(deadline string) []string {
  256. return strings.Split(deadline, "|")
  257. },
  258. "DefaultTheme": func() string {
  259. return setting.UI.DefaultTheme
  260. },
  261. // pass key-value pairs to a partial template which receives them as a dict
  262. "dict": func(values ...interface{}) (map[string]interface{}, error) {
  263. if len(values) == 0 {
  264. return nil, errors.New("invalid dict call")
  265. }
  266. dict := make(map[string]interface{})
  267. return util.MergeInto(dict, values...)
  268. },
  269. /* like dict but merge key-value pairs into the first dict and return it */
  270. "mergeinto": func(root map[string]interface{}, values ...interface{}) (map[string]interface{}, error) {
  271. if len(values) == 0 {
  272. return nil, errors.New("invalid mergeinto call")
  273. }
  274. dict := make(map[string]interface{})
  275. for key, value := range root {
  276. dict[key] = value
  277. }
  278. return util.MergeInto(dict, values...)
  279. },
  280. "percentage": func(n int, values ...int) float32 {
  281. var sum = 0
  282. for i := 0; i < len(values); i++ {
  283. sum += values[i]
  284. }
  285. return float32(n) * 100 / float32(sum)
  286. },
  287. "CommentMustAsDiff": gitdiff.CommentMustAsDiff,
  288. "MirrorRemoteAddress": mirrorRemoteAddress,
  289. "NotificationSettings": func() map[string]interface{} {
  290. return map[string]interface{}{
  291. "MinTimeout": int(setting.UI.Notification.MinTimeout / time.Millisecond),
  292. "TimeoutStep": int(setting.UI.Notification.TimeoutStep / time.Millisecond),
  293. "MaxTimeout": int(setting.UI.Notification.MaxTimeout / time.Millisecond),
  294. "EventSourceUpdateTime": int(setting.UI.Notification.EventSourceUpdateTime / time.Millisecond),
  295. }
  296. },
  297. "containGeneric": func(arr interface{}, v interface{}) bool {
  298. arrV := reflect.ValueOf(arr)
  299. if arrV.Kind() == reflect.String && reflect.ValueOf(v).Kind() == reflect.String {
  300. return strings.Contains(arr.(string), v.(string))
  301. }
  302. if arrV.Kind() == reflect.Slice {
  303. for i := 0; i < arrV.Len(); i++ {
  304. iV := arrV.Index(i)
  305. if !iV.CanInterface() {
  306. continue
  307. }
  308. if iV.Interface() == v {
  309. return true
  310. }
  311. }
  312. }
  313. return false
  314. },
  315. "contain": func(s []int64, id int64) bool {
  316. for i := 0; i < len(s); i++ {
  317. if s[i] == id {
  318. return true
  319. }
  320. }
  321. return false
  322. },
  323. "svg": SVG,
  324. "avatar": Avatar,
  325. "avatarHTML": AvatarHTML,
  326. "avatarByAction": AvatarByAction,
  327. "avatarByEmail": AvatarByEmail,
  328. "repoAvatar": RepoAvatar,
  329. "SortArrow": func(normSort, revSort, urlSort string, isDefault bool) template.HTML {
  330. // if needed
  331. if len(normSort) == 0 || len(urlSort) == 0 {
  332. return ""
  333. }
  334. if len(urlSort) == 0 && isDefault {
  335. // if sort is sorted as default add arrow tho this table header
  336. if isDefault {
  337. return SVG("octicon-triangle-down", 16)
  338. }
  339. } else {
  340. // if sort arg is in url test if it correlates with column header sort arguments
  341. if urlSort == normSort {
  342. // the table is sorted with this header normal
  343. return SVG("octicon-triangle-down", 16)
  344. } else if urlSort == revSort {
  345. // the table is sorted with this header reverse
  346. return SVG("octicon-triangle-up", 16)
  347. }
  348. }
  349. // the table is NOT sorted with this header
  350. return ""
  351. },
  352. "RenderLabels": func(labels []*models.Label) template.HTML {
  353. html := `<span class="labels-list">`
  354. for _, label := range labels {
  355. // Protect against nil value in labels - shouldn't happen but would cause a panic if so
  356. if label == nil {
  357. continue
  358. }
  359. html += fmt.Sprintf("<div class='ui label' style='color: %s; background-color: %s'>%s</div> ",
  360. label.ForegroundColor(), label.Color, RenderEmoji(label.Name))
  361. }
  362. html += "</span>"
  363. return template.HTML(html)
  364. },
  365. "MermaidMaxSourceCharacters": func() int {
  366. return setting.MermaidMaxSourceCharacters
  367. },
  368. }}
  369. }
  370. // NewTextFuncMap returns functions for injecting to text templates
  371. // It's a subset of those used for HTML and other templates
  372. func NewTextFuncMap() []texttmpl.FuncMap {
  373. return []texttmpl.FuncMap{map[string]interface{}{
  374. "GoVer": func() string {
  375. return strings.Title(runtime.Version())
  376. },
  377. "AppName": func() string {
  378. return setting.AppName
  379. },
  380. "AppSubUrl": func() string {
  381. return setting.AppSubURL
  382. },
  383. "AppUrl": func() string {
  384. return setting.AppURL
  385. },
  386. "AppVer": func() string {
  387. return setting.AppVer
  388. },
  389. "AppBuiltWith": func() string {
  390. return setting.AppBuiltWith
  391. },
  392. "AppDomain": func() string {
  393. return setting.Domain
  394. },
  395. "TimeSince": timeutil.TimeSince,
  396. "TimeSinceUnix": timeutil.TimeSinceUnix,
  397. "RawTimeSince": timeutil.RawTimeSince,
  398. "DateFmtLong": func(t time.Time) string {
  399. return t.Format(time.RFC1123Z)
  400. },
  401. "DateFmtShort": func(t time.Time) string {
  402. return t.Format("Jan 02, 2006")
  403. },
  404. "SubStr": func(str string, start, length int) string {
  405. if len(str) == 0 {
  406. return ""
  407. }
  408. end := start + length
  409. if length == -1 {
  410. end = len(str)
  411. }
  412. if len(str) < end {
  413. return str
  414. }
  415. return str[start:end]
  416. },
  417. "EllipsisString": base.EllipsisString,
  418. "URLJoin": util.URLJoin,
  419. "Dict": func(values ...interface{}) (map[string]interface{}, error) {
  420. if len(values)%2 != 0 {
  421. return nil, errors.New("invalid dict call")
  422. }
  423. dict := make(map[string]interface{}, len(values)/2)
  424. for i := 0; i < len(values); i += 2 {
  425. key, ok := values[i].(string)
  426. if !ok {
  427. return nil, errors.New("dict keys must be strings")
  428. }
  429. dict[key] = values[i+1]
  430. }
  431. return dict, nil
  432. },
  433. "Printf": fmt.Sprintf,
  434. "Escape": Escape,
  435. "Sec2Time": models.SecToTime,
  436. "ParseDeadline": func(deadline string) []string {
  437. return strings.Split(deadline, "|")
  438. },
  439. "dict": func(values ...interface{}) (map[string]interface{}, error) {
  440. if len(values) == 0 {
  441. return nil, errors.New("invalid dict call")
  442. }
  443. dict := make(map[string]interface{})
  444. for i := 0; i < len(values); i++ {
  445. switch key := values[i].(type) {
  446. case string:
  447. i++
  448. if i == len(values) {
  449. return nil, errors.New("specify the key for non array values")
  450. }
  451. dict[key] = values[i]
  452. case map[string]interface{}:
  453. m := values[i].(map[string]interface{})
  454. for i, v := range m {
  455. dict[i] = v
  456. }
  457. default:
  458. return nil, errors.New("dict values must be maps")
  459. }
  460. }
  461. return dict, nil
  462. },
  463. "percentage": func(n int, values ...int) float32 {
  464. var sum = 0
  465. for i := 0; i < len(values); i++ {
  466. sum += values[i]
  467. }
  468. return float32(n) * 100 / float32(sum)
  469. },
  470. "Add": func(a ...int) int {
  471. sum := 0
  472. for _, val := range a {
  473. sum += val
  474. }
  475. return sum
  476. },
  477. "Mul": func(a ...int) int {
  478. sum := 1
  479. for _, val := range a {
  480. sum *= val
  481. }
  482. return sum
  483. },
  484. }}
  485. }
  486. var widthRe = regexp.MustCompile(`width="[0-9]+?"`)
  487. var heightRe = regexp.MustCompile(`height="[0-9]+?"`)
  488. func parseOthers(defaultSize int, defaultClass string, others ...interface{}) (int, string) {
  489. size := defaultSize
  490. if len(others) > 0 && others[0].(int) != 0 {
  491. size = others[0].(int)
  492. }
  493. class := defaultClass
  494. if len(others) > 1 && others[1].(string) != "" {
  495. if defaultClass == "" {
  496. class = others[1].(string)
  497. } else {
  498. class = defaultClass + " " + others[1].(string)
  499. }
  500. }
  501. return size, class
  502. }
  503. // AvatarHTML creates the HTML for an avatar
  504. func AvatarHTML(src string, size int, class string, name string) template.HTML {
  505. sizeStr := fmt.Sprintf(`%d`, size)
  506. if name == "" {
  507. name = "avatar"
  508. }
  509. return template.HTML(`<img class="` + class + `" src="` + src + `" title="` + html.EscapeString(name) + `" width="` + sizeStr + `" height="` + sizeStr + `"/>`)
  510. }
  511. // SVG render icons - arguments icon name (string), size (int), class (string)
  512. func SVG(icon string, others ...interface{}) template.HTML {
  513. size, class := parseOthers(16, "", others...)
  514. if svgStr, ok := svg.SVGs[icon]; ok {
  515. if size != 16 {
  516. svgStr = widthRe.ReplaceAllString(svgStr, fmt.Sprintf(`width="%d"`, size))
  517. svgStr = heightRe.ReplaceAllString(svgStr, fmt.Sprintf(`height="%d"`, size))
  518. }
  519. if class != "" {
  520. svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
  521. }
  522. return template.HTML(svgStr)
  523. }
  524. return template.HTML("")
  525. }
  526. // Avatar renders user avatars. args: user, size (int), class (string)
  527. func Avatar(item interface{}, others ...interface{}) template.HTML {
  528. size, class := parseOthers(models.DefaultAvatarPixelSize, "ui avatar image", others...)
  529. if user, ok := item.(*models.User); ok {
  530. src := user.RealSizedAvatarLink(size * models.AvatarRenderedSizeFactor)
  531. if src != "" {
  532. return AvatarHTML(src, size, class, user.DisplayName())
  533. }
  534. }
  535. if user, ok := item.(*models.Collaborator); ok {
  536. src := user.RealSizedAvatarLink(size * models.AvatarRenderedSizeFactor)
  537. if src != "" {
  538. return AvatarHTML(src, size, class, user.DisplayName())
  539. }
  540. }
  541. return template.HTML("")
  542. }
  543. // AvatarByAction renders user avatars from action. args: action, size (int), class (string)
  544. func AvatarByAction(action *models.Action, others ...interface{}) template.HTML {
  545. action.LoadActUser()
  546. return Avatar(action.ActUser, others...)
  547. }
  548. // RepoAvatar renders repo avatars. args: repo, size(int), class (string)
  549. func RepoAvatar(repo *models.Repository, others ...interface{}) template.HTML {
  550. size, class := parseOthers(models.DefaultAvatarPixelSize, "ui avatar image", others...)
  551. src := repo.RelAvatarLink()
  552. if src != "" {
  553. return AvatarHTML(src, size, class, repo.FullName())
  554. }
  555. return template.HTML("")
  556. }
  557. // AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
  558. func AvatarByEmail(email string, name string, others ...interface{}) template.HTML {
  559. size, class := parseOthers(models.DefaultAvatarPixelSize, "ui avatar image", others...)
  560. src := models.SizedAvatarLink(email, size*models.AvatarRenderedSizeFactor)
  561. if src != "" {
  562. return AvatarHTML(src, size, class, name)
  563. }
  564. return template.HTML("")
  565. }
  566. // Safe render raw as HTML
  567. func Safe(raw string) template.HTML {
  568. return template.HTML(raw)
  569. }
  570. // SafeJS renders raw as JS
  571. func SafeJS(raw string) template.JS {
  572. return template.JS(raw)
  573. }
  574. // Str2html render Markdown text to HTML
  575. func Str2html(raw string) template.HTML {
  576. return template.HTML(markup.Sanitize(raw))
  577. }
  578. // Escape escapes a HTML string
  579. func Escape(raw string) string {
  580. return html.EscapeString(raw)
  581. }
  582. // JSEscape escapes a JS string
  583. func JSEscape(raw string) string {
  584. return template.JSEscapeString(raw)
  585. }
  586. // Sha1 returns sha1 sum of string
  587. func Sha1(str string) string {
  588. return base.EncodeSha1(str)
  589. }
  590. // RenderCommitMessage renders commit message with XSS-safe and special links.
  591. func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML {
  592. return RenderCommitMessageLink(msg, urlPrefix, "", metas)
  593. }
  594. // RenderCommitMessageLink renders commit message as a XXS-safe link to the provided
  595. // default url, handling for special links.
  596. func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
  597. cleanMsg := template.HTMLEscapeString(msg)
  598. // we can safely assume that it will not return any error, since there
  599. // shouldn't be any special HTML.
  600. fullMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
  601. URLPrefix: urlPrefix,
  602. DefaultLink: urlDefault,
  603. Metas: metas,
  604. }, cleanMsg)
  605. if err != nil {
  606. log.Error("RenderCommitMessage: %v", err)
  607. return ""
  608. }
  609. msgLines := strings.Split(strings.TrimSpace(string(fullMessage)), "\n")
  610. if len(msgLines) == 0 {
  611. return template.HTML("")
  612. }
  613. return template.HTML(msgLines[0])
  614. }
  615. // RenderCommitMessageLinkSubject renders commit message as a XXS-safe link to
  616. // the provided default url, handling for special links without email to links.
  617. func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
  618. msgLine := strings.TrimLeftFunc(msg, unicode.IsSpace)
  619. lineEnd := strings.IndexByte(msgLine, '\n')
  620. if lineEnd > 0 {
  621. msgLine = msgLine[:lineEnd]
  622. }
  623. msgLine = strings.TrimRightFunc(msgLine, unicode.IsSpace)
  624. if len(msgLine) == 0 {
  625. return template.HTML("")
  626. }
  627. // we can safely assume that it will not return any error, since there
  628. // shouldn't be any special HTML.
  629. renderedMessage, err := markup.RenderCommitMessageSubject(&markup.RenderContext{
  630. URLPrefix: urlPrefix,
  631. DefaultLink: urlDefault,
  632. Metas: metas,
  633. }, template.HTMLEscapeString(msgLine))
  634. if err != nil {
  635. log.Error("RenderCommitMessageSubject: %v", err)
  636. return template.HTML("")
  637. }
  638. return template.HTML(renderedMessage)
  639. }
  640. // RenderCommitBody extracts the body of a commit message without its title.
  641. func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.HTML {
  642. msgLine := strings.TrimRightFunc(msg, unicode.IsSpace)
  643. lineEnd := strings.IndexByte(msgLine, '\n')
  644. if lineEnd > 0 {
  645. msgLine = msgLine[lineEnd+1:]
  646. } else {
  647. return template.HTML("")
  648. }
  649. msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace)
  650. if len(msgLine) == 0 {
  651. return template.HTML("")
  652. }
  653. renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
  654. URLPrefix: urlPrefix,
  655. Metas: metas,
  656. }, template.HTMLEscapeString(msgLine))
  657. if err != nil {
  658. log.Error("RenderCommitMessage: %v", err)
  659. return ""
  660. }
  661. return template.HTML(renderedMessage)
  662. }
  663. // RenderIssueTitle renders issue/pull title with defined post processors
  664. func RenderIssueTitle(text, urlPrefix string, metas map[string]string) template.HTML {
  665. renderedText, err := markup.RenderIssueTitle(&markup.RenderContext{
  666. URLPrefix: urlPrefix,
  667. Metas: metas,
  668. }, template.HTMLEscapeString(text))
  669. if err != nil {
  670. log.Error("RenderIssueTitle: %v", err)
  671. return template.HTML("")
  672. }
  673. return template.HTML(renderedText)
  674. }
  675. // RenderEmoji renders html text with emoji post processors
  676. func RenderEmoji(text string) template.HTML {
  677. renderedText, err := markup.RenderEmoji(template.HTMLEscapeString(text))
  678. if err != nil {
  679. log.Error("RenderEmoji: %v", err)
  680. return template.HTML("")
  681. }
  682. return template.HTML(renderedText)
  683. }
  684. //ReactionToEmoji renders emoji for use in reactions
  685. func ReactionToEmoji(reaction string) template.HTML {
  686. val := emoji.FromCode(reaction)
  687. if val != nil {
  688. return template.HTML(val.Emoji)
  689. }
  690. val = emoji.FromAlias(reaction)
  691. if val != nil {
  692. return template.HTML(val.Emoji)
  693. }
  694. return template.HTML(fmt.Sprintf(`<img alt=":%s:" src="%s/assets/img/emoji/%s.png"></img>`, reaction, setting.StaticURLPrefix, reaction))
  695. }
  696. // RenderNote renders the contents of a git-notes file as a commit message.
  697. func RenderNote(msg, urlPrefix string, metas map[string]string) template.HTML {
  698. cleanMsg := template.HTMLEscapeString(msg)
  699. fullMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
  700. URLPrefix: urlPrefix,
  701. Metas: metas,
  702. }, cleanMsg)
  703. if err != nil {
  704. log.Error("RenderNote: %v", err)
  705. return ""
  706. }
  707. return template.HTML(string(fullMessage))
  708. }
  709. // IsMultilineCommitMessage checks to see if a commit message contains multiple lines.
  710. func IsMultilineCommitMessage(msg string) bool {
  711. return strings.Count(strings.TrimSpace(msg), "\n") >= 1
  712. }
  713. // Actioner describes an action
  714. type Actioner interface {
  715. GetOpType() models.ActionType
  716. GetActUserName() string
  717. GetRepoUserName() string
  718. GetRepoName() string
  719. GetRepoPath() string
  720. GetRepoLink() string
  721. GetBranch() string
  722. GetContent() string
  723. GetCreate() time.Time
  724. GetIssueInfos() []string
  725. }
  726. // ActionIcon accepts an action operation type and returns an icon class name.
  727. func ActionIcon(opType models.ActionType) string {
  728. switch opType {
  729. case models.ActionCreateRepo, models.ActionTransferRepo, models.ActionRenameRepo:
  730. return "repo"
  731. case models.ActionCommitRepo, models.ActionPushTag, models.ActionDeleteTag, models.ActionDeleteBranch:
  732. return "git-commit"
  733. case models.ActionCreateIssue:
  734. return "issue-opened"
  735. case models.ActionCreatePullRequest:
  736. return "git-pull-request"
  737. case models.ActionCommentIssue, models.ActionCommentPull:
  738. return "comment-discussion"
  739. case models.ActionMergePullRequest:
  740. return "git-merge"
  741. case models.ActionCloseIssue, models.ActionClosePullRequest:
  742. return "issue-closed"
  743. case models.ActionReopenIssue, models.ActionReopenPullRequest:
  744. return "issue-reopened"
  745. case models.ActionMirrorSyncPush, models.ActionMirrorSyncCreate, models.ActionMirrorSyncDelete:
  746. return "mirror"
  747. case models.ActionApprovePullRequest:
  748. return "check"
  749. case models.ActionRejectPullRequest:
  750. return "diff"
  751. case models.ActionPublishRelease:
  752. return "tag"
  753. case models.ActionPullReviewDismissed:
  754. return "x"
  755. default:
  756. return "question"
  757. }
  758. }
  759. // ActionContent2Commits converts action content to push commits
  760. func ActionContent2Commits(act Actioner) *repository.PushCommits {
  761. push := repository.NewPushCommits()
  762. if act == nil || act.GetContent() == "" {
  763. return push
  764. }
  765. if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
  766. log.Error("json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err)
  767. }
  768. if push.Len == 0 {
  769. push.Len = len(push.Commits)
  770. }
  771. return push
  772. }
  773. // DiffTypeToStr returns diff type name
  774. func DiffTypeToStr(diffType int) string {
  775. diffTypes := map[int]string{
  776. 1: "add", 2: "modify", 3: "del", 4: "rename", 5: "copy",
  777. }
  778. return diffTypes[diffType]
  779. }
  780. // DiffLineTypeToStr returns diff line type name
  781. func DiffLineTypeToStr(diffType int) string {
  782. switch diffType {
  783. case 2:
  784. return "add"
  785. case 3:
  786. return "del"
  787. case 4:
  788. return "tag"
  789. }
  790. return "same"
  791. }
  792. // Language specific rules for translating plural texts
  793. var trNLangRules = map[string]func(int64) int{
  794. "en-US": func(cnt int64) int {
  795. if cnt == 1 {
  796. return 0
  797. }
  798. return 1
  799. },
  800. "lv-LV": func(cnt int64) int {
  801. if cnt%10 == 1 && cnt%100 != 11 {
  802. return 0
  803. }
  804. return 1
  805. },
  806. "ru-RU": func(cnt int64) int {
  807. if cnt%10 == 1 && cnt%100 != 11 {
  808. return 0
  809. }
  810. return 1
  811. },
  812. "zh-CN": func(cnt int64) int {
  813. return 0
  814. },
  815. "zh-HK": func(cnt int64) int {
  816. return 0
  817. },
  818. "zh-TW": func(cnt int64) int {
  819. return 0
  820. },
  821. "fr-FR": func(cnt int64) int {
  822. if cnt > -2 && cnt < 2 {
  823. return 0
  824. }
  825. return 1
  826. },
  827. }
  828. // TrN returns key to be used for plural text translation
  829. func TrN(lang string, cnt interface{}, key1, keyN string) string {
  830. var c int64
  831. if t, ok := cnt.(int); ok {
  832. c = int64(t)
  833. } else if t, ok := cnt.(int16); ok {
  834. c = int64(t)
  835. } else if t, ok := cnt.(int32); ok {
  836. c = int64(t)
  837. } else if t, ok := cnt.(int64); ok {
  838. c = t
  839. } else {
  840. return keyN
  841. }
  842. ruleFunc, ok := trNLangRules[lang]
  843. if !ok {
  844. ruleFunc = trNLangRules["en-US"]
  845. }
  846. if ruleFunc(c) == 0 {
  847. return key1
  848. }
  849. return keyN
  850. }
  851. // MigrationIcon returns a SVG name matching the service an issue/comment was migrated from
  852. func MigrationIcon(hostname string) string {
  853. switch hostname {
  854. case "github.com":
  855. return "octicon-mark-github"
  856. default:
  857. return "gitea-git"
  858. }
  859. }
  860. func buildSubjectBodyTemplate(stpl *texttmpl.Template, btpl *template.Template, name string, content []byte) {
  861. // Split template into subject and body
  862. var subjectContent []byte
  863. bodyContent := content
  864. loc := mailSubjectSplit.FindIndex(content)
  865. if loc != nil {
  866. subjectContent = content[0:loc[0]]
  867. bodyContent = content[loc[1]:]
  868. }
  869. if _, err := stpl.New(name).
  870. Parse(string(subjectContent)); err != nil {
  871. log.Warn("Failed to parse template [%s/subject]: %v", name, err)
  872. }
  873. if _, err := btpl.New(name).
  874. Parse(string(bodyContent)); err != nil {
  875. log.Warn("Failed to parse template [%s/body]: %v", name, err)
  876. }
  877. }
  878. type remoteAddress struct {
  879. Address string
  880. Username string
  881. Password string
  882. }
  883. func mirrorRemoteAddress(m models.RemoteMirrorer) remoteAddress {
  884. a := remoteAddress{}
  885. u, err := git.GetRemoteAddress(m.GetRepository().RepoPath(), m.GetRemoteName())
  886. if err != nil {
  887. log.Error("GetRemoteAddress %v", err)
  888. return a
  889. }
  890. if u.User != nil {
  891. a.Username = u.User.Username()
  892. a.Password, _ = u.User.Password()
  893. }
  894. u.User = nil
  895. a.Address = u.String()
  896. return a
  897. }