Commit c6b2600c authored by Matt Holt's avatar Matt Holt

Merge pull request #202 from evermax/master

markdown: Change metadata variables type to map[string]string

Instead of map[string]interface{}
parents 453d3eb5 d1eb2ea9
...@@ -13,9 +13,9 @@ import ( ...@@ -13,9 +13,9 @@ import (
var ( var (
parsers = []MetadataParser{ parsers = []MetadataParser{
&JSONMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}}, &JSONMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
&TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}}, &TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
&YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}}, &YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
} }
) )
...@@ -28,7 +28,7 @@ type Metadata struct { ...@@ -28,7 +28,7 @@ type Metadata struct {
Template string Template string
// Variables to be used with Template // Variables to be used with Template
Variables map[string]interface{} Variables map[string]string
} }
// load loads parsed values in parsedMap into Metadata // load loads parsed values in parsedMap into Metadata
...@@ -40,7 +40,7 @@ func (m *Metadata) load(parsedMap map[string]interface{}) { ...@@ -40,7 +40,7 @@ func (m *Metadata) load(parsedMap map[string]interface{}) {
m.Template, _ = template.(string) m.Template, _ = template.(string)
} }
if variables, ok := parsedMap["variables"]; ok { if variables, ok := parsedMap["variables"]; ok {
m.Variables, _ = variables.(map[string]interface{}) m.Variables, _ = variables.(map[string]string)
} }
} }
...@@ -76,6 +76,15 @@ func (j *JSONMetadataParser) Parse(b []byte) ([]byte, error) { ...@@ -76,6 +76,15 @@ func (j *JSONMetadataParser) Parse(b []byte) ([]byte, error) {
if err := decoder.Decode(&m); err != nil { if err := decoder.Decode(&m); err != nil {
return b, err return b, err
} }
if vars, ok := m["variables"].(map[string]interface{}); ok {
vars1 := make(map[string]string)
for k, v := range vars {
if val, ok := v.(string); ok {
vars1[k] = val
}
}
m["variables"] = vars1
}
j.metadata.load(m) j.metadata.load(m)
...@@ -120,6 +129,15 @@ func (t *TOMLMetadataParser) Parse(b []byte) ([]byte, error) { ...@@ -120,6 +129,15 @@ func (t *TOMLMetadataParser) Parse(b []byte) ([]byte, error) {
if err := toml.Unmarshal(b, &m); err != nil { if err := toml.Unmarshal(b, &m); err != nil {
return markdown, err return markdown, err
} }
if vars, ok := m["variables"].(map[string]interface{}); ok {
vars1 := make(map[string]string)
for k, v := range vars {
if val, ok := v.(string); ok {
vars1[k] = val
}
}
m["variables"] = vars1
}
t.metadata.load(m) t.metadata.load(m)
return markdown, nil return markdown, nil
} }
...@@ -160,10 +178,12 @@ func (y *YAMLMetadataParser) Parse(b []byte) ([]byte, error) { ...@@ -160,10 +178,12 @@ func (y *YAMLMetadataParser) Parse(b []byte) ([]byte, error) {
// convert variables (if present) to map[string]interface{} // convert variables (if present) to map[string]interface{}
// to match expected type // to match expected type
if vars, ok := m["variables"].(map[interface{}]interface{}); ok { if vars, ok := m["variables"].(map[interface{}]interface{}); ok {
vars1 := make(map[string]interface{}) vars1 := make(map[string]string)
for k, v := range vars { for k, v := range vars {
if key, ok := k.(string); ok { if key, ok := k.(string); ok {
vars1[key] = v if val, ok := v.(string); ok {
vars1[key] = val
}
} }
} }
m["variables"] = vars1 m["variables"] = vars1
......
...@@ -98,7 +98,7 @@ func TestParsers(t *testing.T) { ...@@ -98,7 +98,7 @@ func TestParsers(t *testing.T) {
expected := Metadata{ expected := Metadata{
Title: "A title", Title: "A title",
Template: "default", Template: "default",
Variables: map[string]interface{}{"name": "value"}, Variables: map[string]string{"name": "value"},
} }
compare := func(m Metadata) bool { compare := func(m Metadata) bool {
if m.Title != expected.Title { if m.Title != expected.Title {
......
...@@ -20,13 +20,13 @@ const ( ...@@ -20,13 +20,13 @@ const (
type MarkdownData struct { type MarkdownData struct {
middleware.Context middleware.Context
Doc map[string]interface{} Doc map[string]string
} }
// 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]interface{})} var metadata = Metadata{Variables: make(map[string]string)}
var markdown []byte var markdown []byte
var err error var err error
...@@ -166,7 +166,7 @@ func defaultTemplate(c Config, metadata Metadata, requestPath string) []byte { ...@@ -166,7 +166,7 @@ func defaultTemplate(c Config, metadata Metadata, requestPath string) []byte {
} }
// Title is first line (length-limited), otherwise filename // Title is first line (length-limited), otherwise filename
title, _ := metadata.Variables["title"].(string) title, _ := metadata.Variables["title"]
html := []byte(htmlTemplate) html := []byte(htmlTemplate)
html = bytes.Replace(html, []byte("{{title}}"), []byte(title), 1) html = bytes.Replace(html, []byte("{{title}}"), []byte(title), 1)
......
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