mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Merge pull request #573 from miekg/markdown-directive
templates: Add .Markdown directive
This commit is contained in:
commit
5b7e0361dd
2 changed files with 55 additions and 0 deletions
|
@ -9,6 +9,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/russross/blackfriday"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This file contains the context and functions available for
|
// This file contains the context and functions available for
|
||||||
|
@ -190,3 +192,17 @@ func (c Context) StripExt(path string) string {
|
||||||
func (c Context) Replace(input, find, replacement string) string {
|
func (c Context) Replace(input, find, replacement string) string {
|
||||||
return strings.Replace(input, find, replacement, -1)
|
return strings.Replace(input, find, replacement, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Markdown returns the HTML contents of the markdown contained in filename
|
||||||
|
// (relative to the site root).
|
||||||
|
func (c Context) Markdown(filename string) (string, error) {
|
||||||
|
body, err := c.Include(filename)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
renderer := blackfriday.HtmlRenderer(0, "", "")
|
||||||
|
extns := blackfriday.EXTENSION_TABLES | blackfriday.EXTENSION_FENCED_CODE | blackfriday.EXTENSION_STRIKETHROUGH | blackfriday.EXTENSION_DEFINITION_LISTS
|
||||||
|
markdown := blackfriday.Markdown([]byte(body), renderer, extns)
|
||||||
|
|
||||||
|
return string(markdown), nil
|
||||||
|
}
|
||||||
|
|
|
@ -92,6 +92,45 @@ func TestIncludeNotExisting(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarkdown(t *testing.T) {
|
||||||
|
context := getContextOrFail(t)
|
||||||
|
|
||||||
|
inputFilename := "test_file"
|
||||||
|
absInFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), inputFilename)
|
||||||
|
defer func() {
|
||||||
|
err := os.Remove(absInFilePath)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("Failed to clean test file!")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
fileContent string
|
||||||
|
expectedContent string
|
||||||
|
}{
|
||||||
|
// Test 0 - test parsing of markdown
|
||||||
|
{
|
||||||
|
fileContent: "* str1\n* str2\n",
|
||||||
|
expectedContent: "<ul>\n<li>str1</li>\n<li>str2</li>\n</ul>\n",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
testPrefix := getTestPrefix(i)
|
||||||
|
|
||||||
|
// WriteFile truncates the contentt
|
||||||
|
err := ioutil.WriteFile(absInFilePath, []byte(test.fileContent), os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(testPrefix+"Failed to create test file. Error was: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, _ := context.Markdown(inputFilename)
|
||||||
|
if content != test.expectedContent {
|
||||||
|
t.Errorf(testPrefix+"Expected content [%s] but found [%s]. Input file was: %s", test.expectedContent, content, inputFilename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCookie(t *testing.T) {
|
func TestCookie(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|
Loading…
Reference in a new issue