From 67d6503d9a7a715a334ea56391f1990064451cd6 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 3 Aug 2018 09:23:22 +0100 Subject: [PATCH] add citations Signed-off-by: Miek Gieben --- ast/node.go | 18 ++++++++++++++++++ parser/citation.go | 1 + parser/inline.go | 15 ++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 parser/citation.go diff --git a/ast/node.go b/ast/node.go index fff74b0..d7db43a 100644 --- a/ast/node.go +++ b/ast/node.go @@ -39,6 +39,16 @@ const ( DocumentMatterBack ) +// CitationTypes holds the type of a citation, informative, normative or suppressed +type CitationTypes int + +const ( + CitationTypeNone CitationTypes = iota + CitationTypeSuppressed + CitationTypeInformative + CitationTypeNormative +) + // Node defines an ast node type Node interface { AsContainer() *Container @@ -240,6 +250,14 @@ type CrossReference struct { Destination []byte // Destination is where the reference points to } +// Citation is a citation node. +type Citation struct { + Container + + Destination [][]byte // Destination is where the citation points to. Multiple ones are allowed. + Type []CitationTypes // 1:1 mapping of destination and citation type +} + // Image represents markdown image node type Image struct { Container diff --git a/parser/citation.go b/parser/citation.go new file mode 100644 index 0000000..0bfe2c2 --- /dev/null +++ b/parser/citation.go @@ -0,0 +1 @@ +package parser diff --git a/parser/inline.go b/parser/inline.go index de6ebfc..b04c2a9 100644 --- a/parser/inline.go +++ b/parser/inline.go @@ -177,6 +177,7 @@ const ( linkImg linkDeferredFootnote linkInlineFootnote + linkCitation ) func isReferenceStyleLink(data []byte, pos int, t linkType) bool { @@ -200,7 +201,7 @@ func maybeInlineFootnote(p *Parser, data []byte, offset int) (int, ast.Node) { return 0, nil } -// '[': parse a link or an image or a footnote +// '[': parse a link or an image or a footnote or a citation func link(p *Parser, data []byte, offset int) (int, ast.Node) { // no links allowed inside regular links, footnote, and deferred footnotes if p.insideLink && (offset > 0 && data[offset-1] == '[' || len(data)-1 > offset && data[offset+1] == '^') { @@ -226,6 +227,14 @@ func link(p *Parser, data []byte, offset int) (int, ast.Node) { } else if len(data)-1 > offset && data[offset+1] == '^' { t = linkDeferredFootnote } + // [@citation], [-@citation] support + case p.extensions&Mmark != 0: + if len(data)-1 > offset && data[offset+1] == '@' { + t = linkCitation + } + if len(data)-2 > offset && data[offset+1] == '-' && data[offset+2] == '@' { + t = linkCitation + } // [text] == regular link default: t = linkNormal @@ -233,6 +242,10 @@ func link(p *Parser, data []byte, offset int) (int, ast.Node) { data = data[offset:] + if t == linkCitation { + return citation(p, data, 0) + } + var ( i = 1 noteID int