0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2025-01-06 22:40:31 -05:00

Fix for #1276 - support integers and floats as metadata in markdown (#1278)

* Fix for #1276

* Use strconv.Format

* Use map[string]interface{} as variables

* One more file

* Always run all tests before commit

* Get rid of DocFlags
This commit is contained in:
Mateusz Gajewski 2016-12-03 07:35:33 +01:00 committed by Matt Holt
parent 9e98d6cd52
commit 17e7e6076a
4 changed files with 54 additions and 47 deletions

View file

@ -27,17 +27,13 @@ type Metadata struct {
Date time.Time Date time.Time
// Variables to be used with Template // Variables to be used with Template
Variables map[string]string Variables map[string]interface{}
// Flags to be used with Template
Flags map[string]bool
} }
// NewMetadata returns a new Metadata struct, loaded with the given map // NewMetadata returns a new Metadata struct, loaded with the given map
func NewMetadata(parsedMap map[string]interface{}) Metadata { func NewMetadata(parsedMap map[string]interface{}) Metadata {
md := Metadata{ md := Metadata{
Variables: make(map[string]string), Variables: make(map[string]interface{}),
Flags: make(map[string]bool),
} }
md.load(parsedMap) md.load(parsedMap)
@ -63,15 +59,7 @@ func (m *Metadata) load(parsedMap map[string]interface{}) {
} }
} }
// Store everything as a flag or variable m.Variables = parsedMap
for key, val := range parsedMap {
switch v := val.(type) {
case bool:
m.Flags[key] = v
case string:
m.Variables[key] = v
}
}
} }
// Parser is a an interface that must be satisfied by each parser // Parser is a an interface that must be satisfied by each parser

View file

