summaryrefslogtreecommitdiffstats
path: root/vendor/gitlab.com/golang-commonmark/markdown/helpers.go
blob: d158dfcfbf01e8e8ab8887a1571bf12bcbfc6a94 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package markdown

func parseLinkLabel(s *StateInline, start int, disableNested bool) int {
	src := s.Src
	labelEnd := -1
	max := s.PosMax
	oldPos := s.Pos

	s.Pos = start + 1
	level := 1
	found := false

	for s.Pos < max {
		marker := src[s.Pos]

		if marker == ']' {
			level--
			if level == 0 {
				found = true
				break
			}
		}

		prevPos := s.Pos

		s.Md.Inline.SkipToken(s)

		if marker == '[' {
			if prevPos == s.Pos-1 {
				level++
			} else if disableNested {
				s.Pos = oldPos
				return -1
			}
		}
	}

	if found {
		labelEnd = s.Pos
	}

	s.Pos = oldPos

	return labelEnd
}

func parseLinkDestination(s string, pos, max int) (url string, lines, endpos int, ok bool) {
	start := pos
	if pos < max && s[pos] == '<' {
		pos++
		for pos < max {
			b := s[pos]
			if b == '\n' || byteIsSpace(b) {
				return
			}
			if b == '>' {
				endpos = pos + 1
				url = unescapeAll(s[start+1 : pos])
				ok = true
				return
			}
			if b == '\\' && pos+1 < max {
				pos += 2
				continue
			}

			pos++
		}

		return
	}

	level := 0
	for pos < max {
		b := s[pos]

		if b == ' ' {
			break
		}

		if b < 0x20 || b == 0x7f {
			break
		}

		if b == '\\' && pos+1 < max {
			pos += 2
			continue
		}

		if b == '(' {
			level++
		}

		if b == ')' {
			if level == 0 {
				break
			}
			level--
		}

		pos++
	}

	if start == pos {
		return
	}
	if level != 0 {
		return
	}

	url = unescapeAll(s[start:pos])
	endpos = pos
	ok = true

	return
}

func parseLinkTitle(s string, pos, max int) (title string, nlines, endpos int, ok bool) {
	lines := 0
	start := pos

	if pos >= max {
		return
	}

	marker := s[pos]

	if marker != '"' && marker != '\'' && marker != '(' {
		return
	}

	pos++

	if marker == '(' {
		marker = ')'
	}

	for pos < max {
		switch s[pos] {
		case marker:
			endpos = pos + 1
			nlines = lines
			title = unescapeAll(s[start+1 : pos])
			ok = true
			return
		case '\n':
			lines++
		case '\\':
			if pos+1 < max {
				pos++
				if s[pos] == '\n' {
					lines++
				}
			}
		}
		pos++
	}

	return
}