mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-09 12:28:49 +03:00
Merge pull request #623 from xlab/f/docflags
markdown: Implement .DocFlags and tests
This commit is contained in:
commit
367397dbd6
6 changed files with 114 additions and 23 deletions
|
@ -32,6 +32,18 @@ func TestMarkdown(t *testing.T) {
|
||||||
StaticDir: DefaultStaticDir,
|
StaticDir: DefaultStaticDir,
|
||||||
StaticFiles: make(map[string]string),
|
StaticFiles: make(map[string]string),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
||||||
|
PathScope: "/docflags",
|
||||||
|
Extensions: []string{".md"},
|
||||||
|
Styles: []string{},
|
||||||
|
Scripts: []string{},
|
||||||
|
Templates: map[string]string{
|
||||||
|
DefaultTemplate: "testdata/docflags/template.txt",
|
||||||
|
},
|
||||||
|
StaticDir: DefaultStaticDir,
|
||||||
|
StaticFiles: make(map[string]string),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
||||||
PathScope: "/log",
|
PathScope: "/log",
|
||||||
|
@ -114,6 +126,26 @@ Welcome to A Caddy website!
|
||||||
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req, err = http.NewRequest("GET", "/docflags/test.md", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Could not create HTTP request: %v", err)
|
||||||
|
}
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
|
||||||
|
md.ServeHTTP(rec, req)
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code)
|
||||||
|
}
|
||||||
|
respBody = rec.Body.String()
|
||||||
|
expectedBody = `Doc.var_string hello
|
||||||
|
Doc.var_bool <no value>
|
||||||
|
DocFlags.var_string <no value>
|
||||||
|
DocFlags.var_bool true`
|
||||||
|
|
||||||
|
if !equalStrings(respBody, expectedBody) {
|
||||||
|
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
||||||
|
}
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/log/test.md", nil)
|
req, err = http.NewRequest("GET", "/log/test.md", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Could not create HTTP request: %v", err)
|
t.Fatalf("Could not create HTTP request: %v", err)
|
||||||
|
@ -190,6 +222,7 @@ Welcome to title!
|
||||||
|
|
||||||
expectedLinks := []string{
|
expectedLinks := []string{
|
||||||
"/blog/test.md",
|
"/blog/test.md",
|
||||||
|
"/docflags/test.md",
|
||||||
"/log/test.md",
|
"/log/test.md",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,9 @@ type Metadata struct {
|
||||||
|
|
||||||
// Variables to be used with Template
|
// Variables to be used with Template
|
||||||
Variables map[string]string
|
Variables map[string]string
|
||||||
|
|
||||||
|
// Flags to be used with Template
|
||||||
|
Flags map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// load loads parsed values in parsedMap into Metadata
|
// load loads parsed values in parsedMap into Metadata
|
||||||
|
@ -40,8 +43,11 @@ func (m *Metadata) load(parsedMap map[string]interface{}) {
|
||||||
}
|
}
|
||||||
// store everything as a variable
|
// store everything as a variable
|
||||||
for key, val := range parsedMap {
|
for key, val := range parsedMap {
|
||||||
if v, ok := val.(string); ok {
|
switch v := val.(type) {
|
||||||
|
case string:
|
||||||
m.Variables[key] = v
|
m.Variables[key] = v
|
||||||
|
case bool:
|
||||||
|
m.Flags[key] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -219,11 +225,18 @@ func findParser(b []byte) MetadataParser {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newMetadata() Metadata {
|
||||||
|
return Metadata{
|
||||||
|
Variables: make(map[string]string),
|
||||||
|
Flags: make(map[string]bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// parsers returns all available parsers
|
// parsers returns all available parsers
|
||||||
func parsers() []MetadataParser {
|
func parsers() []MetadataParser {
|
||||||
return []MetadataParser{
|
return []MetadataParser{
|
||||||
&JSONMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
&JSONMetadataParser{metadata: newMetadata()},
|
||||||
&TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
&TOMLMetadataParser{metadata: newMetadata()},
|
||||||
&YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
&YAMLMetadataParser{metadata: newMetadata()},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,15 @@ var TOML = [5]string{`
|
||||||
title = "A title"
|
title = "A title"
|
||||||
template = "default"
|
template = "default"
|
||||||
name = "value"
|
name = "value"
|
||||||
|
positive = true
|
||||||
|
negative = false
|
||||||
`,
|
`,
|
||||||
`+++
|
`+++
|
||||||
title = "A title"
|
title = "A title"
|
||||||
template = "default"
|
template = "default"
|
||||||
name = "value"
|
name = "value"
|
||||||
|
positive = true
|
||||||
|
negative = false
|
||||||
+++
|
+++
|
||||||
Page content
|
Page content
|
||||||
`,
|
`,
|
||||||
|
@ -30,12 +34,16 @@ Page content
|
||||||
title = "A title"
|
title = "A title"
|
||||||
template = "default"
|
template = "default"
|
||||||
name = "value"
|
name = "value"
|
||||||
|
positive = true
|
||||||
|
negative = false
|
||||||
`,
|
`,
|
||||||
`title = "A title" template = "default" [variables] name = "value"`,
|
`title = "A title" template = "default" [variables] name = "value"`,
|
||||||
`+++
|
`+++
|
||||||
title = "A title"
|
title = "A title"
|
||||||
template = "default"
|
template = "default"
|
||||||
name = "value"
|
name = "value"
|
||||||
|
positive = true
|
||||||
|
negative = false
|
||||||
+++
|
+++
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
@ -44,11 +52,15 @@ var YAML = [5]string{`
|
||||||
title : A title
|
title : A title
|
||||||
template : default
|
template : default
|
||||||
name : value
|
name : value
|
||||||
|
positive : true
|
||||||
|
negative : false
|
||||||
`,
|
`,
|
||||||
`---
|
`---
|
||||||
title : A title
|
title : A title
|
||||||
template : default
|
template : default
|
||||||
name : value
|
name : value
|
||||||
|
positive : true
|
||||||
|
negative : false
|
||||||
---
|
---
|
||||||
Page content
|
Page content
|
||||||
`,
|
`,
|
||||||
|
@ -57,11 +69,13 @@ title : A title
|
||||||
template : default
|
template : default
|
||||||
name : value
|
name : value
|
||||||
`,
|
`,
|
||||||
`title : A title template : default variables : name : value`,
|
`title : A title template : default variables : name : value : positive : true : negative : false`,
|
||||||
`---
|
`---
|
||||||
title : A title
|
title : A title
|
||||||
template : default
|
template : default
|
||||||
name : value
|
name : value
|
||||||
|
positive : true
|
||||||
|
negative : false
|
||||||
---
|
---
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
@ -69,12 +83,16 @@ name : value
|
||||||
var JSON = [5]string{`
|
var JSON = [5]string{`
|
||||||
"title" : "A title",
|
"title" : "A title",
|
||||||
"template" : "default",
|
"template" : "default",
|
||||||
"name" : "value"
|
"name" : "value",
|
||||||
|
"positive" : true,
|
||||||
|
"negative" : false
|
||||||
`,
|
`,
|
||||||
`{
|
`{
|
||||||
"title" : "A title",
|
"title" : "A title",
|
||||||
"template" : "default",
|
"template" : "default",
|
||||||
"name" : "value"
|
"name" : "value",
|
||||||
|
"positive" : true,
|
||||||
|
"negative" : false
|
||||||
}
|
}
|
||||||
Page content
|
Page content
|
||||||
`,
|
`,
|
||||||
|
@ -82,19 +100,25 @@ Page content
|
||||||
{
|
{
|
||||||
"title" : "A title",
|
"title" : "A title",
|
||||||
"template" : "default",
|
"template" : "default",
|
||||||
"name" : "value"
|
"name" : "value",
|
||||||
|
"positive" : true,
|
||||||
|
"negative" : false
|
||||||
`,
|
`,
|
||||||
`
|
`
|
||||||
{
|
{
|
||||||
"title" :: "A title",
|
"title" :: "A title",
|
||||||
"template" : "default",
|
"template" : "default",
|
||||||
"name" : "value"
|
"name" : "value",
|
||||||
|
"positive" : true,
|
||||||
|
"negative" : false
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
`{
|
`{
|
||||||
"title" : "A title",
|
"title" : "A title",
|
||||||
"template" : "default",
|
"template" : "default",
|
||||||
"name" : "value"
|
"name" : "value",
|
||||||
|
"positive" : true,
|
||||||
|
"negative" : false
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
@ -108,6 +132,10 @@ func TestParsers(t *testing.T) {
|
||||||
"title": "A title",
|
"title": "A title",
|
||||||
"template": "default",
|
"template": "default",
|
||||||
},
|
},
|
||||||
|
Flags: map[string]bool{
|
||||||
|
"positive": true,
|
||||||
|
"negative": false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
compare := func(m Metadata) bool {
|
compare := func(m Metadata) bool {
|
||||||
if m.Title != expected.Title {
|
if m.Title != expected.Title {
|
||||||
|
@ -121,7 +149,14 @@ func TestParsers(t *testing.T) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return len(m.Variables) == len(expected.Variables)
|
for k, v := range m.Flags {
|
||||||
|
if v != expected.Flags[k] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
varLenOK := len(m.Variables) == len(expected.Variables)
|
||||||
|
flagLenOK := len(m.Flags) == len(expected.Flags)
|
||||||
|
return varLenOK && flagLenOK
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []struct {
|
data := []struct {
|
||||||
|
@ -129,9 +164,9 @@ func TestParsers(t *testing.T) {
|
||||||
testData [5]string
|
testData [5]string
|
||||||
name string
|
name string
|
||||||
}{
|
}{
|
||||||
{&JSONMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, JSON, "json"},
|
{&JSONMetadataParser{metadata: newMetadata()}, JSON, "json"},
|
||||||
{&YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, YAML, "yaml"},
|
{&YAMLMetadataParser{metadata: newMetadata()}, YAML, "yaml"},
|
||||||
{&TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, TOML, "toml"},
|
{&TOMLMetadataParser{metadata: newMetadata()}, TOML, "toml"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range data {
|
for _, v := range data {
|
||||||
|
@ -207,9 +242,9 @@ Mycket olika byggnader har man i de nordiska rikena: pyramidformiga, kilformiga,
|
||||||
testData string
|
testData string
|
||||||
name string
|
name string
|
||||||
}{
|
}{
|
||||||
{&JSONMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, JSON, "json"},
|
{&JSONMetadataParser{metadata: newMetadata()}, JSON, "json"},
|
||||||
{&YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, YAML, "yaml"},
|
{&YAMLMetadataParser{metadata: newMetadata()}, YAML, "yaml"},
|
||||||
{&TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, TOML, "toml"},
|
{&TOMLMetadataParser{metadata: newMetadata()}, TOML, "toml"},
|
||||||
}
|
}
|
||||||
for _, v := range data {
|
for _, v := range data {
|
||||||
// metadata without identifiers
|
// metadata without identifiers
|
||||||
|
|
|
@ -24,13 +24,14 @@ const (
|
||||||
type Data struct {
|
type Data struct {
|
||||||
middleware.Context
|
middleware.Context
|
||||||
Doc map[string]string
|
Doc map[string]string
|
||||||
|
DocFlags map[string]bool
|
||||||
Links []PageLink
|
Links []PageLink
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process processes the contents of a page in b. It parses the metadata
|
// Process processes the contents of a page in b. It parses the metadata
|
||||||
// (if any) and uses the template (if found).
|
// (if any) and uses the template (if found).
|
||||||
func (md Markdown) Process(c *Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
|
func (md Markdown) Process(c *Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
|
||||||
var metadata = Metadata{Variables: make(map[string]string)}
|
var metadata = newMetadata()
|
||||||
var markdown []byte
|
var markdown []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -102,6 +103,7 @@ func (md Markdown) processTemplate(c *Config, requestPath string, tmpl []byte, m
|
||||||
mdData := Data{
|
mdData := Data{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Doc: metadata.Variables,
|
Doc: metadata.Variables,
|
||||||
|
DocFlags: metadata.Flags,
|
||||||
Links: c.Links,
|
Links: c.Links,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
middleware/markdown/testdata/docflags/template.txt
vendored
Normal file
4
middleware/markdown/testdata/docflags/template.txt
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
Doc.var_string {{.Doc.var_string}}
|
||||||
|
Doc.var_bool {{.Doc.var_bool}}
|
||||||
|
DocFlags.var_string {{.DocFlags.var_string}}
|
||||||
|
DocFlags.var_bool {{.DocFlags.var_bool}}
|
4
middleware/markdown/testdata/docflags/test.md
vendored
Normal file
4
middleware/markdown/testdata/docflags/test.md
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
var_string: hello
|
||||||
|
var_bool: true
|
||||||
|
---
|
Loading…
Reference in a new issue