aboutsummaryrefslogtreecommitdiffhomepage
path: root/parser
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2017-06-19 16:45:52 +0200
committerBjørn Erik Pedersen <[email protected]>2017-06-19 16:45:52 +0200
commit3183b9a29d8adac962fbc73f79b04542f4c4c55d (patch)
treeaf6253017ce253961e7c80ab75200a644b1abb16 /parser
parent1a282ee4325588b410531401387e4e4daa1a33c5 (diff)
downloadhugo-3183b9a29d8adac962fbc73f79b04542f4c4c55d.tar.gz
hugo-3183b9a29d8adac962fbc73f79b04542f4c4c55d.zip
parser: Fix handling of quoted brackets in JSON front matter
Also remove a broken JSON test. Fixes #3511
Diffstat (limited to 'parser')
-rw-r--r--parser/page.go12
-rw-r--r--parser/parse_frontmatter_test.go9
2 files changed, 15 insertions, 6 deletions
diff --git a/parser/page.go b/parser/page.go
index 9639dd7a4..bacc9754b 100644
--- a/parser/page.go
+++ b/parser/page.go
@@ -304,8 +304,8 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
buf bytes.Buffer
level int
sameDelim = bytes.Equal(left, right)
+ inQuote bool
)
-
// Frontmatter must start with a delimiter. To check it first,
// pre-reads beginning delimiter length - 1 bytes from Reader
for i := 0; i < len(left)-1; i++ {
@@ -332,6 +332,8 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
}
switch c {
+ case '"':
+ inQuote = !inQuote
case left[len(left)-1]:
if sameDelim { // YAML, TOML case
if bytes.HasSuffix(buf.Bytes(), left) && (buf.Len() == len(left) || buf.Bytes()[buf.Len()-len(left)-1] == '\n') {
@@ -373,10 +375,14 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
}
}
} else { // JSON case
- level++
+ if !inQuote {
+ level++
+ }
}
case right[len(right)-1]: // JSON case only reaches here
- level--
+ if !inQuote {
+ level--
+ }
}
if level == 0 {
diff --git a/parser/parse_frontmatter_test.go b/parser/parse_frontmatter_test.go
index 5de663b26..08a1cc42c 100644
--- a/parser/parse_frontmatter_test.go
+++ b/parser/parse_frontmatter_test.go
@@ -296,18 +296,21 @@ func TestExtractFrontMatterDelim(t *testing.T) {
{"{\n{\n}\n}\n", "{\n{\n}\n}", noErrExpected},
{"{\n \"categories\": \"d\",\n \"tags\": [\n \"a\", \n \"b\", \n \"c\"\n ]\n}\nJSON Front Matter with tags and categories", "{\n \"categories\": \"d\",\n \"tags\": [\n \"a\", \n \"b\", \n \"c\"\n ]\n}", noErrExpected},
{"{\n \"categories\": \"d\"\n \"tags\": [\n \"a\" \n \"b\" \n \"c\"\n ]\n}\nJSON Front Matter with tags and categories", "{\n \"categories\": \"d\"\n \"tags\": [\n \"a\" \n \"b\" \n \"c\"\n ]\n}", noErrExpected},
+ // Issue #3511
+ {`{ "title": "{" }`, `{ "title": "{" }`, noErrExpected},
+ {`{ "title": "{}" }`, `{ "title": "{}" }`, noErrExpected},
}
- for _, test := range tests {
+ for i, test := range tests {
fm, err := extractFrontMatterDelims(bufio.NewReader(strings.NewReader(test.frontmatter)), []byte("{"), []byte("}"))
if (err == nil) != test.errIsNil {
t.Logf("\n%q\n", string(test.frontmatter))
- t.Errorf("Expected err == nil => %t, got: %t. err: %s", test.errIsNil, err == nil, err)
+ t.Errorf("[%d] Expected err == nil => %t, got: %t. err: %s", i, test.errIsNil, err == nil, err)
continue
}
if !bytes.Equal(fm, []byte(test.extracted)) {
t.Logf("\n%q\n", string(test.frontmatter))
- t.Errorf("Frontmatter did not match:\nexp: %q\ngot: %q", string(test.extracted), fm)
+ t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot: %q", i, string(test.extracted), fm)
}
}
}