Skip to content

Commit 3183b9a

Browse files
committed
parser: Fix handling of quoted brackets in JSON front matter
Also remove a broken JSON test. Fixes #3511
1 parent 1a282ee commit 3183b9a

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

‎hugolib/page_test.go‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,7 @@ Leading
7979
8080
Content of the file goes Here
8181
`
82-
simplePageJSONLoose = `
83-
{
84-
"title": "spf13-vim 3.0 release and new website"
85-
"description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim."
86-
"tags": [ ".vimrc", "plugins", "spf13-vim", "VIm" ]
87-
"date": "2012-04-06"
88-
"categories": [
89-
"Development"
90-
"VIM"
91-
],
92-
"slug": "spf13-vim-3-0-release-and-new-website"
93-
}
9482

95-
Content of the file goes Here
96-
`
9783
simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
9884
simplePageJSONMultiple = `
9985
{
@@ -941,16 +927,15 @@ func TestCreatePage(t *testing.T) {
941927
r string
942928
}{
943929
{simplePageJSON},
944-
{simplePageJSONLoose},
945930
{simplePageJSONMultiple},
946931
//{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)},
947932
}
948933

949-
for _, test := range tests {
934+
for i, test := range tests {
950935
s := newTestSite(t)
951936
p, _ := s.NewPage("page")
952937
if _, err := p.ReadFrom(strings.NewReader(test.r)); err != nil {
953-
t.Errorf("Unable to parse page: %s", err)
938+
t.Fatalf("[%d] Unable to parse page: %s", i, err)
954939
}
955940
}
956941
}

‎parser/page.go‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
304304
buf bytes.Buffer
305305
level int
306306
sameDelim = bytes.Equal(left, right)
307+
inQuote bool
307308
)
308-
309309
// Frontmatter must start with a delimiter. To check it first,
310310
// pre-reads beginning delimiter length - 1 bytes from Reader
311311
for i := 0; i < len(left)-1; i++ {
@@ -332,6 +332,8 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
332332
}
333333

334334
switch c {
335+
case '"':
336+
inQuote = !inQuote
335337
case left[len(left)-1]:
336338
if sameDelim { // YAML, TOML case
337339
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
373375
}
374376
}
375377
} else { // JSON case
376-
level++
378+
if !inQuote {
379+
level++
380+
}
377381
}
378382
case right[len(right)-1]: // JSON case only reaches here
379-
level--
383+
if !inQuote {
384+
level--
385+
}
380386
}
381387

382388
if level == 0 {

‎parser/parse_frontmatter_test.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,21 @@ func TestExtractFrontMatterDelim(t *testing.T) {
296296
{"{\n{\n}\n}\n", "{\n{\n}\n}", noErrExpected},
297297
{"{\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},
298298
{"{\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},
299+
// Issue #3511
300+
{`{ "title": "{" }`, `{ "title": "{" }`, noErrExpected},
301+
{`{ "title": "{}" }`, `{ "title": "{}" }`, noErrExpected},
299302
}
300303

301-
for _, test := range tests {
304+
for i, test := range tests {
302305
fm, err := extractFrontMatterDelims(bufio.NewReader(strings.NewReader(test.frontmatter)), []byte("{"), []byte("}"))
303306
if (err == nil) != test.errIsNil {
304307
t.Logf("\n%q\n", string(test.frontmatter))
305-
t.Errorf("Expected err == nil => %t, got: %t. err: %s", test.errIsNil, err == nil, err)
308+
t.Errorf("[%d] Expected err == nil => %t, got: %t. err: %s", i, test.errIsNil, err == nil, err)
306309
continue
307310
}
308311
if !bytes.Equal(fm, []byte(test.extracted)) {
309312
t.Logf("\n%q\n", string(test.frontmatter))
310-
t.Errorf("Frontmatter did not match:\nexp: %q\ngot: %q", string(test.extracted), fm)
313+
t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot: %q", i, string(test.extracted), fm)
311314
}
312315
}
313316
}

0 commit comments

Comments
 (0)