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

Commit

Permalink
Allow address specification in file include (#77)
Browse files Browse the repository at this point in the history
Use the code include address syntax also for file includes.

Fixes #66
  • Loading branch information
miekg committed May 7, 2016
1 parent e75e2d6 commit 99ab1dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions code.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ var SourceCodeTypes = map[string]bool{
"go": true,
}

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

textBytes, err := ioutil.ReadFile(string(file))
Expand Down
28 changes: 19 additions & 9 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package mmark

import (
"bytes"
"io/ioutil"
"path"
"unicode/utf8"
)
Expand Down Expand Up @@ -913,16 +912,27 @@ func (p *parser) include(out *bytes.Buffer, data []byte, depth int) int {
if j < 2 && end >= len(data) {
return 0
}
filename := data[i+2 : end-2]

name := string(data[i+2 : end-2])
input, err := ioutil.ReadFile(name)
if err != nil {
printf(p, "failed: `%s': %s", name, err)
return end
// Now a possible address in blockquotes
var address []byte
if end < len(data) && data[end] == '[' {
j = end
for j < len(data) && data[j] != ']' {
j++
}
if j == len(data) {
// assuming no address
address = nil
} else {
address = data[end+1 : j]
end = j + 1
}
}

if len(input) == 0 {
input = []byte{'\n'}
input := parseAddress(address, filename)
if input == nil {
return end
}
if input[len(input)-1] != '\n' {
input = append(input, '\n')
Expand Down Expand Up @@ -989,7 +999,7 @@ func (p *parser) codeInclude(out *bytes.Buffer, data []byte) int {
}
}

code := parseCode(address, filename)
code := parseAddress(address, filename)

if len(code) == 0 {
code = []byte{'\n'}
Expand Down

0 comments on commit 99ab1dc

Please sign in to comment.