From e652d12cfcc642e5b589fecf633a0794cae594eb Mon Sep 17 00:00:00 2001 From: Tobias Weingartner Date: Sat, 16 Apr 2016 15:36:52 -0700 Subject: [PATCH] Move Metadata load into NewMetadata function. --- middleware/markdown/markdown.go | 4 +++- middleware/markdown/metadata/metadata.go | 18 +++++++++++------- middleware/markdown/metadata/metadata_json.go | 3 +-- middleware/markdown/metadata/metadata_none.go | 3 +-- middleware/markdown/metadata/metadata_toml.go | 3 +-- middleware/markdown/metadata/metadata_yaml.go | 3 +-- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/middleware/markdown/markdown.go b/middleware/markdown/markdown.go index 56c735f9..17083668 100644 --- a/middleware/markdown/markdown.go +++ b/middleware/markdown/markdown.go @@ -122,7 +122,9 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error return http.StatusInternalServerError, err } - // html, err = md.doTemplate(cfg, html, ctx) + // TODO(weingart): move template execution here, something like: + // + // html, err = md.execTemplate(cfg, html, ctx) // if err != nil { // return http.StatusInternalServerError, err // } diff --git a/middleware/markdown/metadata/metadata.go b/middleware/markdown/metadata/metadata.go index 027a8cdb..24cdb391 100644 --- a/middleware/markdown/metadata/metadata.go +++ b/middleware/markdown/metadata/metadata.go @@ -32,6 +32,17 @@ type Metadata struct { Flags map[string]bool } +// 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), + } + md.load(parsedMap) + + return md +} + // load loads parsed values in parsedMap into Metadata func (m *Metadata) load(parsedMap map[string]interface{}) { @@ -89,13 +100,6 @@ func GetParser(buf []byte) MetadataParser { return nil } -func NewMetadata() Metadata { - return Metadata{ - Variables: make(map[string]string), - Flags: make(map[string]bool), - } -} - // parsers returns all available parsers func parsers() []MetadataParser { return []MetadataParser{ diff --git a/middleware/markdown/metadata/metadata_json.go b/middleware/markdown/metadata/metadata_json.go index c35148c4..d3b9991f 100644 --- a/middleware/markdown/metadata/metadata_json.go +++ b/middleware/markdown/metadata/metadata_json.go @@ -36,8 +36,7 @@ func (j *JSONMetadataParser) Init(b *bytes.Buffer) bool { } } - j.metadata = NewMetadata() - j.metadata.load(m) + j.metadata = NewMetadata(m) j.markdown = bytes.NewBuffer(b.Bytes()) return true diff --git a/middleware/markdown/metadata/metadata_none.go b/middleware/markdown/metadata/metadata_none.go index f2ca10f3..ed034f2f 100644 --- a/middleware/markdown/metadata/metadata_none.go +++ b/middleware/markdown/metadata/metadata_none.go @@ -17,8 +17,7 @@ func (n *NoneMetadataParser) Type() string { // Parse metadata/markdown file func (n *NoneMetadataParser) Init(b *bytes.Buffer) bool { m := make(map[string]interface{}) - n.metadata = NewMetadata() - n.metadata.load(m) + n.metadata = NewMetadata(m) n.markdown = bytes.NewBuffer(b.Bytes()) return true diff --git a/middleware/markdown/metadata/metadata_toml.go b/middleware/markdown/metadata/metadata_toml.go index ed3a8451..75c2067f 100644 --- a/middleware/markdown/metadata/metadata_toml.go +++ b/middleware/markdown/metadata/metadata_toml.go @@ -28,8 +28,7 @@ func (t *TOMLMetadataParser) Init(b *bytes.Buffer) bool { if err := toml.Unmarshal(meta.Bytes(), &m); err != nil { return false } - t.metadata = NewMetadata() - t.metadata.load(m) + t.metadata = NewMetadata(m) return true } diff --git a/middleware/markdown/metadata/metadata_yaml.go b/middleware/markdown/metadata/metadata_yaml.go index 14161f5e..f7ef5bb4 100644 --- a/middleware/markdown/metadata/metadata_yaml.go +++ b/middleware/markdown/metadata/metadata_yaml.go @@ -27,8 +27,7 @@ func (y *YAMLMetadataParser) Init(b *bytes.Buffer) bool { if err := yaml.Unmarshal(meta.Bytes(), &m); err != nil { return false } - y.metadata = NewMetadata() - y.metadata.load(m) + y.metadata = NewMetadata(m) return true }