@ -2,6 +2,7 @@ package metadata
import ( import (
"bytes" "bytes"
"fmt"
"strings" "strings"
"testing" "testing"
) )
@ -18,6 +19,8 @@ template = "default"
name = "value" name = "value"
positive = true positive = true
negative = false negative = false
number = 1410
float = 1410.07
`, `,
`+++ `+++
title = "A title" title = "A title"
@ -25,6 +28,8 @@ template = "default"
name = "value" name = "value"
positive = true positive = true
negative = false negative = false
number = 1410
float = 1410.07
+++ +++
Page content Page content
`, `,
@ -34,6 +39,8 @@ template = "default"
name = "value" name = "value"
positive = true positive = true
negative = false negative = false
number = 1410
float = 1410.07
`, `,
`title = "A title" template = "default" [variables] name = "value"`, `title = "A title" template = "default" [variables] name = "value"`,
`+++ `+++
@ -42,6 +49,8 @@ template = "default"
name = "value" name = "value"
positive = true positive = true
negative = false negative = false
number = 1410
float = 1410.07
+++ +++
`, `,
} }
@ -52,6 +61,8 @@ template : default
name : value name : value
positive : true positive : true
negative : false negative : false
number : 1410
float : 1410.07
`, `,
`--- `---
title : A title title : A title
@ -59,6 +70,8 @@ template : default
name : value name : value
positive : true positive : true
negative : false negative : false
number : 1410
float : 1410.07
--- ---
Page content Page content
`, `,
@ -66,6 +79,8 @@ negative : false
title : A title title : A title
template : default template : default
name : value name : value
number : 1410
float : 1410.07
`, `,
`title : A title template : default variables : name : value : positive : true : negative : false`, `title : A title template : default variables : name : value : positive : true : negative : false`,
`--- `---
@ -74,6 +89,8 @@ template : default
name : value name : value
positive : true positive : true
negative : false negative : false
number : 1410
float : 1410.07
--- ---
`, `,
} }
@ -83,14 +100,18 @@ var JSON = [5]string{`
"template" : "default", "template" : "default",
"name" : "value", "name" : "value",
"positive" : true, "positive" : true,
"negative" : false "negative" : false,
"number": 1410,
"float": 1410.07
`, `,
`{ `{
"title" : "A title", "title" : "A title",
"template" : "default", "template" : "default",
"name" : "value", "name" : "value",
"positive" : true, "positive" : true,
"negative" : false "negative" : false,
"number" : 1410,
"float": 1410.07
} }
Page content Page content
`, `,
@ -100,7 +121,9 @@ Page content
"template" : "default", "template" : "default",
"name" : "value", "name" : "value",
"positive" : true, "positive" : true,
"negative" : false "negative" : false,
"number" : 1410,
"float": 1410.07
`, `,
` `
{ {
@ -108,7 +131,9 @@ Page content
"template" : "default", "template" : "default",
"name" : "value", "name" : "value",
"positive" : true, "positive" : true,
"negative" : false "negative" : false,
"number" : 1410,
"float": 1410.07
} }
`, `,
`{ `{
@ -116,7 +141,9 @@ Page content
"template" : "default", "template" : "default",
"name" : "value", "name" : "value",
"positive" : true, "positive" : true,
"negative" : false "negative" : false,
"number" : 1410,
"float": 1410.07
} }
`, `,
} }
@ -125,12 +152,12 @@ func TestParsers(t *testing.T) {
expected := Metadata{ expected := Metadata{
Title: "A title", Title: "A title",
Template: "default", Template: "default",
Variables: map[string]string{ Variables: map[string]interface{}{
"name": "value", "name": "value",
"title": "A title", "title": "A title",
"template": "default", "template": "default",
}, "number": 1410,
Flags: map[string]bool{ "float": 1410.07,
"positive": true, "positive": true,
"negative": false, "negative": false,
}, },
@ -143,18 +170,13 @@ func TestParsers(t *testing.T) {
return false return false
} }
for k, v := range m.Variables { for k, v := range m.Variables {
if v != expected.Variables[k] { if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", expected.Variables[k]) {
return false
}
}
for k, v := range m.Flags {
if v != expected.Flags[k] {
return false return false
} }
} }
varLenOK := len(m.Variables) == len(expected.Variables) varLenOK := len(m.Variables) == len(expected.Variables)
flagLenOK := len(m.Flags) == len(expected.Flags) return varLenOK
return varLenOK && flagLenOK
} }
data := []struct { data := []struct {

View file

@ -130,11 +130,10 @@ func equalTemplates(i, j *template.Template) (bool, string, string) {
} }
md := Data{ md := Data{
Context: ctx, Context: ctx,
Doc: make(map[string]string), Doc: make(map[string]interface{}),
DocFlags: make(map[string]bool), Styles: []string{"style1"},
Styles: []string{"style1"}, Scripts: []string{"js1"},
Scripts: []string{"js1"},
} }
md.Doc["title"] = "some title" md.Doc["title"] = "some title"
md.Doc["body"] = "some body" md.Doc["body"] = "some body"

View file

@ -12,11 +12,10 @@ import (
// Data represents a markdown document. // Data represents a markdown document.
type Data struct { type Data struct {
httpserver.Context httpserver.Context
Doc map[string]string Doc map[string]interface{}
DocFlags map[string]bool Styles []string
Styles []string Scripts []string
Scripts []string Files []FileInfo
Files []FileInfo
} }
// Include "overrides" the embedded httpserver.Context's Include() // Include "overrides" the embedded httpserver.Context's Include()
@ -29,12 +28,11 @@ func (d Data) Include(filename string) (string, error) {
// execTemplate executes a template given a requestPath, template, and metadata // execTemplate executes a template given a requestPath, template, and metadata
func execTemplate(c *Config, mdata metadata.Metadata, files []FileInfo, ctx httpserver.Context) ([]byte, error) { func execTemplate(c *Config, mdata metadata.Metadata, files []FileInfo, ctx httpserver.Context) ([]byte, error) {
mdData := Data{ mdData := Data{
Context: ctx, Context: ctx,
Doc: mdata.Variables, Doc: mdata.Variables,
DocFlags: mdata.Flags, Styles: c.Styles,
Styles: c.Styles, Scripts: c.Scripts,
Scripts: c.Scripts, Files: files,
Files: files,
} }
b := new(bytes.Buffer) b := new(bytes.Buffer)