summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gomarkdown/markdown/parser/attribute.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/attribute.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/attribute.go')
-rw-r--r--vendor/github.com/gomarkdown/markdown/parser/attribute.go116
1 files changed, 116 insertions, 0 deletions
diff --git a/vendor/github.com/gomarkdown/markdown/parser/attribute.go b/vendor/github.com/gomarkdown/markdown/parser/attribute.go
new file mode 100644
index 00000000..5fdb0709
--- /dev/null
+++ b/vendor/github.com/gomarkdown/markdown/parser/attribute.go
@@ -0,0 +1,116 @@
+package parser
+
+import (
+ "bytes"
+
+ "github.com/gomarkdown/markdown/ast"
+)
+
+// attribute parses a (potential) block attribute and adds it to p.
+func (p *Parser) attribute(data []byte) []byte {
+ if len(data) < 3 {
+ return data
+ }
+ i := 0
+ if data[i] != '{' {
+ return data
+ }
+ i++
+
+ // last character must be a } otherwise it's not an attribute
+ end := skipUntilChar(data, i, '\n')
+ if data[end-1] != '}' {
+ return data
+ }
+
+ i = skipSpace(data, i)
+ b := &ast.Attribute{Attrs: make(map[string][]byte)}
+
+ esc := false
+ quote := false
+ trail := 0
+Loop:
+ for ; i < len(data); i++ {
+ switch data[i] {
+ case ' ', '\t', '\f', '\v':
+ if quote {
+ continue
+ }
+ chunk := data[trail+1 : i]
+ if len(chunk) == 0 {
+ trail = i
+ continue
+ }
+ switch {
+ case chunk[0] == '.':
+ b.Classes = append(b.Classes, chunk[1:])
+ case chunk[0] == '#':
+ b.ID = chunk[1:]
+ default:
+ k, v := keyValue(chunk)
+ if k != nil && v != nil {
+ b.Attrs[string(k)] = v
+ } else {
+ // this is illegal in an attribute
+ return data
+ }
+ }
+ trail = i
+ case '"':
+ if esc {
+ esc = !esc
+ continue
+ }
+ quote = !quote
+ case '\\':
+ esc = !esc
+ case '}':
+ if esc {
+ esc = !esc
+ continue
+ }
+ chunk := data[trail+1 : i]
+ if len(chunk) == 0 {
+ return data
+ }
+ switch {
+ case chunk[0] == '.':
+ b.Classes = append(b.Classes, chunk[1:])
+ case chunk[0] == '#':
+ b.ID = chunk[1:]
+ default:
+ k, v := keyValue(chunk)
+ if k != nil && v != nil {
+ b.Attrs[string(k)] = v
+ } else {
+ return data
+ }
+ }
+ i++
+ break Loop
+ default:
+ esc = false
+ }
+ }
+
+ p.attr = b
+ return data[i:]
+}
+
+// key="value" quotes are mandatory.
+func keyValue(data []byte) ([]byte, []byte) {
+ chunk := bytes.SplitN(data, []byte{'='}, 2)
+ if len(chunk) != 2 {
+ return nil, nil
+ }
+ key := chunk[0]
+ value := chunk[1]
+
+ if len(value) < 3 || len(key) == 0 {
+ return nil, nil
+ }
+ if value[0] != '"' || value[len(value)-1] != '"' {
+ return key, nil
+ }
+ return key, value[1 : len(value)-1]
+}