diff options
-rw-r--r-- | commands/convert.go | 6 | ||||
-rw-r--r-- | commands/undraft.go | 8 | ||||
-rw-r--r-- | parser/frontmatter.go | 32 | ||||
-rw-r--r-- | parser/page.go | 85 |
4 files changed, 71 insertions, 60 deletions
diff --git a/commands/convert.go b/commands/convert.go index ca87e6fe0..fc15b847c 100644 --- a/commands/convert.go +++ b/commands/convert.go @@ -45,7 +45,7 @@ var toJSONCmd = &cobra.Command{ Long: `toJSON converts all front matter in the content directory to use JSON for the front matter.`, RunE: func(cmd *cobra.Command, args []string) error { - return convertContents(rune([]byte(parser.JSON_LEAD)[0])) + return convertContents(rune([]byte(parser.JSONLead)[0])) }, } @@ -55,7 +55,7 @@ var toTOMLCmd = &cobra.Command{ Long: `toTOML converts all front matter in the content directory to use TOML for the front matter.`, RunE: func(cmd *cobra.Command, args []string) error { - return convertContents(rune([]byte(parser.TOML_LEAD)[0])) + return convertContents(rune([]byte(parser.TOMLLead)[0])) }, } @@ -65,7 +65,7 @@ var toYAMLCmd = &cobra.Command{ Long: `toYAML converts all front matter in the content directory to use YAML for the front matter.`, RunE: func(cmd *cobra.Command, args []string) error { - return convertContents(rune([]byte(parser.YAML_LEAD)[0])) + return convertContents(rune([]byte(parser.YAMLLead)[0])) }, } diff --git a/commands/undraft.go b/commands/undraft.go index 26ebdb2c1..c4bc2ffb0 100644 --- a/commands/undraft.go +++ b/commands/undraft.go @@ -121,15 +121,15 @@ L: // get the front matter as bytes and split it into lines var lineEnding []byte - fmLines := bytes.Split(fm, parser.UnixEnding) + fmLines := bytes.Split(fm, []byte("\n")) if len(fmLines) == 1 { // if the result is only 1 element, try to split on dos line endings - fmLines = bytes.Split(fm, parser.DosEnding) + fmLines = bytes.Split(fm, []byte("\r\n")) if len(fmLines) == 1 { return buff, fmt.Errorf("unable to split FrontMatter into lines") } - lineEnding = append(lineEnding, parser.DosEnding...) + lineEnding = append(lineEnding, []byte("\r\n")...) } else { - lineEnding = append(lineEnding, parser.UnixEnding...) + lineEnding = append(lineEnding, []byte("\n")...) } // Write the front matter lines to the buffer, replacing as necessary diff --git a/parser/frontmatter.go b/parser/frontmatter.go index cbeb10be3..d12a7b6d6 100644 --- a/parser/frontmatter.go +++ b/parser/frontmatter.go @@ -37,7 +37,7 @@ func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) { b := new(bytes.Buffer) switch mark { - case rune(YAML_LEAD[0]): + case rune(YAMLLead[0]): by, err := yaml.Marshal(in) if err != nil { return nil, err @@ -48,13 +48,13 @@ func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) { return nil, err } return b.Bytes(), nil - case rune(TOML_LEAD[0]): + case rune(TOMLLead[0]): err := toml.NewEncoder(b).Encode(in) if err != nil { return nil, err } return b.Bytes(), nil - case rune(JSON_LEAD[0]): + case rune(JSONLead[0]): by, err := json.MarshalIndent(in, "", " ") if err != nil { return nil, err @@ -78,8 +78,8 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) { b := new(bytes.Buffer) switch mark { - case rune(YAML_LEAD[0]): - _, err := b.Write([]byte(YAML_DELIM_UNIX)) + case rune(YAMLLead[0]): + _, err := b.Write([]byte(YAMLDelimUnix)) if err != nil { return nil, err } @@ -88,13 +88,13 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) { return nil, err } b.Write(by) - _, err = b.Write([]byte(YAML_DELIM_UNIX)) + _, err = b.Write([]byte(YAMLDelimUnix)) if err != nil { return nil, err } return b.Bytes(), nil - case rune(TOML_LEAD[0]): - _, err := b.Write([]byte(TOML_DELIM_UNIX)) + case rune(TOMLLead[0]): + _, err := b.Write([]byte(TOMLDelimUnix)) if err != nil { return nil, err } @@ -103,12 +103,12 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) { if err != nil { return nil, err } - _, err = b.Write([]byte("\n" + TOML_DELIM_UNIX)) + _, err = b.Write([]byte("\n" + TOMLDelimUnix)) if err != nil { return nil, err } return b.Bytes(), nil - case rune(JSON_LEAD[0]): + case rune(JSONLead[0]): by, err := json.MarshalIndent(in, "", " ") if err != nil { return nil, err @@ -127,11 +127,11 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) { func FormatToLeadRune(kind string) rune { switch FormatSanitize(kind) { case "yaml": - return rune([]byte(YAML_LEAD)[0]) + return rune([]byte(YAMLLead)[0]) case "json": - return rune([]byte(JSON_LEAD)[0]) + return rune([]byte(JSONLead)[0]) default: - return rune([]byte(TOML_LEAD)[0]) + return rune([]byte(TOMLLead)[0]) } } @@ -152,9 +152,9 @@ func FormatSanitize(kind string) string { func detectFrontMatter(mark rune) (f *frontmatterType) { switch mark { case '-': - return &frontmatterType{[]byte(YAML_DELIM), []byte(YAML_DELIM), HandleYAMLMetaData, false} + return &frontmatterType{[]byte(YAMLDelim), []byte(YAMLDelim), HandleYAMLMetaData, false} case '+': - return &frontmatterType{[]byte(TOML_DELIM), []byte(TOML_DELIM), HandleTOMLMetaData, false} + return &frontmatterType{[]byte(TOMLDelim), []byte(TOMLDelim), HandleTOMLMetaData, false} case '{': return &frontmatterType{[]byte{'{'}, []byte{'}'}, HandleJSONMetaData, true} default: @@ -172,7 +172,7 @@ func HandleTOMLMetaData(datum []byte) (interface{}, error) { } func removeTOMLIdentifier(datum []byte) []byte { - return bytes.Replace(datum, []byte(TOML_DELIM), []byte(""), -1) + return bytes.Replace(datum, []byte(TOMLDelim), []byte(""), -1) } func HandleYAMLMetaData(datum []byte) (interface{}, error) { diff --git a/parser/page.go b/parser/page.go index 018b3d35d..79badf397 100644 --- a/parser/page.go +++ b/parser/page.go @@ -1,4 +1,4 @@ -// Copyright 2015 The Hugo Authors. All rights reserved. +// Copyright 2016n The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -24,50 +24,61 @@ import ( ) const ( - HTML_LEAD = "<" - YAML_LEAD = "-" - YAML_DELIM_UNIX = "---\n" - YAML_DELIM_DOS = "---\r\n" - YAML_DELIM = "---" - TOML_LEAD = "+" - TOML_DELIM_UNIX = "+++\n" - TOML_DELIM_DOS = "+++\r\n" - TOML_DELIM = "+++" - JSON_LEAD = "{" - HTML_COMMENT_START = "<!--" - HTML_COMMENT_END = "-->" + // TODO(bep) Do we really have to export these? + + // HTMLLead identifies the start of HTML documents. + HTMLLead = "<" + // YAMLLead identifies the start of YAML frontmatter. + YAMLLead = "-" + // YAMLDelimUnix identifies the end of YAML front matter on Unix. + YAMLDelimUnix = "---\n" + // YAMLDelimDOS identifies the end of YAML front matter on Windows. + YAMLDelimDOS = "---\r\n" + // YAMLDelim identifies the YAML front matter delimiter. + YAMLDelim = "---" + // TOMLLead identifies the start of TOML front matter. + TOMLLead = "+" + // TOMLDelimUnix identifies the end of TOML front matter on Unix. + TOMLDelimUnix = "+++\n" + // TOMLDelimDOS identifies the end of TOML front matter on Windows. + TOMLDelimDOS = "+++\r\n" + // TOMLDelim identifies the TOML front matter delimiter. + TOMLDelim = "+++" + // JSONLead identifies the start of JSON frontmatter. + JSONLead = "{" + // HTMLCommentStart identifies the start of HTML comment. + HTMLCommentStart = "<!--" + // HTMLCommentEnd identifies the end of HTML comment. + HTMLCommentEnd = "-->" ) var ( delims = regexp.MustCompile( - "^(" + regexp.QuoteMeta(YAML_DELIM) + `\s*\n|` + regexp.QuoteMeta(TOML_DELIM) + `\s*\n|` + regexp.QuoteMeta(JSON_LEAD) + ")", + "^(" + regexp.QuoteMeta(YAMLDelim) + `\s*\n|` + regexp.QuoteMeta(TOMLDelim) + `\s*\n|` + regexp.QuoteMeta(JSONLead) + ")", ) - - UnixEnding = []byte("\n") - DosEnding = []byte("\r\n") + unixEnding = []byte("\n") + dosEnding = []byte("\r\n") ) -type FrontMatter []byte -type Content []byte - +// Page represents a parsed content page. type Page interface { - FrontMatter() FrontMatter - Content() Content + FrontMatter() []byte + Content() []byte IsRenderable() bool Metadata() (interface{}, error) } type page struct { render bool - frontmatter FrontMatter - content Content + frontmatter []byte + content []byte } -func (p *page) Content() Content { +func (p *page) Content() []byte { return p.content } -func (p *page) FrontMatter() FrontMatter { +func (p *page) FrontMatter() []byte { return p.frontmatter } @@ -146,14 +157,14 @@ func chompFrontmatterStartComment(r *bufio.Reader) (err error) { } str := string(candidate) - if strings.HasPrefix(str, HTML_COMMENT_START) { + if strings.HasPrefix(str, HTMLCommentStart) { lineEnd := strings.IndexAny(str, "\n") if lineEnd == -1 { //TODO: if we can't find it, Peek more? return nil } testStr := strings.TrimSuffix(str[0:lineEnd], "\r") - if strings.Index(testStr, HTML_COMMENT_END) != -1 { + if strings.Index(testStr, HTMLCommentEnd) != -1 { return nil } buf := make([]byte, lineEnd) @@ -180,12 +191,12 @@ func chompFrontmatterEndComment(r *bufio.Reader) (err error) { return nil } testStr := strings.TrimSuffix(str[0:lineEnd], "\r") - if strings.Index(testStr, HTML_COMMENT_START) != -1 { + if strings.Index(testStr, HTMLCommentStart) != -1 { return nil } //TODO: if we can't find it, Peek more? - if strings.HasSuffix(testStr, HTML_COMMENT_END) { + if strings.HasSuffix(testStr, HTMLCommentEnd) { buf := make([]byte, lineEnd) if _, err = r.Read(buf); err != nil { return @@ -216,7 +227,7 @@ func shouldRender(lead []byte) (frontmatter bool) { return } - if bytes.Equal(lead[:1], []byte(HTML_LEAD)) { + if bytes.Equal(lead[:1], []byte(HTMLLead)) { return } return true @@ -231,16 +242,16 @@ func determineDelims(firstLine []byte) (left, right []byte) { case 5: fallthrough case 4: - if firstLine[0] == YAML_LEAD[0] { - return []byte(YAML_DELIM), []byte(YAML_DELIM) + if firstLine[0] == YAMLLead[0] { + return []byte(YAMLDelim), []byte(YAMLDelim) } - return []byte(TOML_DELIM), []byte(TOML_DELIM) + return []byte(TOMLDelim), []byte(TOMLDelim) case 3: fallthrough case 2: fallthrough case 1: - return []byte(JSON_LEAD), []byte("}") + return []byte(JSONLead), []byte("}") default: panic(fmt.Sprintf("Unable to determine delims from %q", firstLine)) } @@ -249,7 +260,7 @@ func determineDelims(firstLine []byte) (left, right []byte) { // extractFrontMatterDelims takes a frontmatter from the content bufio.Reader. // Beginning white spaces of the bufio.Reader must be trimmed before call this // function. -func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm FrontMatter, err error) { +func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, err error) { var ( c byte buf bytes.Buffer @@ -344,7 +355,7 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm FrontMatt } } -func extractContent(r io.Reader) (content Content, err error) { +func extractContent(r io.Reader) (content []byte, err error) { wr := new(bytes.Buffer) if _, err = wr.ReadFrom(r); err != nil { return |