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

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