Fix include's address specification

Fixes #10

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben 2018-08-07 20:46:42 +01:00
parent 01a56b20f8
commit 074b4fa373
2 changed files with 35 additions and 4 deletions

View File

@ -15,7 +15,9 @@ func updateWd(cur, new string) string {
return path.Dir(filepath.Join(cur, new)) return path.Dir(filepath.Join(cur, new))
} }
// {{...}} // isInclude parses {{...}}[...], that contains a path between the {{, the [...] syntax contains
// 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) { func (p *Parser) isInclude(data []byte) (filename string, address []byte, consumed int) {
i := 0 i := 0
if len(data) < 3 { if len(data) < 3 {
@ -30,10 +32,24 @@ func (p *Parser) isInclude(data []byte) (filename string, address []byte, consum
if i+1 >= len(data) { if i+1 >= len(data) {
return "", nil, 0 return "", nil, 0
} }
if data[i+1] != '}' { end := i
i++
if data[i] != '}' {
return "", nil, 0 return "", nil, 0
} }
return string(data[2:i]), nil, i + 1
if i+1 < len(data) && data[i+1] == '[' { // potential address specification
i++
start := i + 1
i = skipUntilChar(data, start, ']')
if i >= len(data) {
return "", nil, 0
}
address = data[start:i]
}
return string(data[2:end]), address, i
} }
func (p *Parser) readInclude(file string, address []byte) []byte { func (p *Parser) readInclude(file string, address []byte) []byte {
@ -46,7 +62,7 @@ func (p *Parser) readInclude(file string, address []byte) []byte {
return nil return nil
} }
// <{{...}} // 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) { func (p *Parser) isCodeInclude(data []byte) (filename string, address []byte, consumed int) {
if len(data) < 3 { if len(data) < 3 {
return "", nil, 0 return "", nil, 0

View File

@ -25,4 +25,19 @@ func TestIsInclude(t *testing.T) {
if name, _, _ := p.isInclude([]byte("{foo}")); name != "" { if name, _, _ := p.isInclude([]byte("{foo}")); name != "" {
t.Errorf("want %s, got %s", "", name) t.Errorf("want %s, got %s", "", name)
} }
name, address, _ := p.isInclude([]byte("{{foo}}[a]"))
if name != "foo" {
t.Errorf("want %s, got %s", "foo", name)
}
if string(address) != "a" {
t.Errorf("want %s, got %s", "a", string(address))
}
name, address, _ = p.isInclude([]byte("{{foo}}[a"))
if name != "" {
t.Errorf("want %s, got %s", "", name)
}
if string(address) != "" {
t.Errorf("want %s, got %s", "", string(address))
}
} }