This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.go
145 lines (132 loc) · 3.16 KB
/
list.go
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
package parser
import (
"bytes"
"github.com/bouncepaw/mycomarkup/blocks"
"github.com/bouncepaw/mycomarkup/mycocontext"
)
// Call only if there is a list item on the line.
func nextList(ctx mycocontext.Context) (list blocks.List, eof bool) {
var contents []blocks.Block
list = blocks.List{
Items: make([]blocks.ListItem, 0),
}
for !eof {
marker, level, found := markerOnNextLine(ctx)
if !found {
break
}
mycocontext.EatUntilSpace(ctx)
contents, eof = nextListItem(ctx)
item := blocks.ListItem{
Marker: marker,
Level: level,
Contents: contents,
}
list.Items = append(list.Items, item)
}
list.Marker = list.Items[0].Marker // There should be at least one item!
return list, eof
}
func readNextListItemsContents(ctx mycocontext.Context) (text bytes.Buffer, eof bool) {
var (
onNewLine = true
escaping = false
curlyBracesOpen = 0
b byte
)
walker: // Read all item's contents
for !eof {
b, eof = mycocontext.NextByte(ctx)
stateMachine: // I'm extremely sorry
switch {
case onNewLine && b != ' ':
onNewLine = false
goto stateMachine
case onNewLine: // We just ignore spaces on line beginnings
case escaping:
escaping = false
if b == '\n' && curlyBracesOpen == 0 {
break walker
}
text.WriteByte(b)
case b == '\\':
escaping = true
text.WriteByte('\\')
case b == '{':
if curlyBracesOpen > 0 {
text.WriteByte('{')
}
curlyBracesOpen++
case b == '}':
if curlyBracesOpen != 1 {
text.WriteByte('}')
}
if curlyBracesOpen >= 0 {
curlyBracesOpen--
}
case b == '\n' && curlyBracesOpen == 0:
break walker
case b == '\n':
text.WriteByte(b)
onNewLine = true
default:
text.WriteByte(b)
}
}
return text, eof
}
func nextListItem(ctx mycocontext.Context) (contents []blocks.Block, eof bool) {
// Parse the text as a separate mycodoc
var (
text bytes.Buffer
ast = make([]blocks.Block, 0)
)
text, eof = readNextListItemsContents(ctx)
parseSubdocumentForEachBlock(ctx, &text, func(block blocks.Block) {
ast = append(ast, block)
})
return ast, eof
}
func looksLikeList(ctx mycocontext.Context) bool {
_, level, found := markerOnNextLine(ctx)
return found && level == 1
}
func markerOnNextLine(ctx mycocontext.Context) (m blocks.ListMarker, level uint, found bool) {
var (
onStart = true
onAsterisk = false
onSpecialCharacter = false
)
for _, b := range ctx.Input().Bytes() {
switch {
case onStart && b != '*':
return blocks.MarkerUnordered, 0, false
case onStart:
level = 1
onStart = false
onAsterisk = true
case onAsterisk && b == '*':
level++
case onAsterisk && b == ' ':
return m, level, true
case onAsterisk && (b == 'v' || b == 'x' || b == '.'):
onAsterisk = false
onSpecialCharacter = true
switch b {
case 'v':
m = blocks.MarkerTodoDone
case 'x':
m = blocks.MarkerTodo
case '.':
m = blocks.MarkerOrdered
}
case onAsterisk:
return blocks.MarkerUnordered, 0, false
case onSpecialCharacter && b != ' ':
return blocks.MarkerUnordered, 0, false
case onSpecialCharacter:
return m, level, true
}
}
return blocks.MarkerUnordered, 0, false
}