summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gomarkdown/markdown/parser/aside.go
blob: 9d02ed04902d9f263b7cf29a87e86191bbd98d93 (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 IsEmpty(data[beg:]) <= 0 {
		return false
	}
	if end >= len(data) {
		return true
	}
	return p.asidePrefix(data[end:]) == 0 && 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
}