aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vendor/rsc.io/markdown/list.go
blob: 8b9fcfe42e923c450be1b30298eb661edd615a75 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2021 The Go 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 (
	"bytes"
	"fmt"
	"strings"
)

type List struct {
	Position
	Bullet rune
	Start  int
	Loose  bool
	Items  []Block // always *Item
}

type Item struct {
	Position
	Blocks []Block
	width  int
}

func (b *List) PrintHTML(buf *bytes.Buffer) {
	if b.Bullet == '.' || b.Bullet == ')' {
		buf.WriteString("<ol")
		if b.Start != 1 {
			fmt.Fprintf(buf, " start=\"%d\"", b.Start)
		}
		buf.WriteString(">\n")
	} else {
		buf.WriteString("<ul>\n")
	}
	for _, c := range b.Items {
		c.PrintHTML(buf)
	}
	if b.Bullet == '.' || b.Bullet == ')' {
		buf.WriteString("</ol>\n")
	} else {
		buf.WriteString("</ul>\n")
	}
}

func (b *List) printMarkdown(buf *bytes.Buffer, s mdState) {
	if buf.Len() > 0 && buf.Bytes()[buf.Len()-1] != '\n' {
		buf.WriteByte('\n')
	}
	s.bullet = b.Bullet
	s.num = b.Start
	for i, item := range b.Items {
		if i > 0 && b.Loose {
			buf.WriteByte('\n')
		}
		item.printMarkdown(buf, s)
		s.num++
	}
}

func (b *Item) printMarkdown(buf *bytes.Buffer, s mdState) {
	var marker string
	if s.bullet == '.' || s.bullet == ')' {
		marker = fmt.Sprintf("%d%c ", s.num, s.bullet)
	} else {
		marker = fmt.Sprintf("%c ", s.bullet)
	}
	marker = strings.Repeat(" ", b.width-len(marker)) + marker
	s.prefix1 = s.prefix + marker
	s.prefix += strings.Repeat(" ", len(marker))
	printMarkdownBlocks(b.Blocks, buf, s)
}

func (b *Item) PrintHTML(buf *bytes.Buffer) {
	buf.WriteString("<li>")
	if len(b.Blocks) > 0 {
		if _, ok := b.Blocks[0].(*Text); !ok {
			buf.WriteString("\n")
		}
	}
	for i, c := range b.Blocks {
		c.PrintHTML(buf)
		if i+1 < len(b.Blocks) {
			if _, ok := c.(*Text); ok {
				buf.WriteString("\n")
			}
		}
	}
	buf.WriteString("</li>\n")
}

type listBuilder struct {
	bullet rune
	num    int
	loose  bool
	item   *itemBuilder
	todo   func() line
}

func (b *listBuilder) build(p buildState) Block {
	blocks := p.blocks()
	pos := p.pos()

	// list can have wrong pos b/c extend dance.
	pos.EndLine = blocks[len(blocks)-1].Pos().EndLine
Loose:
	for i, c := range blocks {
		c := c.(*Item)
		if i+1 < len(blocks) {
			if blocks[i+1].Pos().StartLine-c.EndLine > 1 {
				b.loose = true
				break Loose
			}
		}
		for j, d := range c.Blocks {
			endLine := d.Pos().EndLine
			if j+1 < len(c.Blocks) {
				if c.Blocks[j+1].Pos().StartLine-endLine > 1 {
					b.loose = true
					break Loose
				}
			}
		}
	}

	if !b.loose {
		for _, c := range blocks {
			c := c.(*Item)
			for i, d := range c.Blocks {
				if p, ok := d.(*Paragraph); ok {
					c.Blocks[i] = p.Text
				}
			}
		}
	}

	return &List{
		pos,
		b.bullet,
		b.num,
		b.loose,
		p.blocks(),
	}
}

func (b *itemBuilder) build(p buildState) Block {
	b.list.item = nil
	return &Item{p.pos(), p.blocks(), b.width}
}

func (c *listBuilder) extend(p *parseState, s line) (line, bool) {
	d := c.item
	if d != nil && s.trimSpace(d.width, d.width, true) || d == nil && s.isBlank() {
		return s, true
	}
	return s, false
}

func (c *itemBuilder) extend(p *parseState, s line) (line, bool) {
	if s.isBlank() && !c.haveContent {
		return s, false
	}
	if s.isBlank() {
		// Goldmark does this and apparently commonmark.js too.
		// Not sure why it is necessary.
		return line{}, true
	}
	if !s.isBlank() {
		c.haveContent = true
	}
	return s, true
}

