mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 09:52:10 -05:00
parser: Fix handling of quoted brackets in JSON front matter
Also remove a broken JSON test. Fixes #3511
This commit is contained in:
parent
1a282ee432
commit
3183b9a29d
3 changed files with 17 additions and 23 deletions
|
@ -79,21 +79,7 @@ Leading
|
||||||
|
|
||||||
Content of the file goes Here
|
Content of the file goes Here
|
||||||
`
|
`
|
||||||
simplePageJSONLoose = `
|
|
||||||
{
|
|
||||||
"title": "spf13-vim 3.0 release and new website"
|
|
||||||
"description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim."
|
|
||||||
"tags": [ ".vimrc", "plugins", "spf13-vim", "VIm" ]
|
|
||||||
"date": "2012-04-06"
|
|
||||||
"categories": [
|
|
||||||
"Development"
|
|
||||||
"VIM"
|
|
||||||
],
|
|
||||||
"slug": "spf13-vim-3-0-release-and-new-website"
|
|
||||||
}
|
|
||||||
|
|
||||||
Content of the file goes Here
|
|
||||||
`
|
|
||||||
simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
|
simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
|
||||||
simplePageJSONMultiple = `
|
simplePageJSONMultiple = `
|
||||||
{
|
{
|
||||||
|
@ -941,16 +927,15 @@ func TestCreatePage(t *testing.T) {
|
||||||
r string
|
r string
|
||||||
}{
|
}{
|
||||||
{simplePageJSON},
|
{simplePageJSON},
|
||||||
{simplePageJSONLoose},
|
|
||||||
{simplePageJSONMultiple},
|
{simplePageJSONMultiple},
|
||||||
//{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)},
|
//{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for i, test := range tests {
|
||||||
s := newTestSite(t)
|
s := newTestSite(t)
|
||||||
p, _ := s.NewPage("page")
|
p, _ := s.NewPage("page")
|
||||||
if _, err := p.ReadFrom(strings.NewReader(test.r)); err != nil {
|
if _, err := p.ReadFrom(strings.NewReader(test.r)); err != nil {
|
||||||
t.Errorf("Unable to parse page: %s", err)
|
t.Fatalf("[%d] Unable to parse page: %s", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -304,8 +304,8 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
level int
|
level int
|
||||||
sameDelim = bytes.Equal(left, right)
|
sameDelim = bytes.Equal(left, right)
|
||||||
|
inQuote bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// Frontmatter must start with a delimiter. To check it first,
|
// Frontmatter must start with a delimiter. To check it first,
|
||||||
// pre-reads beginning delimiter length - 1 bytes from Reader
|
// pre-reads beginning delimiter length - 1 bytes from Reader
|
||||||
for i := 0; i < len(left)-1; i++ {
|
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 {
|
switch c {
|
||||||
|
case '"':
|
||||||
|
inQuote = !inQuote
|
||||||
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) && (buf.Len() == len(left) || buf.Bytes()[buf.Len()-len(left)-1] == '\n') {
|
if bytes.HasSuffix(buf.Bytes(), left) && (buf.Len() == len(left) || buf.Bytes()[buf.Len()-len(left)-1] == '\n') {
|
||||||
|
@ -373,11 +375,15 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else { // JSON case
|
} else { // JSON case
|
||||||
|
if !inQuote {
|
||||||
level++
|
level++
|
||||||
}
|
}
|
||||||
|
}
|
||||||
case right[len(right)-1]: // JSON case only reaches here
|
case right[len(right)-1]: // JSON case only reaches here
|
||||||
|
if !inQuote {
|
||||||
level--
|
level--
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if level == 0 {
|
if level == 0 {
|
||||||
// Consumes white spaces immediately behind frontmatter
|
// Consumes white spaces immediately behind frontmatter
|
||||||
|
|
|
@ -296,18 +296,21 @@ func TestExtractFrontMatterDelim(t *testing.T) {
|
||||||
{"{\n{\n}\n}\n", "{\n{\n}\n}", noErrExpected},
|
{"{\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},
|
||||||
{"{\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("}"))
|
fm, err := extractFrontMatterDelims(bufio.NewReader(strings.NewReader(test.frontmatter)), []byte("{"), []byte("}"))
|
||||||
if (err == nil) != test.errIsNil {
|
if (err == nil) != test.errIsNil {
|
||||||
t.Logf("\n%q\n", string(test.frontmatter))
|
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
|
continue
|
||||||
}
|
}
|
||||||
if !bytes.Equal(fm, []byte(test.extracted)) {
|
if !bytes.Equal(fm, []byte(test.extracted)) {
|
||||||
t.Logf("\n%q\n", string(test.frontmatter))
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue