fix codeblock ending

This commit is contained in:
Erik Winter 2022-03-29 06:53:36 +02:00
parent 496d9a21d8
commit e1d454085a
3 changed files with 54 additions and 4 deletions

View File

@ -30,16 +30,16 @@ func NewCodeBlockFromTokens(p ReadUnreader) (ParseResult, bool) {
return ParseResult{}, false return ParseResult{}, false
} }
for { for {
ntoks, ok := p.Read(1) ntoks, ok := p.Read(2)
if !ok { if !ok {
p.Unread(len(toks)) p.Unread(len(toks))
return ParseResult{}, false return ParseResult{}, false
} }
tok := ntoks[0] if ntoks[0].Equal(delimiter) && (ntoks[1].Type == token.TYPE_NEWLINE || ntoks[1].Equal(token.TOKEN_EOF)) {
if tok.Equal(delimiter) {
break break
} }
toks = append(toks, tok) p.Unread(1)
toks = append(toks, ntoks[0])
} }
cb := CodeBlock{} cb := CodeBlock{}

View File

@ -42,6 +42,20 @@ more
}}, }},
}, },
}, },
{
name: "with newline at end",
input: `----
code
----
`,
exp: &adoc.ADoc{
Attributes: map[string]string{},
Content: []element.Element{element.CodeBlock{
element.Word("code"),
element.WhiteSpace("\n"),
}},
},
},
{ {
name: "missing end", name: "missing end",
input: `---- input: `----

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"ewintr.nl/adoc" "ewintr.nl/adoc"
"ewintr.nl/adoc/element"
"ewintr.nl/adoc/parser" "ewintr.nl/adoc/parser"
"ewintr.nl/go-kit/test" "ewintr.nl/go-kit/test"
) )
@ -19,6 +20,41 @@ func TestParser(t *testing.T) {
name: "empty", name: "empty",
exp: adoc.New(), exp: adoc.New(),
}, },
{
name: "codeblock paragraph edge",
input: `= some title
----
a code block
----
And then some text`,
exp: &adoc.ADoc{
Title: "some title",
Attributes: map[string]string{},
Content: []element.Element{
element.CodeBlock{
element.Word("a"),
element.WhiteSpace(" "),
element.Word("code"),
element.WhiteSpace(" "),
element.Word("block"),
element.WhiteSpace("\n"),
},
element.Paragraph{
Elements: []element.Element{
element.Word("And"),
element.WhiteSpace(" "),
element.Word("then"),
element.WhiteSpace(" "),
element.Word("some"),
element.WhiteSpace(" "),
element.Word("text"),
},
},
},
},
},
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
par := parser.New(strings.NewReader(tc.input)) par := parser.New(strings.NewReader(tc.input))