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.

228 lines
6.4 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
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. // IsReadmeFile returns true if given file name suppose to be a README file.
  54. func IsReadmeFile(name string) bool {
  55. name = strings.ToLower(name)
  56. if len(name) < 6 {
  57. return false
  58. } else if len(name) == 6 {
  59. if name == "readme" {
  60. return true
  61. }
  62. return false
  63. }
  64. if name[:7] == "readme." {
  65. return true
  66. }
  67. return false
  68. }
  69. type CustomRender struct {
  70. blackfriday.Renderer
  71. urlPrefix string
  72. }
  73. func (options *CustomRender) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  74. if len(link) > 0 && !isLink(link) {
  75. if link[0] == '#' {
  76. // link = append([]byte(options.urlPrefix), link...)
  77. } else {
  78. link = []byte(path.Join(options.urlPrefix, string(link)))
  79. }
  80. }
  81. options.Renderer.Link(out, link, title, content)
  82. }
  83. func (options *CustomRender) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  84. if len(link) > 0 && !isLink(link) {
  85. link = []byte(path.Join(strings.Replace(options.urlPrefix, "/src/", "/raw/", 1), string(link)))
  86. }
  87. options.Renderer.Image(out, link, title, alt)
  88. }
  89. var (
  90. MentionPattern = regexp.MustCompile(`(\s|^)@[0-9a-zA-Z_]+`)
  91. commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`)
  92. issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`)
  93. issueIndexPattern = regexp.MustCompile(`( |^)#[0-9]+`)
  94. sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{40}\b`)
  95. )
  96. func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
  97. buf := bytes.NewBufferString("")
  98. inCodeBlock := false
  99. codeBlockPrefix := []byte("```")
  100. lineBreak := []byte("\n")
  101. tab := []byte("\t")
  102. lines := bytes.Split(rawBytes, lineBreak)
  103. for _, line := range lines {
  104. if bytes.HasPrefix(line, codeBlockPrefix) {
  105. inCodeBlock = !inCodeBlock
  106. }
  107. if !inCodeBlock && !bytes.HasPrefix(line, tab) {
  108. ms := MentionPattern.FindAll(line, -1)
  109. for _, m := range ms {
  110. m = bytes.TrimSpace(m)
  111. line = bytes.Replace(line, m,
  112. []byte(fmt.Sprintf(`<a href="%s/%s">%s</a>`, setting.AppSubUrl, m[1:], m)), -1)
  113. }
  114. }
  115. buf.Write(line)
  116. buf.Write(lineBreak)
  117. }
  118. rawBytes = buf.Bytes()
  119. ms := commitPattern.FindAll(rawBytes, -1)
  120. for _, m := range ms {
  121. m = bytes.TrimSpace(m)
  122. i := strings.Index(string(m), "commit/")
  123. j := strings.Index(string(m), "#")
  124. if j == -1 {
  125. j = len(m)
  126. }
  127. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  128. ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1)
  129. }
  130. ms = issueFullPattern.FindAll(rawBytes, -1)
  131. for _, m := range ms {
  132. m = bytes.TrimSpace(m)
  133. i := strings.Index(string(m), "issues/")
  134. j := strings.Index(string(m), "#")
  135. if j == -1 {
  136. j = len(m)
  137. }
  138. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  139. ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
  140. }
  141. rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix)
  142. rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix)
  143. return rawBytes
  144. }
  145. func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
  146. ms := sha1CurrentPattern.FindAll(rawBytes, -1)
  147. for _, m := range ms {
  148. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  149. `<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, ShortSha(string(m)))), -1)
  150. }
  151. return rawBytes
  152. }
  153. func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string) []byte {
  154. ms := issueIndexPattern.FindAll(rawBytes, -1)
  155. for _, m := range ms {
  156. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(`<a href="%s/issues/%s">%s</a>`,
  157. urlPrefix, strings.TrimPrefix(string(m[1:]), "#"), m)), -1)
  158. }
  159. return rawBytes
  160. }
  161. func RenderRawMarkdown(body []byte, urlPrefix string) []byte {
  162. htmlFlags := 0
  163. // htmlFlags |= blackfriday.HTML_USE_XHTML
  164. // htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
  165. // htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
  166. // htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
  167. // htmlFlags |= blackfriday.HTML_SKIP_HTML
  168. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  169. // htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
  170. // htmlFlags |= blackfriday.HTML_GITHUB_BLOCKCODE
  171. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  172. // htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
  173. renderer := &CustomRender{
  174. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  175. urlPrefix: urlPrefix,
  176. }
  177. // set up the parser
  178. extensions := 0
  179. extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
  180. extensions |= blackfriday.EXTENSION_TABLES
  181. extensions |= blackfriday.EXTENSION_FENCED_CODE
  182. extensions |= blackfriday.EXTENSION_AUTOLINK
  183. extensions |= blackfriday.EXTENSION_STRIKETHROUGH
  184. extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
  185. extensions |= blackfriday.EXTENSION_SPACE_HEADERS
  186. extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  187. body = blackfriday.Markdown(body, renderer, extensions)
  188. return body
  189. }
  190. func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
  191. body := RenderSpecialLink(rawBytes, urlPrefix)
  192. body = RenderRawMarkdown(body, urlPrefix)
  193. body = Sanitizer.SanitizeBytes(body)
  194. return body
  195. }
  196. func RenderMarkdownString(raw, urlPrefix string) string {
  197. return string(RenderMarkdown([]byte(raw), urlPrefix))
  198. }