func newListItem(p *parseState, s line) (line, bool) {
	if list, ok := p.curB().(*listBuilder); ok && list.todo != nil {
		s = list.todo()
		list.todo = nil
		return s, true
	}
	if p.startListItem(&s) {
		return s, true
	}
	return s, false
}

func (p *parseState) startListItem(s *line) bool {
	t := *s
	n := 0
	for i := 0; i < 3; i++ {
		if !t.trimSpace(1, 1, false) {
			break
		}
		n++
	}
	bullet := t.peek()
	var num int
Switch:
	switch bullet {
	default:
		return false
	case '-', '*', '+':
		t.trim(bullet)
		n++
	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
		for j := t.i; ; j++ {
			if j >= len(t.text) {
				return false
			}
			c := t.text[j]
			if c == '.' || c == ')' {
				// success
				bullet = c
				j++
				n += j - t.i
				t.i = j
				break Switch
			}
			if c < '0' || '9' < c {
				return false
			}
			if j-t.i >= 9 {
				return false
			}
			num = num*10 + int(c) - '0'
		}

	}
	if !t.trimSpace(1, 1, true) {
		return false
	}
	n++
	tt := t
	m := 0
	for i := 0; i < 3 && tt.trimSpace(1, 1, false); i++ {
		m++
	}
	if !tt.trimSpace(1, 1, true) {
		n += m
		t = tt
	}

	// point of no return

	var list *listBuilder
	if c, ok := p.nextB().(*listBuilder); ok {
		list = c
	}
	if list == nil || list.bullet != rune(bullet) {
		// “When the first list item in a list interrupts a paragraph—that is,
		// when it starts on a line that would otherwise count as
		// paragraph continuation text—then (a) the lines Ls must
		// not begin with a blank line,
		// and (b) if the list item is ordered, the start number must be 1.”
		if list == nil && p.para() != nil && (t.isBlank() || (bullet == '.' || bullet == ')') && num != 1) {
			// Goldmark and Dingus both seem to get this wrong
			// (or the words above don't mean what we think they do).
			// when the paragraph that could be continued
			// is inside a block quote.
			// See testdata/extra.txt 117.md.
			p.corner = true
			return false
		}
		list = &listBuilder{bullet: rune(bullet), num: num}
		p.addBlock(list)
	}
	b := &itemBuilder{list: list, width: n, haveContent: !t.isBlank()}
	list.todo = func() line {
		p.addBlock(b)
		list.item = b
		return t
	}
	return true
}

// GitHub task list extension

func (p *parseState) taskList(list *List) {
	for _, item := range list.Items {
		item := item.(*Item)
		if len(item.Blocks) == 0 {
			continue
		}
		var text *Text
		switch b := item.Blocks[0].(type) {
		default:
			continue
		case *Paragraph:
			text = b.Text
		case *Text:
			text = b
		}
		if len(text.Inline) < 1 {
			continue
		}
		pl, ok := text.Inline[0].(*Plain)
		if !ok {
			continue
		}
		s := pl.Text
		if len(s) < 4 || s[0] != '[' || s[2] != ']' || (s[1] != ' ' && s[1] != 'x' && s[1] != 'X') {
			continue
		}
		if s[3] != ' ' && s[3] != '\t' {
			p.corner = true // goldmark does not require the space
			continue
		}
		text.Inline = append([]Inline{&Task{Checked: s[1] == 'x' || s[1] == 'X'},
			&Plain{Text: s[len("[x]"):]}}, text.Inline[1:]...)
	}
}

func ins(first Inline, x []Inline) []Inline {
	x = append(x, nil)
	copy(x[1:], x)
	x[0] = first
	return x
}

type Task struct {
	Checked bool
}

func (x *Task) Inline() {
}

func (x *Task) PrintHTML(buf *bytes.Buffer) {
	buf.WriteString("<input ")
	if x.Checked {
		buf.WriteString(`checked="" `)
	}
	buf.WriteString(`disabled="" type="checkbox">`)
}

func (x *Task) printMarkdown(buf *bytes.Buffer) {
	x.PrintText(buf)
}

func (x *Task) PrintText(buf *bytes.Buffer) {
	buf.WriteByte('[')
	if x.Checked {
		buf.WriteByte('x')
	} else {
		buf.WriteByte(' ')
	}
	buf.WriteByte(']')
	buf.WriteByte(' ')
}

func listCorner(list *List) bool {
	for _, item := range list.Items {
		item := item.(*Item)
		if len(item.Blocks) == 0 {
			// Goldmark mishandles what follows; see testdata/extra.txt 111.md.
			return true
		}
		switch item.Blocks[0].(type) {
		case *List, *ThematicBreak, *CodeBlock:
			// Goldmark mishandles a list with various block items inside it.
			return true
		}
	}
	return false
}