summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-04-21 15:01:08 +0800
committerKim "BKC" Carlbäcker <kim.carlbacker@gmail.com>2017-04-21 09:01:08 +0200
commit52627032bc55bd73694bea9e6e17df575b51664c (patch)
tree2324046a3936f5b5e68bf07ed7adaf083411b329 /modules
parentf0db3da713eb9440923ddf376349e72b65f129ef (diff)
downloadgitea-52627032bc55bd73694bea9e6e17df575b51664c.tar.gz
gitea-52627032bc55bd73694bea9e6e17df575b51664c.zip
Add markup package to prepare for org markup format (#1493)
Diffstat (limited to 'modules')
-rw-r--r--modules/markdown/markdown.go41
-rw-r--r--modules/markdown/markdown_test.go29
-rw-r--r--modules/markup/markup.go72
-rw-r--r--modules/markup/markup_test.go36
4 files changed, 141 insertions, 37 deletions
diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go
index 813fabe178..183804a9ee 100644
--- a/modules/markdown/markdown.go
+++ b/modules/markdown/markdown.go
@@ -19,6 +19,7 @@ import (
"golang.org/x/net/html"
"code.gitea.io/gitea/modules/base"
+ "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
)
@@ -40,18 +41,6 @@ func IsMarkdownFile(name string) bool {
return false
}
-// IsReadmeFile reports whether name looks like a README file
-// based on its name.
-func IsReadmeFile(name string) bool {
- name = strings.ToLower(name)
- if len(name) < 6 {
- return false
- } else if len(name) == 6 {
- return name == "readme"
- }
- return name[:7] == "readme."
-}
-
var (
// MentionPattern matches string that mentions someone, e.g. @Unknwon
MentionPattern = regexp.MustCompile(`(\s|^|\W)@[0-9a-zA-Z-_\.]+`)
@@ -707,3 +696,31 @@ func RenderString(raw, urlPrefix string, metas map[string]string) string {
func RenderWiki(rawBytes []byte, urlPrefix string, metas map[string]string) string {
return string(render(rawBytes, urlPrefix, metas, true))
}
+
+var (
+ // MarkupName describes markup's name
+ MarkupName = "markdown"
+)
+
+func init() {
+ markup.RegisterParser(Parser{})
+}
+
+// Parser implements markup.Parser
+type Parser struct {
+}
+
+// Name implements markup.Parser
+func (Parser) Name() string {
+ return MarkupName
+}
+
+// Extensions implements markup.Parser
+func (Parser) Extensions() []string {
+ return setting.Markdown.FileExtensions
+}
+
+// Render implements markup.Parser
+func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
+ return render(rawBytes, urlPrefix, metas, isWiki)
+}
diff --git a/modules/markdown/markdown_test.go b/modules/markdown/markdown_test.go
index 88d289aa0e..8364146573 100644
--- a/modules/markdown/markdown_test.go
+++ b/modules/markdown/markdown_test.go
@@ -1,3 +1,7 @@
+// Copyright 2017 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
package markdown_test
import (
@@ -586,31 +590,6 @@ func TestMisc_IsMarkdownFile(t *testing.T) {
}
}
-func TestMisc_IsReadmeFile(t *testing.T) {
- trueTestCases := []string{
- "readme",
- "README",
- "readME.mdown",
- "README.md",
- }
- falseTestCases := []string{
- "test.md",
- "wow.MARKDOWN",
- "LOL.mDoWn",
- "test",
- "abcdefg",
- "abcdefghijklmnopqrstuvwxyz",
- "test.md.test",
- }
-
- for _, testCase := range trueTestCases {
- assert.True(t, IsReadmeFile(testCase))
- }
- for _, testCase := range falseTestCases {
- assert.False(t, IsReadmeFile(testCase))
- }
-}
-
func TestMisc_IsSameDomain(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL
diff --git a/modules/markup/markup.go b/modules/markup/markup.go
new file mode 100644
index 0000000000..0847fcfb0d
--- /dev/null
+++ b/modules/markup/markup.go
@@ -0,0 +1,72 @@
+// Copyright 2017 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package markup
+
+import (
+ "path/filepath"
+ "strings"
+)
+
+// Parser defines an interface for parsering markup file to HTML
+type Parser interface {
+ Name() string // markup format name
+ Extensions() []string
+ Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte
+}
+
+var (
+ parsers = make(map[string]Parser)
+)
+
+// RegisterParser registers a new markup file parser
+func RegisterParser(parser Parser) {
+ for _, ext := range parser.Extensions() {
+ parsers[strings.ToLower(ext)] = parser
+ }
+}
+
+// Render renders markup file to HTML with all specific handling stuff.
+func Render(filename string, rawBytes []byte, urlPrefix string, metas map[string]string) []byte {
+ return render(filename, rawBytes, urlPrefix, metas, false)
+}
+
+func render(filename string, rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
+ extension := strings.ToLower(filepath.Ext(filename))
+ if parser, ok := parsers[extension]; ok {
+ return parser.Render(rawBytes, urlPrefix, metas, isWiki)
+ }
+ return nil
+}
+
+// RenderString renders Markdown to HTML with special links and returns string type.
+func RenderString(filename string, raw, urlPrefix string, metas map[string]string) string {
+ return string(render(filename, []byte(raw), urlPrefix, metas, false))
+}
+
+// RenderWiki renders markdown wiki page to HTML and return HTML string
+func RenderWiki(filename string, rawBytes []byte, urlPrefix string, metas map[string]string) string {
+ return string(render(filename, rawBytes, urlPrefix, metas, true))
+}
+
+// Type returns if markup format via the filename
+func Type(filename string) string {
+ extension := strings.ToLower(filepath.Ext(filename))
+ if parser, ok := parsers[extension]; ok {
+ return parser.Name()
+ }
+ return ""
+}
+
+// IsReadmeFile reports whether name looks like a README file
+// based on its name.
+func IsReadmeFile(name string) bool {
+ name = strings.ToLower(name)
+ if len(name) < 6 {
+ return false
+ } else if len(name) == 6 {
+ return name == "readme"
+ }
+ return name[:7] == "readme."
+}
diff --git a/modules/markup/markup_test.go b/modules/markup/markup_test.go
new file mode 100644
index 0000000000..92caa8ff96
--- /dev/null
+++ b/modules/markup/markup_test.go
@@ -0,0 +1,36 @@
+// Copyright 2017 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package markup
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestMisc_IsReadmeFile(t *testing.T) {
+ trueTestCases := []string{
+ "readme",
+ "README",
+ "readME.mdown",
+ "README.md",
+ }
+ falseTestCases := []string{
+ "test.md",
+ "wow.MARKDOWN",
+ "LOL.mDoWn",
+ "test",
+ "abcdefg",
+ "abcdefghijklmnopqrstuvwxyz",
+ "test.md.test",
+ }
+
+ for _, testCase := range trueTestCases {
+ assert.True(t, IsReadmeFile(testCase))
+ }
+ for _, testCase := range falseTestCases {
+ assert.False(t, IsReadmeFile(testCase))
+ }
+}