From b024454ea99e647c2b85ce13b3c89c61a4cd0e54 Mon Sep 17 00:00:00 2001 From: tycho garen Date: Sun, 7 Jul 2013 00:49:57 -0400 Subject: [PATCH] fixes: returning json parsing, error messages, yaml header offsets --- docs/content/doc/example.md | 2 +- hugolib/page.go | 32 ++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/docs/content/doc/example.md b/docs/content/doc/example.md index ab409c595..51e3c160e 100644 --- a/docs/content/doc/example.md +++ b/docs/content/doc/example.md @@ -16,7 +16,7 @@ Somethings are better shown than explained. The following is a very basic exampl Topics": [ "Development", "GoLang" ] Slug": "nitro" project_url": "http://github.com/spf13/nitro" - ... + --- # Nitro diff --git a/hugolib/page.go b/hugolib/page.go index a1bfffe3d..b0e796e97 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -165,14 +165,13 @@ func (page *Page) parseYamlMetaData(data []byte) ([]string, error) { } if found == 0 { - datum = lines[0: i+1] - lines = lines[i+1:] + datum = lines[1: i+2] + lines = lines[i+3:] break } } - err = page.handleYamlMetaData([]byte(strings.Join(datum, "\n"))) - + err = page.handleMetaData(page.handleYamlMetaData([]byte(strings.Join(datum, "\n")))) return lines, err } @@ -203,8 +202,7 @@ func (page *Page) parseJsonMetaData(data []byte) ([]string, error) { } } - err = page.handleJsonMetaData([]byte(strings.Join(datum, "\n"))) - + err = page.handleMetaData(page.handleJsonMetaData([]byte(strings.Join(datum, "\n")))) return lines, err } @@ -220,11 +218,25 @@ func (p *Page) Permalink() template.HTML { } } -func (page *Page) handleYamlMetaData(datum []byte) error { +func (page *Page) handleYamlMetaData(datum []byte) interface{} { m := map[string]interface{}{} if err := goyaml.Unmarshal(datum, &m); err != nil { - return fmt.Errorf("Invalid YAML in $v \nError parsing page meta data: %s", page.FileName, err) + return fmt.Errorf("Invalid YAML in %s \nError parsing page meta data: %s", page.FileName, err) } + return m +} + + +func (page *Page) handleJsonMetaData(datum []byte) interface{} { + var f interface{} + if err := json.Unmarshal(datum, &f); err != nil { + return fmt.Errorf("Invalide JSON in $v \nError parsing page meta data: %s", page.FileName, err) + } + return f +} + +func (page *Page) handleMetaData(f interface{}) error { + m := f.(map[string]interface{}) for k, v := range m { switch strings.ToLower(k) { @@ -271,8 +283,8 @@ func (page *Page) handleYamlMetaData(datum []byte) error { } } } - //Printer(page.Params) return nil + } func (page *Page) GetParam(key string) interface{} { @@ -388,7 +400,7 @@ func (page *Page) convertRestructuredText(lines []string) { var out bytes.Buffer cmd.Stdout = &out if err := cmd.Run(); err != nil { - print(err) + fmt.Println(err) } content := out.String()