summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gomarkdown/markdown/parser/aside.go
blob: 96e25fe064ff7cecc588524fa01adb8eb085332d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package parser

import (
	"bytes"

	"github.com/gomarkdown/markdown/ast"
)

// returns aisde prefix length
func (p *Parser) asidePrefix(data []byte) int {
	i := 0
	n := len(data)
	for i < 3 && i < n && data[i] == ' ' {
		i++
	}
	if i+1 < n && data[i] == 'A' && data[i+1] == '>' {
		if i+2 < n && data[i+2] == ' ' {
			return i + 3
		}
		return i + 2
	}
	return 0
}

// aside ends with at least one blank line
// followed by something without a aside prefix
func (p *Parser) terminateAside(data []byte, beg, end int) bool {
	if p.isEmpty(data[beg:]) <= 0 {
		return false
	}
	if end >= len(data) {
		return true
	}
	return p.asidePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0
}

// parse a aside fragment
func (p *Parser) aside(data []byte) int {
	var raw bytes.Buffer
	beg, end := 0, 0
	// identical to quote
	for beg < len(data) {
		end = beg
		// Step over whole lines, collecting them. While doing that, check for
		// fenced code and if one's found, incorporate it altogether,
		// irregardless of any contents inside it
		for end < len(data) && data[end] != '\n' {
			if p.extensions&FencedCode != 0 {
				if i := p.fencedCodeBlock(data[end:], false); i > 0 {
					// -1 to compensate for the extra end++ after the loop:
					end += i - 1
					break
				}
			}
			end++
		}
		end = skipCharN(data, end, '\n', 1)
		if pre := p.asidePrefix(data[beg:]); pre > 0 {
			// skip the prefix
			beg += pre
		} else if p.terminateAside(data, beg, end) {
			break
		}
		// this line is part of the aside
		raw.Write(data[beg:end])
		beg = end
	}

	block := p.addBlock(&ast.Aside{})
	p.block(raw.Bytes())
	p.finalize(block)
	return end
}