Commit c7674e20 authored by Maxim Kupriianov's avatar Maxim Kupriianov

Implement .DocFlags directive and tests. It holds all the boolean-typed front matter values.

parent c12847e5
...@@ -32,6 +32,18 @@ func TestMarkdown(t *testing.T) { ...@@ -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! ...@@ -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! ...@@ -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 { ...@@ -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{}) { ...@@ -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 { ...@@ -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{` ...@@ -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 ...@@ -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{` ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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, ...@@ -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
......
...@@ -23,14 +23,15 @@ const ( ...@@ -23,14 +23,15 @@ const (
// Data represents a markdown document. // Data represents a markdown document.
type Data struct { type Data struct {
middleware.Context middleware.Context
Doc map[string]string Doc map[string]string
Links []PageLink DocFlags map[string]bool
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
...@@ -100,9 +101,10 @@ func (md Markdown) processTemplate(c *Config, requestPath string, tmpl []byte, m ...@@ -100,9 +101,10 @@ func (md Markdown) processTemplate(c *Config, requestPath string, tmpl []byte, m
return nil, err return nil, err
} }
mdData := Data{ mdData := Data{
Context: ctx, Context: ctx,
Doc: metadata.Variables, Doc: metadata.Variables,
Links: c.Links, DocFlags: metadata.Flags,
Links: c.Links,
} }
c.RLock() c.RLock()
......
Doc.var_string {{.Doc.var_string}}
Doc.var_bool {{.Doc.var_bool}}
DocFlags.var_string {{.DocFlags.var_string}}
DocFlags.var_bool {{.DocFlags.var_bool}}
---
var_string: hello
var_bool: true
---
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment