From d2a6267ad7664be33363bbe9e0502beca8f2c56d Mon Sep 17 00:00:00 2001 From: spf13 Date: Mon, 8 Jul 2013 22:23:54 -0400 Subject: [PATCH] Adding support for TOML, updating documentation --- README.md | 98 +++++++++++++++++++++++--------- docs/content/doc/contributors.md | 2 +- docs/content/doc/example.md | 2 +- docs/content/doc/front-matter.md | 79 ++++++++++++++++++------- docs/content/doc/roadmap.md | 4 +- docs/content/doc/rst.rst | 2 +- hugolib/page.go | 44 ++++++++++---- 7 files changed, 168 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index c56dc315d..555e7fa65 100644 --- a/README.md +++ b/README.md @@ -262,35 +262,78 @@ Without any additional configuration the following will just work. The front matter is one of the features that gives Hugo it's strength. It enables you to include the meta data of the content right with it. Hugo supports a few -different formats. The main format supported is JSON. Here is an example: +different formats each with their own identifying tokens. + +Supported formats:
+ **YAML**, identified by '\-\-\-'.
+ **TOML**, indentified with '+++'.
+ **JSON**, a single JSON object which is surrounded by '{' and '}' each on their own line. + +### YAML Example + + --- + title: "spf13-vim 3.0 release and new website" + description: "spf13-vim is a cross platform distribution of vim plugins and resources for Vim." + tags: [ ".vimrc", "plugins", "spf13-vim", "vim" ] + pubdate: "2012-04-06" + categories: + - "Development" + - "VIM" + slug: "spf13-vim-3-0-release-and-new-website" + --- + Content of the file goes Here + +### TOML Example + + +++ + title = "spf13-vim 3.0 release and new website" + description = "spf13-vim is a cross platform distribution of vim plugins and resources for Vim." + tags = [ ".vimrc", "plugins", "spf13-vim", "vim" ] + Pubdate = "2012-04-06" + categories = [ + "Development", + "VIM" + ] + slug = "spf13-vim-3-0-release-and-new-website" + +++ + Content of the file goes Here + +### JSON Example { - "Title": "spf13-vim 3.0 release and new website", - "Description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.", - "Tags": [ ".vimrc", "plugins", "spf13-vim", "vim" ], - "Pubdate": "2012-04-06", - "Categories": [ "Development", "VIM" ], - "Slug": "spf13-vim-3-0-release-and-new-website" + "title": "spf13-vim 3.0 release and new website", + "description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.", + "tags": [ ".vimrc", "plugins", "spf13-vim", "vim" ], + "date": "2012-04-06", + "categories": [ + "Development", + "VIM" + ], + "slug": "spf13-vim-3-0-release-and-new-website", } + Content of the file goes Here ### Variables There are a few predefined variables that Hugo is aware of and utilizes. The user can also create any variable they want to. These will be placed into the `.Params` variable available to the templates. +**Field names are case insensitive.** #### Required -**Title** The title for the content.
-**Description** The description for the content.
-**Pubdate** The date the content will be sorted by.
-**Indexes** These will use the field name of the plural form of the index (see tags and categories above) +**title** The title for the content.
+**description** The description for the content.
+**date** The date the content will be sorted by.
+**indexes** These will use the field name of the plural form of the index (see tags and categories above) #### Optional -**Draft** If true the content will not be rendered unless `hugo` is called with -d
-**Type** The type of the content (will be derived from the directory automatically if unset).
-**Slug** The token to appear in the tail of the url.
+**draft** If true the content will not be rendered unless `hugo` is called with -d
+**type** The type of the content (will be derived from the directory automatically if unset).
+**markup** (Experimental) Specify "rst" for reStructuredText (requires + `rst2html`,) or "md" (default) for the Markdown.
+**slug** The token to appear in the tail of the url.
*or*
-**Url** The full path to the content from the web root.
+**url** The full path to the content from the web root.
*If neither is present the filename will be used.* ## Example @@ -298,16 +341,16 @@ Somethings are better shown than explained. The following is a very basic exampl **mysite/project/nitro.md <- http://mysite.com/project/nitro.html** - { - "Title": "Nitro : A quick and simple profiler for golang", - "Description": "", - "Keywords": [ "Development", "golang", "profiling" ], - "Tags": [ "Development", "golang", "profiling" ], - "Pubdate": "2013-06-19", - "Topics": [ "Development", "GoLang" ], - "Slug": "nitro", - "project_url": "http://github.com/spf13/nitro" - } + --- + Title: "Nitro : A quick and simple profiler for golang" + Description": "" + Keywords": [ "Development", "golang", "profiling" ] + Tags": [ "Development", "golang", "profiling" ] + Pubdate": "2013-06-19" + Topics": [ "Development", "GoLang" ] + Slug": "nitro" + project_url": "http://github.com/spf13/nitro" + --- # Nitro @@ -329,7 +372,6 @@ Somethings are better shown than explained. The following is a very basic exampl Next include nitro in your application. - # Extras ## Shortcodes @@ -423,8 +465,8 @@ In no particular order, here is what I'm working on: * Syntax highlighting * Previous & Next * Related Posts - * Support for TOML front matter - * Proper YAML support for front matter + * Support for TOML front matter -- in head + * Proper YAML support for front matter -- in head * Support for other formats ## Contributing diff --git a/docs/content/doc/contributors.md b/docs/content/doc/contributors.md index 3a7d7e647..4bc5a9d31 100644 --- a/docs/content/doc/contributors.md +++ b/docs/content/doc/contributors.md @@ -1,6 +1,6 @@ --- title: "Contributors" -Pubdate: "2013-07-01" +date: "2013-07-01" --- Hugo was built with love and golang by: 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/docs/content/doc/front-matter.md b/docs/content/doc/front-matter.md index e4342099f..0fb03b443 100644 --- a/docs/content/doc/front-matter.md +++ b/docs/content/doc/front-matter.md @@ -1,43 +1,82 @@ ---- -title: "Front Matter" -Pubdate: "2013-07-01" ---- ++++ +title = "Front Matter" +date = "2013-07-01" ++++ The front matter is one of the features that gives Hugo it's strength. It enables you to include the meta data of the content right with it. Hugo supports a few -different formats. The main format supported is YAML. Here is an example: +different formats each with their own identifying tokens. + +Supported formats:
+ **YAML**, identified by '\-\-\-'.
+ **TOML**, indentified with '+++'.
+ **JSON**, a single JSON object which is surrounded by '{' and '}' each on their own line. + +### YAML Example --- - Title: "spf13-vim 3.0 release and new website" - Description: "spf13-vim is a cross platform distribution of vim plugins and resources for Vim." - Tags: [ ".vimrc", "plugins", "spf13-vim", "vim" ] - Pubdate: "2012-04-06" - Categories: + title: "spf13-vim 3.0 release and new website" + description: "spf13-vim is a cross platform distribution of vim plugins and resources for Vim." + tags: [ ".vimrc", "plugins", "spf13-vim", "vim" ] + pubdate: "2012-04-06" + categories: - "Development" - "VIM" - Slug: "spf13-vim-3-0-release-and-new-website" + slug: "spf13-vim-3-0-release-and-new-website" --- + Content of the file goes Here + +### TOML Example + + +++ + title = "spf13-vim 3.0 release and new website" + description = "spf13-vim is a cross platform distribution of vim plugins and resources for Vim." + tags = [ ".vimrc", "plugins", "spf13-vim", "vim" ] + Pubdate = "2012-04-06" + categories = [ + "Development", + "VIM" + ] + slug = "spf13-vim-3-0-release-and-new-website" + +++ + Content of the file goes Here + +### JSON Example + + { + "title": "spf13-vim 3.0 release and new website", + "description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.", + "tags": [ ".vimrc", "plugins", "spf13-vim", "vim" ], + "date": "2012-04-06", + "categories": [ + "Development", + "VIM" + ], + "slug": "spf13-vim-3-0-release-and-new-website", + } + Content of the file goes Here ### Variables There are a few predefined variables that Hugo is aware of and utilizes. The user can also create any variable they want to. These will be placed into the `.Params` variable available to the templates. +**Field names are case insensitive.** #### Required -**Title** The title for the content.
-**Description** The description for the content.
-**Pubdate** The date the content will be sorted by.
-**Indexes** These will use the field name of the plural form of the index (see tags and categories above) +**title** The title for the content.
+**description** The description for the content.
+**date** The date the content will be sorted by.
+**indexes** These will use the field name of the plural form of the index (see tags and categories above) #### Optional -**Draft** If true the content will not be rendered unless `hugo` is called with -d
-**Type** The type of the content (will be derived from the directory automatically if unset).
-**Markup** (Experimental) Specify "rst" for reStructuredText (requires +**draft** If true the content will not be rendered unless `hugo` is called with -d
+**type** The type of the content (will be derived from the directory automatically if unset).
+**markup** (Experimental) Specify "rst" for reStructuredText (requires `rst2html`,) or "md" (default) for the Markdown.
-**Slug** The token to appear in the tail of the url.
+**slug** The token to appear in the tail of the url.
*or*
-**Url** The full path to the content from the web root.
+**url** The full path to the content from the web root.
*If neither is present the filename will be used.* diff --git a/docs/content/doc/roadmap.md b/docs/content/doc/roadmap.md index 93a7a83d4..1db9f2d79 100644 --- a/docs/content/doc/roadmap.md +++ b/docs/content/doc/roadmap.md @@ -11,8 +11,8 @@ In no particular order, here is what I'm working on: * Syntax highlighting * Previous & Next * Related Posts - * Support for TOML front matter - * Proper YAML support for front matter + * Support for TOML front matter -- in head + * Proper YAML support for front matter -- in head * Support for other formats diff --git a/docs/content/doc/rst.rst b/docs/content/doc/rst.rst index c2838eb0a..37e1357cb 100644 --- a/docs/content/doc/rst.rst +++ b/docs/content/doc/rst.rst @@ -1,6 +1,6 @@ --- Markup: 'rst' -... +--- ============== diff --git a/hugolib/page.go b/hugolib/page.go index 0adcb4957..c21c1c357 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -17,6 +17,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/BurntSushi/toml" "github.com/theplant/blackfriday" "html/template" "io/ioutil" @@ -154,6 +155,15 @@ func (page *Page) parseYamlMetaData(data []byte) ([]string, error) { return lines, err } +func (page *Page) parseTomlMetaData(data []byte) ([]string, error) { + var err error + + datum, lines := splitPageContent(data, "+++", "+++") + + err = page.handleMetaData(page.handleTomlMetaData([]byte(strings.Join(datum, "\n")))) + return lines, err +} + func (page *Page) parseJsonMetaData(data []byte) ([]string, error) { var err error @@ -185,15 +195,17 @@ func splitPageContent(data []byte, start string, end string) ([]string, []string break } } - } else { - if found == 0 && strings.HasPrefix(line, start) { - found += 1 - } + } else { // Start token & end token are the same + for i, line := range lines { + if found == 1 && strings.HasPrefix(line, end) { + datum = lines[1:i] + lines = lines[i+1:] + break + } - if found == 0 && strings.HasPrefix(line, end) { - datum = lines[1 : i+1] - lines = lines[i+1:] - break + if found == 0 && strings.HasPrefix(line, start) { + found = 1 + } } } return datum, lines @@ -211,6 +223,14 @@ func (p *Page) Permalink() template.HTML { } } +func (page *Page) handleTomlMetaData(datum []byte) interface{} { + m := map[string]interface{}{} + if _, err := toml.Decode(string(datum), &m); err != nil { + return fmt.Errorf("Invalid TOML in %s \nError parsing page meta data: %s", page.FileName, err) + } + return m +} + func (page *Page) handleYamlMetaData(datum []byte) interface{} { m := map[string]interface{}{} if err := goyaml.Unmarshal(datum, &m); err != nil { @@ -304,10 +324,14 @@ func (page *Page) parseFileHeading(data []byte) ([]string, error) { if len(data) == 0 { page.Err("Empty File, skipping") } else { - if data[0] == '{' { + switch data[0] { + case '{': return page.parseJsonMetaData(data) + case '-': + return page.parseYamlMetaData(data) + case '+': + return page.parseTomlMetaData(data) } - return page.parseYamlMetaData(data) } return nil, nil }