shithub: hugo

Download patch

ref: 1cb7ed6ac7fd808b7755a47ffc4878506a42d13c
parent: 937592cb85f6b4bf4603230050ac0afdcc93058b
author: Bjørn Erik Pedersen <[email protected]>
date: Wed Mar 23 10:51:16 EDT 2016

parser: Spring code cleaning

--- a/commands/convert.go
+++ b/commands/convert.go
@@ -45,7 +45,7 @@
 	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 @@
 	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 @@
 	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]))
 	},
 }
 
--- a/commands/undraft.go
+++ b/commands/undraft.go
@@ -121,15 +121,15 @@
 
 	// 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
--- a/parser/frontmatter.go
+++ b/parser/frontmatter.go
@@ -37,7 +37,7 @@
 	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 @@
 			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 @@
 	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 @@
 			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 @@
 		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 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 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 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) {
--- 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,35 +24,46 @@
 )
 
 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)
 }
@@ -59,15 +70,15 @@
 
 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,7 +157,7 @@
 	}
 
 	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?
@@ -153,7 +164,7 @@
 			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 @@
 		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 @@
 		return
 	}
 
-	if bytes.Equal(lead[:1], []byte(HTML_LEAD)) {
+	if bytes.Equal(lead[:1], []byte(HTMLLead)) {
 		return
 	}
 	return true
@@ -231,16 +242,16 @@
 	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 @@
 // 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 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