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
155 lines (142 loc) · 3.3 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
146
147
148
149
150
151
152
153
154
155
package parser
import (
"bytes"
"context"
"github.com/bouncepaw/mycomarkup/blocks"
"github.com/bouncepaw/mycomarkup/util"
"sync"
)
// Call only if there is a list item on the line.
func nextList(ctx context.Context) (list blocks.List, eof bool) {
var contents []interface{}
list = blocks.List{
Items: make([]blocks.ListItem, 0),
}
for !eof {
marker, level, found := markerOnNextLine(ctx)
if !found {
break
}
util.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 context.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 = util.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 context.Context) (contents []interface{}, eof bool) {
// Parse the text as a separate mycodoc
var (
text bytes.Buffer
blocksCh = make(chan interface{})
blocks = make([]interface{}, 0)
wg sync.WaitGroup
)
text, eof = readNextListItemsContents(ctx)
wg.Add(1)
go func() {
Parse(context.WithValue(ctx, util.KeyInputBuffer, &text), blocksCh)
wg.Done()
}()
for block := range blocksCh {
blocks = append(blocks, block)
}
wg.Wait()
return blocks, eof
}
func looksLikeList(ctx context.Context) bool {
_, level, found := markerOnNextLine(ctx)
return found && level == 1
}
func markerOnNextLine(ctx context.Context) (m blocks.ListMarker, level uint, found bool) {
var (
onStart = true
onAsterisk = false
onSpecialCharacter = false
)
for _, b := range util.InputFrom(ctx).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
}