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.

168 lines
4.4 KiB

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":
  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. ms := MentionPattern.FindAll(rawBytes, -1)
  84. for _, m := range ms {
  85. rawBytes = bytes.Replace(rawBytes, m,
  86. []byte(fmt.Sprintf(`<a href="/user/%s">%s</a>`, m[1:], m)), -1)
  87. }
  88. ms = commitPattern.FindAll(rawBytes, -1)
  89. for _, m := range ms {
  90. m = bytes.TrimSpace(m)
  91. i := strings.Index(string(m), "commit/")
  92. j := strings.Index(string(m), "#")
  93. if j == -1 {
  94. j = len(m)
  95. }
  96. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  97. ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1)
  98. }
  99. ms = issueFullPattern.FindAll(rawBytes, -1)
  100. for _, m := range ms {
  101. m = bytes.TrimSpace(m)
  102. i := strings.Index(string(m), "issues/")
  103. j := strings.Index(string(m), "#")
  104. if j == -1 {
  105. j = len(m)
  106. }
  107. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  108. ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
  109. }
  110. ms = issueIndexPattern.FindAll(rawBytes, -1)
  111. for _, m := range ms {
  112. rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
  113. `<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1)
  114. }
  115. return rawBytes
  116. }
  117. func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
  118. body := RenderSpecialLink(rawBytes, urlPrefix)
  119. // fmt.Println(string(body))
  120. htmlFlags := 0
  121. // htmlFlags |= gfm.HTML_USE_XHTML
  122. // htmlFlags |= gfm.HTML_USE_SMARTYPANTS
  123. // htmlFlags |= gfm.HTML_SMARTYPANTS_FRACTIONS
  124. // htmlFlags |= gfm.HTML_SMARTYPANTS_LATEX_DASHES
  125. // htmlFlags |= gfm.HTML_SKIP_HTML
  126. htmlFlags |= gfm.HTML_SKIP_STYLE
  127. htmlFlags |= gfm.HTML_SKIP_SCRIPT
  128. htmlFlags |= gfm.HTML_GITHUB_BLOCKCODE
  129. htmlFlags |= gfm.HTML_OMIT_CONTENTS
  130. // htmlFlags |= gfm.HTML_COMPLETE_PAGE
  131. renderer := &CustomRender{
  132. Renderer: gfm.HtmlRenderer(htmlFlags, "", ""),
  133. urlPrefix: urlPrefix,
  134. }
  135. // set up the parser
  136. extensions := 0
  137. extensions |= gfm.EXTENSION_NO_INTRA_EMPHASIS
  138. extensions |= gfm.EXTENSION_TABLES
  139. extensions |= gfm.EXTENSION_FENCED_CODE
  140. extensions |= gfm.EXTENSION_AUTOLINK
  141. extensions |= gfm.EXTENSION_STRIKETHROUGH
  142. extensions |= gfm.EXTENSION_HARD_LINE_BREAK
  143. extensions |= gfm.EXTENSION_SPACE_HEADERS
  144. extensions |= gfm.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
  145. body = gfm.Markdown(body, renderer, extensions)
  146. // fmt.Println(string(body))
  147. return body
  148. }