shithub: hugo

Download patch

ref: 8d28686edcf19b5dd067482b72fdc1b93c99ef7f
parent: 211b757fee6d587a365104652ef3b1d335b702fa
author: Jeff Hodges <[email protected]>
date: Sat Aug 1 18:24:22 EDT 2015

Trim trailing spaces from YAML and TOML delimiters

When someone hits space after typing "---" (or "+++") but before they
hit return, hugo silently failed to parse the file. This corrects that.

--- a/parser/page.go
+++ b/parser/page.go
@@ -5,6 +5,7 @@
 	"bytes"
 	"fmt"
 	"io"
+	"regexp"
 	"unicode"
 )
 
@@ -22,13 +23,9 @@
 )
 
 var (
-	delims = [][]byte{
-		[]byte(YAML_DELIM_UNIX),
-		[]byte(YAML_DELIM_DOS),
-		[]byte(TOML_DELIM_UNIX),
-		[]byte(TOML_DELIM_DOS),
-		[]byte(JSON_LEAD),
-	}
+	delims = regexp.MustCompile(
+		"^(" + regexp.QuoteMeta(YAML_DELIM) + `\s*\n|` + regexp.QuoteMeta(TOML_DELIM) + `\s*\n|` + regexp.QuoteMeta(JSON_LEAD) + ")",
+	)
 
 	UnixEnding = []byte("\n")
 	DosEnding  = []byte("\r\n")
@@ -148,13 +145,7 @@
 }
 
 func isFrontMatterDelim(data []byte) bool {
-	for _, d := range delims {
-		if bytes.HasPrefix(data, d) {
-			return true
-		}
-	}
-
-	return false
+	return delims.Match(data)
 }
 
 func determineDelims(firstLine []byte) (left, right []byte) {
@@ -217,6 +208,7 @@
 		case left[len(left)-1]:
 			if sameDelim { // YAML, TOML case
 				if bytes.HasSuffix(buf.Bytes(), left) {
+				nextByte:
 					c, err = r.ReadByte()
 					if err != nil {
 						// It is ok that the end delimiter ends with EOF
@@ -227,6 +219,9 @@
 						switch c {
 						case '\n':
 							// ok
+						case ' ':
+							// Consume this byte and try to match again
+							goto nextByte
 						case '\r':
 							if err = buf.WriteByte(c); err != nil {
 								return nil, err
--- a/parser/parse_frontmatter_test.go
+++ b/parser/parse_frontmatter_test.go
@@ -200,6 +200,8 @@
 		{[]byte("---"), false},
 		{[]byte("---\n"), true},
 		{[]byte("---\n"), true},
+		{[]byte("--- \n"), true},
+		{[]byte("---  \n"), true},
 		{[]byte{'a'}, false},
 		{[]byte{'{'}, true},
 		{[]byte("{\n  "), true},
@@ -230,7 +232,10 @@
 		{"---\nblar\n-\n", nil, false},
 		{"---\nralb\n---\n", []byte("---\nralb\n---\n"), true},
 		{"---\neof\n---", []byte("---\neof\n---"), true},
+		{"--- \neof\n---", []byte("---\neof\n---"), true},
 		{"---\nminc\n---\ncontent", []byte("---\nminc\n---\n"), true},
+		{"---\nminc\n---    \ncontent", []byte("---\nminc\n---\n"), true},
+		{"---  \nminc\n--- \ncontent", []byte("---\nminc\n---\n"), true},
 		{"---\ncnim\n---\ncontent\n", []byte("---\ncnim\n---\n"), true},
 		{"---\ntitle: slug doc 2\nslug: slug-doc-2\n---\ncontent\n", []byte("---\ntitle: slug doc 2\nslug: slug-doc-2\n---\n"), true},
 	}