mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Add markup package to prepare for org markup format (#1493)
This commit is contained in:
		
				
					committed by
					
						
						Kim "BKC" Carlbäcker
					
				
			
			
				
	
			
			
			
						parent
						
							f0db3da713
						
					
				
				
					commit
					52627032bc
				
			
							
								
								
									
										72
									
								
								modules/markup/markup.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								modules/markup/markup.go
									
									
									
									
									
										Normal file
									
								
							@@ -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."
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								modules/markup/markup_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								modules/markup/markup_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -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))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user