mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
* 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:
parent
9e98d6cd52
commit
17e7e6076a
4 changed files with 54 additions and 47 deletions
|
@ -27,17 +27,13 @@ type Metadata struct {
|
|||
Date time.Time
|
||||
|
||||
// Variables to be used with Template
|
||||
Variables map[string]string
|
||||
|
||||
// Flags to be used with Template
|
||||
Flags map[string]bool
|
||||
Variables map[string]interface{}
|
||||
}
|
||||
|
||||
// NewMetadata returns a new Metadata struct, loaded with the given map
|
||||
func NewMetadata(parsedMap map[string]interface{}) Metadata {
|
||||
md := Metadata{
|
||||
Variables: make(map[string]string),
|
||||
Flags: make(map[string]bool),
|
||||
Variables: make(map[string]interface{}),
|
||||
}
|
||||
md.load(parsedMap)
|
||||
|
||||
|
@ -63,15 +59,7 @@ func (m *Metadata) load(parsedMap map[string]interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
// Store everything as a flag or variable
|
||||
for key, val := range parsedMap {
|
||||
switch v := val.(type) {
|
||||
case bool:
|
||||
m.Flags[key] = v
|
||||
case string:
|
||||
m.Variables[key] = v
|
||||
}
|
||||
}
|
||||
m.Variables = parsedMap
|
||||
}
|
||||
|
||||
// Parser is a an interface that must be satisfied by each parser
|
||||
|
|
|
@ -2,6 +2,7 @@ package metadata
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -18,6 +19,8 @@ template = "default"
|
|||
name = "value"
|
||||
positive = true
|
||||
negative = false
|
||||
number = 1410
|
||||
float = 1410.07
|
||||
`,
|
||||
`+++
|
||||
title = "A title"
|
||||
|
@ -25,6 +28,8 @@ template = "default"
|
|||
name = "value"
|
||||
positive = true
|
||||
negative = false
|
||||
number = 1410
|
||||
float = 1410.07
|
||||
+++
|
||||
Page content
|
||||
`,
|
||||
|
@ -34,6 +39,8 @@ template = "default"
|
|||
name = "value"
|
||||
positive = true
|
||||
negative = false
|
||||
number = 1410
|
||||
float = 1410.07
|
||||
`,
|
||||
`title = "A title" template = "default" [variables] name = "value"`,
|
||||
`+++
|
||||
|
@ -42,6 +49,8 @@ template = "default"
|
|||
name = "value"
|
||||
positive = true
|
||||
negative = false
|
||||
number = 1410
|
||||
float = 1410.07
|
||||
+++
|
||||
`,
|
||||
}
|
||||
|
@ -52,6 +61,8 @@ template : default
|
|||
name : value
|
||||
positive : true
|
||||
negative : false
|
||||
number : 1410
|
||||
float : 1410.07
|
||||
`,
|
||||
`---
|
||||
title : A title
|
||||
|
@ -59,6 +70,8 @@ template : default
|
|||
name : value
|
||||
positive : true
|
||||
negative : false
|
||||
number : 1410
|
||||
float : 1410.07
|
||||
---
|
||||
Page content
|
||||
`,
|
||||
|
@ -66,6 +79,8 @@ negative : false
|
|||
title : A title
|
||||
template : default
|
||||
name : value
|
||||
number : 1410
|
||||
float : 1410.07
|
||||
`,
|
||||
`title : A title template : default variables : name : value : positive : true : negative : false`,
|
||||
`---
|
||||
|
@ -74,6 +89,8 @@ template : default
|
|||
name : value
|
||||
positive : true
|
||||
negative : false
|
||||
number : 1410
|
||||
float : 1410.07
|
||||
---
|
||||
`,
|
||||
}
|
||||
|
@ -83,14 +100,18 @@ var JSON = [5]string{`
|
|||
"template" : "default",
|
||||
"name" : "value",
|
||||
"positive" : true,
|
||||
"negative" : false
|
||||
"negative" : false,
|
||||
"number": 1410,
|
||||
"float": 1410.07
|
||||
`,
|
||||
`{
|
||||
"title" : "A title",
|
||||
"template" : "default",
|
||||
"name" : "value",
|
||||
"positive" : true,
|
||||
"negative" : false
|
||||
"negative" : false,
|
||||
"number" : 1410,
|
||||
"float": 1410.07
|
||||
}
|
||||
Page content
|
||||
`,
|
||||
|
@ -100,7 +121,9 @@ Page content
|
|||
"template" : "default",
|
||||
"name" : "value",
|
||||
"positive" : true,
|
||||
"negative" : false
|
||||
"negative" : false,
|
||||
"number" : 1410,
|
||||
"float": 1410.07
|
||||
`,
|
||||
`
|
||||
{
|
||||
|
@ -108,7 +131,9 @@ Page content
|
|||
"template" : "default",
|
||||
"name" : "value",
|
||||
"positive" : true,
|
||||
"negative" : false
|
||||
"negative" : false,
|
||||
"number" : 1410,
|
||||
"float": 1410.07
|
||||
}
|
||||
`,
|
||||
`{
|
||||
|
@ -116,7 +141,9 @@ Page content
|
|||
"template" : "default",
|
||||
"name" : "value",
|
||||
"positive" : true,
|
||||
"negative" : false
|
||||
"negative" : false,
|
||||
"number" : 1410,
|
||||
"float": 1410.07
|
||||
}
|
||||
`,
|
||||
}
|
||||
|
@ -125,12 +152,12 @@ func TestParsers(t *testing.T) {
|
|||
expected := Metadata{
|
||||
Title: "A title",
|
||||
Template: "default",
|
||||
Variables: map[string]string{
|
||||
Variables: map[string]interface{}{
|
||||
"name": "value",
|
||||
"title": "A title",
|
||||
"template": "default",
|
||||
},
|
||||
Flags: map[string]bool{
|
||||
"number": 1410,
|
||||
"float": 1410.07,
|
||||
"positive": true,
|
||||
"negative": false,
|
||||
},
|
||||
|
@ -143,18 +170,13 @@ func TestParsers(t *testing.T) {
|
|||
return false
|
||||
}
|
||||
for k, v := range m.Variables {
|
||||
if v != expected.Variables[k] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for k, v := range m.Flags {
|
||||
if v != expected.Flags[k] {
|
||||
if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", expected.Variables[k]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
varLenOK := len(m.Variables) == len(expected.Variables)
|
||||
flagLenOK := len(m.Flags) == len(expected.Flags)
|
||||
return varLenOK && flagLenOK
|
||||
return varLenOK
|
||||
}
|
||||
|
||||
data := []struct {
|
||||
|
|
|
@ -130,11 +130,10 @@ func equalTemplates(i, j *template.Template) (bool, string, string) {
|
|||
}
|
||||
|
||||
md := Data{
|
||||
Context: ctx,
|
||||
Doc: make(map[string]string),
|
||||
DocFlags: make(map[string]bool),
|
||||
Styles: []string{"style1"},
|
||||
Scripts: []string{"js1"},
|
||||
Context: ctx,
|
||||
Doc: make(map[string]interface{}),
|
||||
Styles: []string{"style1"},
|
||||
Scripts: []string{"js1"},
|
||||
}
|
||||
md.Doc["title"] = "some title"
|
||||
md.Doc["body"] = "some body"
|
||||
|
|
|
@ -12,11 +12,10 @@ import (
|
|||
// Data represents a markdown document.
|
||||
type Data struct {
|
||||
httpserver.Context
|
||||
Doc map[string]string
|
||||
DocFlags map[string]bool
|
||||
Styles []string
|
||||
Scripts []string
|
||||
Files []FileInfo
|
||||
Doc map[string]interface{}
|
||||
Styles []string
|
||||
Scripts []string
|
||||
Files []FileInfo
|
||||
}
|
||||
|
||||
// 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
|
||||
func execTemplate(c *Config, mdata metadata.Metadata, files []FileInfo, ctx httpserver.Context) ([]byte, error) {
|
||||
mdData := Data{
|
||||
Context: ctx,
|
||||
Doc: mdata.Variables,
|
||||
DocFlags: mdata.Flags,
|
||||
Styles: c.Styles,
|
||||
Scripts: c.Scripts,
|
||||
Files: files,
|
||||
Context: ctx,
|
||||
Doc: mdata.Variables,
|
||||
Styles: c.Styles,
|
||||
Scripts: c.Scripts,
|
||||
Files: files,
|
||||
}
|
||||
|
||||
b := new(bytes.Buffer)
|
||||
|
|
Loading…
Reference in a new issue