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.
This commit is contained in:
Jeff Hodges 2015-08-01 22:24:22 -07:00 committed by Bjørn Erik Pedersen
parent 211b757fee
commit 8d28686edc
2 changed files with 14 additions and 14 deletions

View file

@ -5,6 +5,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"regexp"
"unicode" "unicode"
) )
@ -22,13 +23,9 @@ const (
) )
var ( var (
delims = [][]byte{ delims = regexp.MustCompile(
[]byte(YAML_DELIM_UNIX), "^(" + regexp.QuoteMeta(YAML_DELIM) + `\s*\n|` + regexp.QuoteMeta(TOML_DELIM) + `\s*\n|` + regexp.QuoteMeta(JSON_LEAD) + ")",
[]byte(YAML_DELIM_DOS), )
[]byte(TOML_DELIM_UNIX),
[]byte(TOML_DELIM_DOS),
[]byte(JSON_LEAD),
}
UnixEnding = []byte("\n") UnixEnding = []byte("\n")
DosEnding = []byte("\r\n") DosEnding = []byte("\r\n")
@ -148,13 +145,7 @@ func shouldRender(lead []byte) (frontmatter bool) {
} }
func isFrontMatterDelim(data []byte) bool { func isFrontMatterDelim(data []byte) bool {
for _, d := range delims { return delims.Match(data)
if bytes.HasPrefix(data, d) {
return true
}
}
return false
} }
func determineDelims(firstLine []byte) (left, right []byte) { func determineDelims(firstLine []byte) (left, right []byte) {
@ -217,6 +208,7 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm FrontMatt
case left[len(left)-1]: case left[len(left)-1]:
if sameDelim { // YAML, TOML case if sameDelim { // YAML, TOML case
if bytes.HasSuffix(buf.Bytes(), left) { if bytes.HasSuffix(buf.Bytes(), left) {
nextByte:
c, err = r.ReadByte() c, err = r.ReadByte()
if err != nil { if err != nil {
// It is ok that the end delimiter ends with EOF // It is ok that the end delimiter ends with EOF
@ -227,6 +219,9 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm FrontMatt
switch c { switch c {
case '\n': case '\n':
// ok // ok
case ' ':
// Consume this byte and try to match again
goto nextByte
case '\r': case '\r':
if err = buf.WriteByte(c); err != nil { if err = buf.WriteByte(c); err != nil {
return nil, err return nil, err

View file

@ -200,6 +200,8 @@ func TestPageHasFrontMatter(t *testing.T) {
{[]byte("---"), false}, {[]byte("---"), false},
{[]byte("---\n"), true}, {[]byte("---\n"), true},
{[]byte("---\n"), true}, {[]byte("---\n"), true},
{[]byte("--- \n"), true},
{[]byte("--- \n"), true},
{[]byte{'a'}, false}, {[]byte{'a'}, false},
{[]byte{'{'}, true}, {[]byte{'{'}, true},
{[]byte("{\n "), true}, {[]byte("{\n "), true},
@ -230,7 +232,10 @@ func TestExtractFrontMatter(t *testing.T) {
{"---\nblar\n-\n", nil, false}, {"---\nblar\n-\n", nil, false},
{"---\nralb\n---\n", []byte("---\nralb\n---\n"), true}, {"---\nralb\n---\n", []byte("---\nralb\n---\n"), true},
{"---\neof\n---", []byte("---\neof\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},
{"--- \nminc\n--- \ncontent", []byte("---\nminc\n---\n"), true},
{"---\ncnim\n---\ncontent\n", []byte("---\ncnim\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}, {"---\ntitle: slug doc 2\nslug: slug-doc-2\n---\ncontent\n", []byte("---\ntitle: slug doc 2\nslug: slug-doc-2\n---\n"), true},
} }