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.

221 lines
6.1 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
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. sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{40}\b`)
  89. )
  90. func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
  91. buf := bytes.NewBufferString("")
  92. inCodeBlock := false
  93. codeBlockPrefix := []byte("```")
  94. lineBreak := []byte("\n")
  95. tab := []byte("\t")
  96. lines := bytes.Split(rawBytes, lineBreak)
  97. for _, line := range lines {
  98. if bytes.HasPrefix(line, codeBlockPrefix) {
  99. inCodeBlock = !inCodeBlock
  100. }
  101. if !inCodeBlock && !bytes.HasPrefix(line, tab) {
  102. ms := MentionPattern.FindAll(line, -1)
  103. for _, m := range ms {
  104. line = bytes.Replace(line, m,
  105. []byte(fmt.Sprintf(`<a href="%s/user/%s">%s</a>`, setting.AppSubUrl, m[1:], m)), -1)
  106. }
  107. }
  108. buf.Write(line)
  109. buf.Write(lineBreak)
  110. }
  111. rawBytes = buf.Bytes()
  112. ms := commitPattern.FindAll(rawBytes, -1)
  113. for _, m := range ms {
  114. m = bytes.TrimSpace(m)
  115. i := strings.Index(string(m), "commit/")
  116. j := strings.Index(string(m), "#")
  117. if j == -1 {
  118. j = len(m)
  119. }
  120. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  121. ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1)
  122. }
  123. ms = issueFullPattern.FindAll(rawBytes, -1)
  124. for _, m := range ms {
  125. m = bytes.TrimSpace(m)
  126. i := strings.Index(string(m), "issues/")
  127. j := strings.Index(string(m), "#")
  128. if j == -1 {
  129. j = len(m)
  130. }
  131. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  132. ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
  133. }
  134. rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix)
  135. rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix)
  136. return rawBytes
  137. }
  138. func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
  139. ms := sha1CurrentPattern.FindAll(rawBytes, -1)
  140. for _, m := range ms {
  141. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  142. `<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, ShortSha(string(m)))), -1)
  143. }
  144. return rawBytes
  145. }
  146. func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string) []byte {
  147. ms := issueIndexPattern.FindAll(rawBytes, -1)
  148. for _, m := range ms {
  149. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  150. `<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1)
  151. }
  152. return rawBytes
  153. }
  154. func RenderRawMarkdown(body []byte, urlPrefix string) []byte {
  155. htmlFlags := 0
  156. // htmlFlags |= blackfriday.HTML_USE_XHTML
  157. // htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
  158. // htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
  159. // htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
  160. // htmlFlags |= blackfriday.HTML_SKIP_HTML
  161. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  162. // htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
  163. // htmlFlags |= blackfriday.HTML_GITHUB_BLOCKCODE
  164. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  165. // htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
  166. renderer := &CustomRender{
  167. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  168. urlPrefix: urlPrefix,
  169. }
  170. // set up the parser
  171. extensions := 0
  172. extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
  173. extensions |= blackfriday.EXTENSION_TABLES
  174. extensions |= blackfriday.EXTENSION_FENCED_CODE
  175. extensions |= blackfriday.EXTENSION_AUTOLINK
  176. extensions |= blackfriday.EXTENSION_STRIKETHROUGH
  177. extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
  178. extensions |= blackfriday.EXTENSION_SPACE_HEADERS
  179. extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  180. body = blackfriday.Markdown(body, renderer, extensions)
  181. return body
  182. }
  183. func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
  184. body := RenderSpecialLink(rawBytes, urlPrefix)
  185. body = RenderRawMarkdown(body, urlPrefix)
  186. body = XSS(body)
  187. return body
  188. }
  189. func RenderMarkdownString(raw, urlPrefix string) string {
  190. return string(RenderMarkdown([]byte(raw), urlPrefix))
  191. }