Skip to content
This repository was archived by the owner on Nov 10, 2020. It is now read-only.

Commit 99ab1dc

Browse files
committed
Allow address specification in file include (#77)
Use the code include address syntax also for file includes. Fixes #66
1 parent e75e2d6 commit 99ab1dc

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

code.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ var SourceCodeTypes = map[string]bool{
3939
"go": true,
4040
}
4141

42-
// parseCode parses a code address directive.
43-
func parseCode(addr []byte, file []byte) []byte {
42+
// parseAddress parses a code address directive and returns the bytes.
43+
func parseAddress(addr []byte, file []byte) []byte {
4444
bytes.TrimSpace(addr)
4545

4646
textBytes, err := ioutil.ReadFile(string(file))

markdown.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package mmark
44

55
import (
66
"bytes"
7-
"io/ioutil"
87
"path"
98
"unicode/utf8"
109
)
@@ -913,16 +912,27 @@ func (p *parser) include(out *bytes.Buffer, data []byte, depth int) int {
913912
if j < 2 && end >= len(data) {
914913
return 0
915914
}
915+
filename := data[i+2 : end-2]
916916

917-
name := string(data[i+2 : end-2])
918-
input, err := ioutil.ReadFile(name)
919-
if err != nil {
920-
printf(p, "failed: `%s': %s", name, err)
921-
return end
917+
// Now a possible address in blockquotes
918+
var address []byte
919+
if end < len(data) && data[end] == '[' {
920+
j = end
921+
for j < len(data) && data[j] != ']' {
922+
j++
923+
}
924+
if j == len(data) {
925+
// assuming no address
926+
address = nil
927+
} else {
928+
address = data[end+1 : j]
929+
end = j + 1
930+
}
922931
}
923932

924-
if len(input) == 0 {
925-
input = []byte{'\n'}
933+
input := parseAddress(address, filename)
934+
if input == nil {
935+
return end
926936
}
927937
if input[len(input)-1] != '\n' {
928938
input = append(input, '\n')
@@ -989,7 +999,7 @@ func (p *parser) codeInclude(out *bytes.Buffer, data []byte) int {
989999
}
9901000
}
9911001

992-
code := parseCode(address, filename)
1002+
code := parseAddress(address, filename)
9931003

9941004
if len(code) == 0 {
9951005
code = []byte{'\n'}

0 commit comments

Comments
 (0)