summaryrefslogtreecommitdiffstats
path: root/vendor/gitlab.com/golang-commonmark/markdown/list.go
blob: 3b2e02303b7bc8532d2726e9c8dfab3b8c71486e (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// 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

import "strconv"

var listTerminatedBy []BlockRule

func skipBulletListMarker(s *StateBlock, startLine int) int {
	pos := s.BMarks[startLine] + s.TShift[startLine]
	max := s.EMarks[startLine]
	src := s.Src

	if pos >= max {
		return -1
	}

	marker := src[pos]
	if marker != '*' && marker != '-' && marker != '+' {
		return -1
	}
	pos++

	if pos < max && !byteIsSpace(src[pos]) {
		return -1
	}

	return pos
}

func skipOrderedListMarker(s *StateBlock, startLine int) int {
	start := s.BMarks[startLine] + s.TShift[startLine]
	pos := start
	max := s.EMarks[startLine]

	if pos+1 >= max {
		return -1
	}

	src := s.Src
	ch := src[pos]
	if ch < '0' || ch > '9' {
		return -1
	}
	pos++

	for {
		if pos >= max {
			return -1
		}

		ch = src[pos]
		pos++

		if ch >= '0' && ch <= '9' {
			if pos-start >= 10 {
				return -1
			}
			continue
		}

		if ch == ')' || ch == '.' {
			break
		}

		return -1
	}

	if pos < max && !byteIsSpace(src[pos]) {
		return -1
	}

	return pos
}

func markParagraphsTight(s *StateBlock, idx int) {
	level := s.Level + 2
	tokens := s.Tokens

	for i := idx + 2; i < len(tokens)-2; i++ {
		if tokens[i].Level() == level {
			if tok, ok := tokens[i].(*ParagraphOpen); ok {
				tok.Hidden = true
				i += 2
				tokens[i].(*ParagraphClose).Hidden = true
			}
		}
	}
}

func ruleList(s *StateBlock, startLine, endLine int, silent bool) bool {
	isTerminatingParagraph := false
	tight := true

	if s.SCount[startLine]-s.BlkIndent >= 4 {
		return false
	}
	src := s.Src

	if silent && s.ParentType == ptParagraph {
		if s.TShift[startLine] >= s.BlkIndent {
			isTerminatingParagraph = true
		}
	}

	var start int
	var markerValue int

	isOrdered := false
	posAfterMarker := skipOrderedListMarker(s, startLine)
	if posAfterMarker > 0 {
		isOrdered = true
		start = s.BMarks[startLine] + s.TShift[startLine]
		markerValue, _ = strconv.Atoi(src[start : posAfterMarker-1])

		if isTerminatingParagraph && markerValue != 1 {
			return false
		}
	} else {
		posAfterMarker = skipBulletListMarker(s, startLine)
		if posAfterMarker < 0 {
			return false
		}
	}

	if isTerminatingParagraph {
		if s.SkipSpaces(posAfterMarker) >= s.EMarks[startLine] {
			return false
		}
	}

	markerChar := src[posAfterMarker-1]

	if silent {
		return true
	}

	tokenIdx := len(s.Tokens)

	var listMap *[2]int
	if isOrdered {
		tok := &OrderedListOpen{
			Order: markerValue,
			Map:   [2]int{startLine, 0},
		}
		s.PushOpeningToken(tok)
		listMap = &tok.Map
	} else {
		tok := &BulletListOpen{
			Map: [2]int{startLine, 0},
		}
		s.PushOpeningToken(tok)
		listMap = &tok.Map
	}

	nextLine := startLine
	prevEmptyEnd := false

	oldParentType := s.ParentType
	s.ParentType = ptList

	var pos int
	var contentStart int

outer:
	for nextLine < endLine {
		pos = posAfterMarker
		max := s.EMarks[nextLine]

		initial := s.SCount[nextLine] + posAfterMarker - (s.BMarks[startLine] + s.TShift[startLine])
		offset := initial

	loop:
		for pos < max {
			switch src[pos] {
			case '\t':
				offset += 4 - (offset+s.BSCount[nextLine])%4
			case ' ':
				offset++
			default:
				break loop
			}
			pos++
		}

		contentStart = pos

		indentAfterMarker := 1
		if contentStart < max {
			if iam := offset - initial; iam <= 4 {
				indentAfterMarker = iam
			}
		}

		indent := initial + indentAfterMarker

		tok := &ListItemOpen{
			Map: [2]int{startLine, 0},
		}
		s.PushOpeningToken(tok)
		itemMap := &tok.Map

		oldIndent := s.BlkIndent
		oldTight := s.Tight
		oldTShift := s.TShift[startLine]
		oldLIndent := s.SCount[startLine]
		s.BlkIndent = indent
		s.Tight = true
		s.TShift[startLine] = contentStart - s.BMarks[startLine]
		s.SCount[startLine] = offset

		if contentStart >= max && s.IsLineEmpty(startLine+1) {
			s.Line = min(s.Line+2, endLine)
		} else {
			s.Md.Block.Tokenize(s, startLine, endLine)
		}

		if !s.Tight || prevEmptyEnd {
			tight = false
		}

		prevEmptyEnd = s.Line-startLine > 1 && s.IsLineEmpty(s.Line-1)

		s.BlkIndent = oldIndent
		s.TShift[startLine] = oldTShift
		s.SCount[startLine] = oldLIndent
		s.Tight = oldTight

		s.PushClosingToken(&ListItemClose{})

		startLine = s.Line
		nextLine = startLine
		(*itemMap)[1] = nextLine

		if nextLine >= endLine {
			break
		}

		contentStart = s.BMarks[startLine]

		if s.SCount[nextLine] < s.BlkIndent {
			break
		}

		for _, r := range listTerminatedBy {
			if r(s, nextLine, endLine, true) {
				break outer
			}
		}

		if isOrdered {
			posAfterMarker = skipOrderedListMarker(s, nextLine)
			if posAfterMarker < 0 {
				break
			}
		} else {
			posAfterMarker = skipBulletListMarker(s, nextLine)
			if posAfterMarker < 0 {
				break
			}
		}

		if markerChar != src[posAfterMarker-1] {
			break
		}
	}

	if isOrdered {
		s.PushClosingToken(&OrderedListClose{})
	} else {
		s.PushClosingToken(&BulletListClose{})
	}

	(*listMap)[1] = nextLine
	s.Line = nextLine
	s.ParentType = oldParentType

	if tight {
		markParagraphsTight(s, tokenIdx)
	}

	return true
}