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.

195 lines
5.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
  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/gogits/gfm"
  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. gfm.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. var (
  78. MentionPattern = regexp.MustCompile(`@[0-9a-zA-Z_]{1,}`)
  79. commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`)
  80. issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`)
  81. issueIndexPattern = regexp.MustCompile(`#[0-9]+`)
  82. )
  83. func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
  84. buf := bytes.NewBufferString("")
  85. inCodeBlock := false
  86. codeBlockPrefix := []byte("```")
  87. lineBreak := []byte("\n")
  88. tab := []byte("\t")
  89. lines := bytes.Split(rawBytes, lineBreak)
  90. for _, line := range lines {
  91. if bytes.HasPrefix(line, codeBlockPrefix) {
  92. inCodeBlock = !inCodeBlock
  93. }
  94. if !inCodeBlock && !bytes.HasPrefix(line, tab) {
  95. ms := MentionPattern.FindAll(line, -1)
  96. for _, m := range ms {
  97. line = bytes.Replace(line, m,
  98. []byte(fmt.Sprintf(`<a href="%s/user/%s">%s</a>`, setting.AppSubUrl, m[1:], m)), -1)
  99. }
  100. }
  101. buf.Write(line)
  102. buf.Write(lineBreak)
  103. }
  104. rawBytes = buf.Bytes()
  105. ms := commitPattern.FindAll(rawBytes, -1)
  106. for _, m := range ms {
  107. m = bytes.TrimSpace(m)
  108. i := strings.Index(string(m), "commit/")
  109. j := strings.Index(string(m), "#")
  110. if j == -1 {
  111. j = len(m)
  112. }
  113. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  114. ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1)
  115. }
  116. ms = issueFullPattern.FindAll(rawBytes, -1)
  117. for _, m := range ms {
  118. m = bytes.TrimSpace(m)
  119. i := strings.Index(string(m), "issues/")
  120. j := strings.Index(string(m), "#")
  121. if j == -1 {
  122. j = len(m)
  123. }
  124. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  125. ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
  126. }
  127. ms = issueIndexPattern.FindAll(rawBytes, -1)
  128. for _, m := range ms {
  129. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  130. `<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1)
  131. }
  132. return rawBytes
  133. }
  134. func RenderRawMarkdown(body []byte, urlPrefix string) []byte {
  135. htmlFlags := 0
  136. // htmlFlags |= gfm.HTML_USE_XHTML
  137. // htmlFlags |= gfm.HTML_USE_SMARTYPANTS
  138. // htmlFlags |= gfm.HTML_SMARTYPANTS_FRACTIONS
  139. // htmlFlags |= gfm.HTML_SMARTYPANTS_LATEX_DASHES
  140. // htmlFlags |= gfm.HTML_SKIP_HTML
  141. htmlFlags |= gfm.HTML_SKIP_STYLE
  142. htmlFlags |= gfm.HTML_SKIP_SCRIPT
  143. htmlFlags |= gfm.HTML_GITHUB_BLOCKCODE
  144. htmlFlags |= gfm.HTML_OMIT_CONTENTS
  145. // htmlFlags |= gfm.HTML_COMPLETE_PAGE
  146. renderer := &CustomRender{
  147. Renderer: gfm.HtmlRenderer(htmlFlags, "", ""),
  148. urlPrefix: urlPrefix,
  149. }
  150. // set up the parser
  151. extensions := 0
  152. extensions |= gfm.EXTENSION_NO_INTRA_EMPHASIS
  153. extensions |= gfm.EXTENSION_TABLES
  154. extensions |= gfm.EXTENSION_FENCED_CODE
  155. extensions |= gfm.EXTENSION_AUTOLINK
  156. extensions |= gfm.EXTENSION_STRIKETHROUGH
  157. extensions |= gfm.EXTENSION_HARD_LINE_BREAK
  158. extensions |= gfm.EXTENSION_SPACE_HEADERS
  159. extensions |= gfm.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  160. body = gfm.Markdown(body, renderer, extensions)
  161. return body
  162. }
  163. func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
  164. body := RenderSpecialLink(rawBytes, urlPrefix)
  165. body = RenderRawMarkdown(body, urlPrefix)
  166. return body
  167. }
  168. func RenderMarkdownString(raw, urlPrefix string) string {
  169. return string(RenderMarkdown([]byte(raw), urlPrefix))
  170. }