You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown.go 7.9 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. "io"
  9. "net/http"
  10. "path"
  11. "path/filepath"
  12. "regexp"
  13. "strings"
  14. "github.com/russross/blackfriday"
  15. "github.com/gogits/gogs/modules/setting"
  16. "golang.org/x/net/html"
  17. )
  18. func isletter(c byte) bool {
  19. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
  20. }
  21. func isalnum(c byte) bool {
  22. return (c >= '0' && c <= '9') || isletter(c)
  23. }
  24. var validLinks = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")}
  25. func isLink(link []byte) bool {
  26. for _, prefix := range validLinks {
  27. if len(link) > len(prefix) && bytes.Equal(bytes.ToLower(link[:len(prefix)]), prefix) && isalnum(link[len(prefix)]) {
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. func IsMarkdownFile(name string) bool {
  34. name = strings.ToLower(name)
  35. switch filepath.Ext(name) {
  36. case ".md", ".markdown", ".mdown", ".mkd":
  37. return true
  38. }
  39. return false
  40. }
  41. func IsTextFile(data []byte) (string, bool) {
  42. contentType := http.DetectContentType(data)
  43. if strings.Index(contentType, "text/") != -1 {
  44. return contentType, true
  45. }
  46. return contentType, false
  47. }
  48. func IsImageFile(data []byte) (string, bool) {
  49. contentType := http.DetectContentType(data)
  50. if strings.Index(contentType, "image/") != -1 {
  51. return contentType, true
  52. }
  53. return contentType, false
  54. }
  55. // IsReadmeFile returns true if given file name suppose to be a README file.
  56. func IsReadmeFile(name string) bool {
  57. name = strings.ToLower(name)
  58. if len(name) < 6 {
  59. return false
  60. } else if len(name) == 6 {
  61. if name == "readme" {
  62. return true
  63. }
  64. return false
  65. }
  66. if name[:7] == "readme." {
  67. return true
  68. }
  69. return false
  70. }
  71. type CustomRender struct {
  72. blackfriday.Renderer
  73. urlPrefix string
  74. }
  75. func (options *CustomRender) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  76. if len(link) > 0 && !isLink(link) {
  77. if link[0] == '#' {
  78. // link = append([]byte(options.urlPrefix), link...)
  79. } else {
  80. link = []byte(path.Join(options.urlPrefix, string(link)))
  81. }
  82. }
  83. options.Renderer.Link(out, link, title, content)
  84. }
  85. func (options *CustomRender) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  86. if len(link) > 0 && !isLink(link) {
  87. link = []byte(path.Join(strings.Replace(options.urlPrefix, "/src/", "/raw/", 1), string(link)))
  88. }
  89. options.Renderer.Image(out, link, title, alt)
  90. }
  91. var (
  92. MentionPattern = regexp.MustCompile(`(\s|^)@[0-9a-zA-Z_]+`)
  93. commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`)
  94. issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`)
  95. issueIndexPattern = regexp.MustCompile(`( |^)#[0-9]+`)
  96. sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{40}\b`)
  97. )
  98. func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
  99. buf := bytes.NewBufferString("")
  100. inCodeBlock := false
  101. codeBlockPrefix := []byte("```")
  102. lineBreak := []byte("\n")
  103. tab := []byte("\t")
  104. lines := bytes.Split(rawBytes, lineBreak)
  105. for _, line := range lines {
  106. if bytes.HasPrefix(line, codeBlockPrefix) {
  107. inCodeBlock = !inCodeBlock
  108. }
  109. if !inCodeBlock && !bytes.HasPrefix(line, tab) {
  110. ms := MentionPattern.FindAll(line, -1)
  111. for _, m := range ms {
  112. m = bytes.TrimSpace(m)
  113. line = bytes.Replace(line, m,
  114. []byte(fmt.Sprintf(`<a href="%s/%s">%s</a>`, setting.AppSubUrl, m[1:], m)), -1)
  115. }
  116. }
  117. buf.Write(line)
  118. buf.Write(lineBreak)
  119. }
  120. rawBytes = buf.Bytes()
  121. ms := commitPattern.FindAll(rawBytes, -1)
  122. for _, m := range ms {
  123. m = bytes.TrimSpace(m)
  124. i := strings.Index(string(m), "commit/")
  125. j := strings.Index(string(m), "#")
  126. if j == -1 {
  127. j = len(m)
  128. }
  129. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  130. ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1)
  131. }
  132. ms = issueFullPattern.FindAll(rawBytes, -1)
  133. for _, m := range ms {
  134. m = bytes.TrimSpace(m)
  135. i := strings.Index(string(m), "issues/")
  136. j := strings.Index(string(m), "#")
  137. if j == -1 {
  138. j = len(m)
  139. }
  140. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  141. ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
  142. }
  143. rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix)
  144. rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix)
  145. return rawBytes
  146. }
  147. func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
  148. ms := sha1CurrentPattern.FindAll(rawBytes, -1)
  149. for _, m := range ms {
  150. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  151. `<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, ShortSha(string(m)))), -1)
  152. }
  153. return rawBytes
  154. }
  155. func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string) []byte {
  156. ms := issueIndexPattern.FindAll(rawBytes, -1)
  157. for _, m := range ms {
  158. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(`<a href="%s/issues/%s">%s</a>`,
  159. urlPrefix, strings.TrimPrefix(string(m[1:]), "#"), m)), -1)
  160. }
  161. return rawBytes
  162. }
  163. func RenderRawMarkdown(body []byte, urlPrefix string) []byte {
  164. htmlFlags := 0
  165. // htmlFlags |= blackfriday.HTML_USE_XHTML
  166. // htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
  167. // htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
  168. // htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
  169. // htmlFlags |= blackfriday.HTML_SKIP_HTML
  170. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  171. // htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
  172. // htmlFlags |= blackfriday.HTML_GITHUB_BLOCKCODE
  173. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  174. // htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
  175. renderer := &CustomRender{
  176. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  177. urlPrefix: urlPrefix,
  178. }
  179. // set up the parser
  180. extensions := 0
  181. extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
  182. extensions |= blackfriday.EXTENSION_TABLES
  183. extensions |= blackfriday.EXTENSION_FENCED_CODE
  184. extensions |= blackfriday.EXTENSION_AUTOLINK
  185. extensions |= blackfriday.EXTENSION_STRIKETHROUGH
  186. extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
  187. extensions |= blackfriday.EXTENSION_SPACE_HEADERS
  188. extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  189. body = blackfriday.Markdown(body, renderer, extensions)
  190. return body
  191. }
  192. func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
  193. result := RenderRawMarkdown(rawBytes, urlPrefix)
  194. result = PostProcessMarkdown(result, urlPrefix)
  195. result = Sanitizer.SanitizeBytes(result)
  196. return result
  197. }
  198. func RenderMarkdownString(raw, urlPrefix string) string {
  199. return string(RenderMarkdown([]byte(raw), urlPrefix))
  200. }
  201. func PostProcessMarkdown(rawHtml []byte, urlPrefix string) []byte {
  202. var buf bytes.Buffer
  203. tokenizer := html.NewTokenizer(bytes.NewReader(rawHtml))
  204. for html.ErrorToken != tokenizer.Next() {
  205. // A parse error has occurred, so return the original input unmodified
  206. return rawHtml
  207. token := tokenizer.Token()
  208. switch token.Type {
  209. case html.TextToken:
  210. text := []byte(token.String())
  211. text = RenderSpecialLink(text, urlPrefix)
  212. buf.Write(text)
  213. case html.StartTagToken:
  214. buf.WriteString(token.String())
  215. tagName := token.Data
  216. // If this is an excluded tag, we skip processing all output until a close tag is encountered
  217. if strings.EqualFold("a", tagName) || strings.EqualFold("code", tagName) || strings.EqualFold("pre", tagName) {
  218. for html.ErrorToken != tokenizer.Next() {
  219. token = tokenizer.Token()
  220. // Copy the token to the output verbatim
  221. buf.WriteString(token.String())
  222. // If this is the close tag, we are done
  223. if html.EndTagToken == token.Type && strings.EqualFold(tagName, token.Data) { break }
  224. }
  225. }
  226. default:
  227. buf.WriteString(token.String())
  228. }
  229. }
  230. if io.EOF == tokenizer.Err() {
  231. return buf.Bytes()
  232. }
  233. // If we are not at the end of the input, then some other parsing error has occurred, so return
  234. // the input verbatim.
  235. return rawHtml
  236. }