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.

205 lines
5.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package base
  5. import (
  6. "bytes"
  7. "fmt"
  8. "net/http"
  9. "path"
  10. "path/filepath"
  11. "regexp"
  12. "strings"
  13. "github.com/russross/blackfriday"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. func isletter(c byte) bool {
  17. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
  18. }
  19. func isalnum(c byte) bool {
  20. return (c >= '0' && c <= '9') || isletter(c)
  21. }
  22. var validLinks = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")}
  23. func isLink(link []byte) bool {
  24. for _, prefix := range validLinks {
  25. if len(link) > len(prefix) && bytes.Equal(bytes.ToLower(link[:len(prefix)]), prefix) && isalnum(link[len(prefix)]) {
  26. return true
  27. }
  28. }
  29. return false
  30. }
  31. func IsMarkdownFile(name string) bool {
  32. name = strings.ToLower(name)
  33. switch filepath.Ext(name) {
  34. case ".md", ".markdown", ".mdown", ".mkd":
  35. return true
  36. }
  37. return false
  38. }
  39. func IsTextFile(data []byte) (string, bool) {
  40. contentType := http.DetectContentType(data)
  41. if strings.Index(contentType, "text/") != -1 {
  42. return contentType, true
  43. }
  44. return contentType, false
  45. }
  46. func IsImageFile(data []byte) (string, bool) {
  47. contentType := http.DetectContentType(data)
  48. if strings.Index(contentType, "image/") != -1 {
  49. return contentType, true
  50. }
  51. return contentType, false
  52. }
  53. func IsReadmeFile(name string) bool {
  54. name = strings.ToLower(name)
  55. if len(name) < 6 {
  56. return false
  57. }
  58. if name[:6] == "readme" {
  59. return true
  60. }
  61. return false
  62. }
  63. type CustomRender struct {
  64. blackfriday.Renderer
  65. urlPrefix string
  66. }
  67. func (options *CustomRender) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  68. if len(link) > 0 && !isLink(link) {
  69. if link[0] == '#' {
  70. // link = append([]byte(options.urlPrefix), link...)
  71. } else {
  72. link = []byte(path.Join(options.urlPrefix, string(link)))
  73. }
  74. }
  75. options.Renderer.Link(out, link, title, content)
  76. }
  77. func (options *CustomRender) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  78. if len(link) > 0 && !isLink(link) {
  79. link = []byte(path.Join(strings.Replace(options.urlPrefix, "/src/", "/raw/", 1), string(link)))
  80. }
  81. options.Renderer.Image(out, link, title, alt)
  82. }
  83. var (
  84. MentionPattern = regexp.MustCompile(`@[0-9a-zA-Z_]{1,}`)
  85. commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`)
  86. issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`)
  87. issueIndexPattern = regexp.MustCompile(`#[0-9]+`)
  88. )
  89. func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
  90. buf := bytes.NewBufferString("")
  91. inCodeBlock := false
  92. codeBlockPrefix := []byte("```")
  93. lineBreak := []byte("\n")
  94. tab := []byte("\t")
  95. lines := bytes.Split(rawBytes, lineBreak)
  96. for _, line := range lines {
  97. if bytes.HasPrefix(line, codeBlockPrefix) {
  98. inCodeBlock = !inCodeBlock
  99. }
  100. if !inCodeBlock && !bytes.HasPrefix(line, tab) {
  101. ms := MentionPattern.FindAll(line, -1)
  102. for _, m := range ms {
  103. line = bytes.Replace(line, m,
  104. []byte(fmt.Sprintf(`<a href="%s/user/%s">%s</a>`, setting.AppSubUrl, m[1:], m)), -1)
  105. }
  106. }
  107. buf.Write(line)
  108. buf.Write(lineBreak)
  109. }
  110. rawBytes = buf.Bytes()
  111. ms := commitPattern.FindAll(rawBytes, -1)
  112. for _, m := range ms {
  113. m = bytes.TrimSpace(m)
  114. i := strings.Index(string(m), "commit/")
  115. j := strings.Index(string(m), "#")
  116. if j == -1 {
  117. j = len(m)
  118. }
  119. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  120. ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1)
  121. }
  122. ms = issueFullPattern.FindAll(rawBytes, -1)
  123. for _, m := range ms {
  124. m = bytes.TrimSpace(m)
  125. i := strings.Index(string(m), "issues/")
  126. j := strings.Index(string(m), "#")
  127. if j == -1 {
  128. j = len(m)
  129. }
  130. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  131. ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
  132. }
  133. ms = issueIndexPattern.FindAll(rawBytes, -1)
  134. for _, m := range ms {
  135. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  136. `<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1)
  137. }
  138. return rawBytes
  139. }
  140. func RenderRawMarkdown(body []byte, urlPrefix string) []byte {
  141. htmlFlags := 0
  142. // htmlFlags |= blackfriday.HTML_USE_XHTML
  143. // htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
  144. // htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
  145. // htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
  146. // htmlFlags |= blackfriday.HTML_SKIP_HTML
  147. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  148. // htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
  149. // htmlFlags |= blackfriday.HTML_GITHUB_BLOCKCODE
  150. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  151. // htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
  152. renderer := &CustomRender{
  153. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  154. urlPrefix: urlPrefix,
  155. }
  156. // set up the parser
  157. extensions := 0
  158. extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
  159. extensions |= blackfriday.EXTENSION_TABLES
  160. extensions |= blackfriday.EXTENSION_FENCED_CODE
  161. extensions |= blackfriday.EXTENSION_AUTOLINK
  162. extensions |= blackfriday.EXTENSION_STRIKETHROUGH
  163. extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
  164. extensions |= blackfriday.EXTENSION_SPACE_HEADERS
  165. extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  166. body = blackfriday.Markdown(body, renderer, extensions)
  167. return body
  168. }
  169. func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
  170. body := RenderSpecialLink(rawBytes, urlPrefix)
  171. body = RenderRawMarkdown(body, urlPrefix)
  172. body = XSS(body)
  173. return body
  174. }
  175. func RenderMarkdownString(raw, urlPrefix string) string {
  176. return string(RenderMarkdown([]byte(raw), urlPrefix))
  177. }