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
/
img.go
210 lines (191 loc) · 4.66 KB
/
img.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
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
package parser
import (
"github.com/bouncepaw/mycomarkup/v3/blocks"
"github.com/bouncepaw/mycomarkup/v3/links"
"github.com/bouncepaw/mycomarkup/v3/mycocontext"
"regexp"
"strings"
)
var imgRe = regexp.MustCompile(`^img\s*[a-z\s]*{`)
func matchesImg(ctx mycocontext.Context) bool {
return imgRe.Match(ctx.Input().Bytes())
}
func nextImg(ctx mycocontext.Context) (img blocks.Img, eof bool) {
img = parseImgUntilCurlyBrace(ctx)
for {
imgEntry, found, imgFinished := nextImgEntry(ctx)
if found {
img.Entries = append(img.Entries, imgEntry)
}
if imgFinished {
break
}
}
return img, mycocontext.IsEof(ctx)
}
type imgEntryParsingState int
const (
imgEntryOnStart imgEntryParsingState = iota
imgEntryCollectingTarget
imgEntryCollectingDimensionWidth
imgEntryCollectingDimensionHeight
)
func nextImgEntryDescription(ctx mycocontext.Context) string {
var (
r rune
eof bool
curlyBracesOpen = 0
res strings.Builder
)
for {
r, eof = mycocontext.NextRune(ctx)
if eof {
return res.String()
}
switch r {
case '{':
curlyBracesOpen++
res.WriteRune('{')
case '}':
if curlyBracesOpen == 0 {
return res.String()
}
if curlyBracesOpen > 0 {
curlyBracesOpen--
}
res.WriteRune('{')
default: // Including \n
res.WriteRune(r)
}
}
}
func nextImgEntry(ctx mycocontext.Context) (
imgEntry blocks.ImgEntry,
entryFound bool, // true if an entry was found
imgDone bool, // true if final img } found
) {
var (
r rune
eof bool
state = imgEntryOnStart
target, width, height strings.Builder
descbuf string
)
entryFound = true
runewalker:
for {
r, eof = mycocontext.NextRune(ctx)
if eof {
imgDone = true // Just to be sure
break
}
runechecker: // TODO: add escaping \
switch state {
case imgEntryOnStart:
switch r {
case '}':
entryFound, imgDone = false, true
_, _ = mycocontext.NextLine(ctx) // After closing }
break runewalker
case '\n':
entryFound, imgDone = false, false
break runewalker
case ' ', '\t': // Ignore the leading whitespace
case '|': // Empty target, so it seems. This entry becomes invalid.
entryFound = false
state = imgEntryCollectingDimensionWidth
default:
state = imgEntryCollectingTarget
goto runechecker // uwu
}
case imgEntryCollectingTarget:
switch r {
case '}':
entryFound, imgDone = true, true
break runewalker
case '\n':
entryFound, imgDone = true, false
break runewalker
case '|':
state = imgEntryCollectingDimensionWidth
case '{':
descbuf = nextImgEntryDescription(ctx)
break runewalker
default:
// I am confident in myself, thus I ignore errors
_, _ = target.WriteRune(r)
}
case imgEntryCollectingDimensionWidth:
switch r {
case '}':
entryFound, imgDone = true, true
break runewalker
case '\n':
entryFound, imgDone = true, false
break runewalker
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
_, _ = width.WriteRune(r)
case '*':
state = imgEntryCollectingDimensionHeight
case '{':
descbuf = nextImgEntryDescription(ctx)
break runewalker
default: // Ignore the garbage!
}
case imgEntryCollectingDimensionHeight:
switch r {
case '}':
entryFound, imgDone = true, true
break runewalker
case '\n':
entryFound, imgDone = true, false
break runewalker
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
_, _ = height.WriteRune(r)
case '{':
descbuf = nextImgEntryDescription(ctx)
break runewalker
default: // Ignore the garbage!
}
default:
panic("warning warning warning!!!!!!!!")
}
}
return blocks.NewImgEntry(
links.From(target.String(), "", ctx.HyphaName()),
ctx.HyphaName(),
width.String(),
height.String(),
descbuf),
entryFound,
imgDone
}
// Call this function if and only if matchesImg(ctx) == true.
func parseImgUntilCurlyBrace(ctx mycocontext.Context) (img blocks.Img) {
// Input:
// img<attr>{<rest...>
// Read img first. Sorry for party rocking 😎
_, _ = mycocontext.NextRune(ctx)
_, _ = mycocontext.NextRune(ctx)
_, _ = mycocontext.NextRune(ctx)
var attr strings.Builder
for {
// It must be safe to ignore the error as long as parseImgUntilCurlyBrace is called correctly.
r, _ := mycocontext.NextRune(ctx)
if r == '{' {
break
}
_, _ = attr.WriteRune(r)
}
return blocks.NewImg(make([]blocks.ImgEntry, 0), parseImgLayout(attr.String()))
}
func parseImgLayout(attr string) blocks.ImgLayout {
switch {
case strings.Contains(attr, "side"):
return blocks.ImgLayoutSide
case strings.Contains(attr, "grid"):
return blocks.ImgLayoutGrid
default:
return blocks.ImgLayoutNormal
}
}