summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gomarkdown/markdown/parser/citation.go
diff options
context:
space:
mode:
authorBenjamin <b.mpickford@outlook.com>2019-11-18 06:18:01 +1000
committerWim <wim@42.be>2019-11-17 21:18:01 +0100
commit0917dc876613fd71c9726a34bf0138b4f5121be9 (patch)
tree1eacaadaa4869e8f74ccf65c684c2a80f90787f9 /vendor/github.com/gomarkdown/markdown/parser/citation.go
parentaba86855b5f71c9809d892a7eebc6b69872fcd5b (diff)
downloadmatterbridge-msglm-0917dc876613fd71c9726a34bf0138b4f5121be9.tar.gz
matterbridge-msglm-0917dc876613fd71c9726a34bf0138b4f5121be9.tar.bz2
matterbridge-msglm-0917dc876613fd71c9726a34bf0138b4f5121be9.zip
Update markdown parsing library to github.com/gomarkdown/markdown (#944)
Diffstat (limited to 'vendor/github.com/gomarkdown/markdown/parser/citation.go')
-rw-r--r--vendor/github.com/gomarkdown/markdown/parser/citation.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/vendor/github.com/gomarkdown/markdown/parser/citation.go b/vendor/github.com/gomarkdown/markdown/parser/citation.go
new file mode 100644
index 00000000..8ea1fbee
--- /dev/null
+++ b/vendor/github.com/gomarkdown/markdown/parser/citation.go
@@ -0,0 +1,86 @@
+package parser
+
+import (
+ "bytes"
+
+ "github.com/gomarkdown/markdown/ast"
+)
+
+// citation parses a citation. In its most simple form [@ref], we allow multiple
+// being separated by semicolons and a sub reference inside ala pandoc: [@ref, p. 23].
+// Each citation can have a modifier: !, ? or - wich mean:
+//
+// ! - normative
+// ? - formative
+// - - suppressed
+//
+// The suffix starts after a comma, we strip any whitespace before and after. If the output
+// allows for it, this can be rendered.
+func citation(p *Parser, data []byte, offset int) (int, ast.Node) {
+ // look for the matching closing bracket
+ i := offset + 1
+ for level := 1; level > 0 && i < len(data); i++ {
+ switch {
+ case data[i] == '\n':
+ // no newlines allowed.
+ return 0, nil
+
+ case data[i-1] == '\\':
+ continue
+
+ case data[i] == '[':
+ level++
+
+ case data[i] == ']':
+ level--
+ if level <= 0 {
+ i-- // compensate for extra i++ in for loop
+ }
+ }
+ }
+
+ if i >= len(data) {
+ return 0, nil
+ }
+
+ node := &ast.Citation{}
+
+ citations := bytes.Split(data[1:i], []byte(";"))
+ for _, citation := range citations {
+ var suffix []byte
+ citation = bytes.TrimSpace(citation)
+ j := 0
+ if citation[j] != '@' {
+ // not a citation, drop out entirely.
+ return 0, nil
+ }
+ if c := bytes.Index(citation, []byte(",")); c > 0 {
+ part := citation[:c]
+ suff := citation[c+1:]
+ part = bytes.TrimSpace(part)
+ suff = bytes.TrimSpace(suff)
+
+ citation = part
+ suffix = suff
+ }
+
+ citeType := ast.CitationTypeInformative
+ j = 1
+ switch citation[j] {
+ case '!':
+ citeType = ast.CitationTypeNormative
+ j++
+ case '?':
+ citeType = ast.CitationTypeInformative
+ j++
+ case '-':
+ citeType = ast.CitationTypeSuppressed
+ j++
+ }
+ node.Destination = append(node.Destination, citation[j:])
+ node.Type = append(node.Type, citeType)
+ node.Suffix = append(node.Suffix, suffix)
+ }
+
+ return i + 1, node
+}