Skip to content

Commit

Permalink
Add IsReference
Browse files Browse the repository at this point in the history
We need to this when parsing HTMLBlock to *not* output the reference.
Pull it out in its own function.

Signed-off-by: Miek Gieben <[email protected]>
  • Loading branch information
miekg committed Nov 25, 2018
1 parent b94a5f4 commit d8d2394
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions mparser/bibliography.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,22 @@ func anchorFromReference(data []byte) []byte {

// ReferenceHook is the hook used to parse reference nodes.
func ReferenceHook(data []byte) (ast.Node, []byte, int) {
if !bytes.HasPrefix(data, []byte("<reference ")) {
ref, ok := IsReference(data)
if !ok {
return nil, nil, 0
}

node := &ast.HTMLBlock{}
node.Literal = fmtReference(ref)
return node, nil, len(ref)
}

// IfReference returns wether data contains a reference.
func IsReference(data []byte) ([]byte, bool) {
if !bytes.HasPrefix(data, []byte("<reference ")) {
return nil, false
}

i := 12
// scan for an end-of-reference marker, across lines if necessary
for i < len(data) &&
Expand All @@ -134,12 +146,9 @@ func ReferenceHook(data []byte) (ast.Node, []byte, int) {

// no end-of-reference marker
if i > len(data) {
return nil, nil, 0
return nil, false
}

node := &ast.HTMLBlock{}
node.Literal = fmtReference(data[:i])
return node, nil, i
return data[:i], true
}

func fmtReference(data []byte) []byte {
Expand Down

0 comments on commit d8d2394

Please sign in to comment.