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.

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