Merge pull request #67 from mmarkdown/include-allow-spaces

Include allow spaces
This commit is contained in:
Miek Gieben 2018-08-17 15:26:57 +01:00 committed by GitHub
commit c959a8b9ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View File

@ -10,13 +10,14 @@ import (
// an address to select which lines to include. It is treated as an opaque string and just given
// to readInclude.
func (p *Parser) isInclude(data []byte) (filename string, address []byte, consumed int) {
i := 0
if len(data) < 3 {
i := skipCharN(data, 0, ' ', 3) // start with up to 3 spaces
if len(data[i:]) < 3 {
return "", nil, 0
}
if data[i] != '{' || data[i+1] != '{' {
return "", nil, 0
}
start := i + 2
// find the end delimiter
i = skipUntilChar(data, i, '}')
@ -28,7 +29,7 @@ func (p *Parser) isInclude(data []byte) (filename string, address []byte, consum
if data[i] != '}' {
return "", nil, 0
}
filename = string(data[2:end])
filename = string(data[start:end])
if i+1 < len(data) && data[i+1] == '[' { // potential address specification
start := i + 2
@ -54,17 +55,20 @@ func (p *Parser) readInclude(from, file string, address []byte) []byte {
// isCodeInclude parses <{{...}} which is similar to isInclude the returned bytes are, however wrapped in a code block.
func (p *Parser) isCodeInclude(data []byte) (filename string, address []byte, consumed int) {
if len(data) < 3 {
i := skipCharN(data, 0, ' ', 3) // start with up to 3 spaces
if len(data[i:]) < 3 {
return "", nil, 0
}
if data[0] != '<' {
if data[i] != '<' {
return "", nil, 0
}
filename, address, consumed = p.isInclude(data[1:])
start := i
filename, address, consumed = p.isInclude(data[i+1:])
if consumed == 0 {
return "", nil, 0
}
return filename, address, consumed + 1
return filename, address, start + consumed + 1
}
// readCodeInclude acts like include except the returned bytes are wrapped in a fenced code block.

View File

@ -29,6 +29,10 @@ func TestIsInclude(t *testing.T) {
[]byte("{{foo}}a]"),
"foo", "", 7,
},
{
[]byte(" {{foo}}"),
"foo", "", 10,
},
// fails
{
[]byte("{foo}}"),
@ -74,6 +78,10 @@ func TestIsCodeInclude(t *testing.T) {
[]byte("<{{foo}} "),
"foo", "", 8,
},
{
[]byte(" <{{foo}} "),
"foo", "", 11,
},
}
p := New()