-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
aftertext.parsers
313 lines (294 loc) · 8.77 KB
/
aftertext.parsers
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
// Related work:
CSS is great for text selector ideas: https://www.w3schools.com/cssref/css_selectors.asp
Roff has a lot of related markup ideas: https://www.systutorials.com/docs/linux/man/7-groff_man/
abstractAftertextParser
description Text followed by markup commands.
extends abstractScrollParser
inScope abstractAftertextDirectiveParser abstractAftertextAttributeParser aftertextTagParser commentParser
javascript
get markupInserts() {
const { originalTextPostLinkify } = this
return this.filter(particle => particle.isMarkup)
.map(particle => particle.getInserts(originalTextPostLinkify))
.filter(i => i)
.flat()
}
get originalText() {
return this.content ?? ""
}
get originalTextPostLinkify() {
const { originalText } = this
const shouldLinkify = this.get("linkify") === "false" || originalText.includes("<a ") ? false : true
return shouldLinkify ? this.replaceNotes(Utils.linkify(originalText)) : originalText
}
replaceNotes(originalText) {
// Skip the replacements if there are no footnotes or the text has none.
if (!this.root.footnotes.length || !originalText.includes("^")) return originalText
this.root.footnotes.forEach((note, index) => {
const needle = note.firstAtom
const {linkBack} = note
if (originalText.includes(needle)) originalText = originalText.replace(new RegExp("\\" + needle + "\\b"), `<a href="#${note.anchorId}" class="scrollNoteLink" id="${linkBack}"><sup>${note.label}</sup></a>`)
})
return originalText
}
get text() {
const { originalTextPostLinkify, markupInserts } = this
let adjustment = 0
let newText = originalTextPostLinkify
markupInserts.sort((a, b) => {
if (a.index !== b.index)
return a.index - b.index
// If multiple tags start at same index, the tag that closes first should start last. Otherwise HTML breaks.
if (b.index === b.endIndex) // unless the endIndex is the same as index
return a.endIndex - b.endIndex
return b.endIndex - a.endIndex
})
markupInserts.forEach(insertion => {
insertion.index += adjustment
const consumeStartCharacters = insertion.consumeStartCharacters ?? 0
const consumeEndCharacters = insertion.consumeEndCharacters ?? 0
newText = newText.slice(0, insertion.index - consumeEndCharacters) + insertion.string + newText.slice(insertion.index + consumeStartCharacters)
adjustment += insertion.string.length - consumeEndCharacters - consumeStartCharacters
})
return newText
}
tag = "p"
get className() {
if (this.get("classes"))
return this.get("classes")
const classLine = this.getParticle("class")
if (classLine && classLine.applyToParentElement) return classLine.content
return this.defaultClassName
}
defaultClassName = "scrollParagraph"
get isHidden() {
return this.has("hidden")
}
compile(compileSettings) {
if (this.isHidden) return ""
this.compileSettings = compileSettings
const { className } = this
const classAttr = className ? `class="${this.className}"` : ""
const tag = this.get("tag") || this.tag
if (tag === "none") // Allow no tag for aftertext in tables
return this.text
return this.getHtmlRequirements(compileSettings) + `<${tag} ${this.divAttributes}${classAttr}>${this.text}</${tag}>`
}
get divAttributes() {
const attrs = this.filter(particle => particle.isAttribute)
return attrs.length ? attrs.map(particle => particle.divAttributes).join(" ") + " " : ""
}
abstractAftertextAttributeParser
atoms cueAtom
boolean isAttribute true
javascript
get divAttributes() {
return `${this.firstAtom}="${this.content}"`
}
compile() {
return ""
}
aftertextTagParser
atoms cueAtom htmlTagAtom
description Override the HTML tag that the compiled particle will use.
crux tag
javascript
compile() {
return ""
}
abstractAftertextDirectiveParser
atoms cueAtom
catchAllAtomType stringAtom
javascript
isMarkup = true
compile() {
return ""
}
getErrors() {
const errors = super.getErrors()
if (!this.isMarkup || this.matchWholeLine) return errors
const inserts = this.getInserts(this.parent.originalTextPostLinkify)
// todo: make AbstractParticleError class exported by sdk to allow Parsers to define their own error types.
// todo: also need to be able to map lines back to their line in source (pre-imports)
if (!inserts.length)
errors.push(this.makeError(`No match found for "${this.getLine()}".`))
return errors
}
get pattern() {
return this.getAtomsFrom(1).join(" ")
}
get shouldMatchAll() {
return this.has("matchAll")
}
getMatches(text) {
const { pattern } = this
const escapedPattern = pattern.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")
return [...text.matchAll(new RegExp(escapedPattern, "g"))].map(match => {
const { index } = match
const endIndex = index + pattern.length
return [
{ index, string: `<${this.openTag}${this.allAttributes}>`, endIndex },
{ index: endIndex, endIndex, string: `</${this.closeTag}>` }
]
})
}
getInserts(text) {
const matches = this.getMatches(text)
if (!matches.length) return false
if (this.shouldMatchAll) return matches.flat()
const match = this.getParticle("match")
if (match)
return match.indexes
.map(index => matches[index])
.filter(i => i)
.flat()
return matches[0]
}
get allAttributes() {
const attr = this.attributes.join(" ")
return attr ? " " + attr : ""
}
get attributes() {
return []
}
get openTag() {
return this.tag
}
get closeTag() {
return this.tag
}
abstractMarkupParser
extends abstractAftertextDirectiveParser
inScope abstractMarkupParameterParser
javascript
get matchWholeLine() {
return this.getAtomsFrom(this.patternStartsAtAtom).length === 0
}
get pattern() {
return this.matchWholeLine ? this.parent.originalText : this.getAtomsFrom(this.patternStartsAtAtom).join(" ")
}
patternStartsAtAtom = 1
abstractMarkupParameterParser
atoms cueAtom
cruxFromId
matchAllParser
popularity 0.000024
description Use this to match all occurrences of the text.
extends abstractMarkupParameterParser
matchParser
popularity 0.000048
catchAllAtomType integerAtom
description Use this to specify which index(es) to match.
javascript
get indexes() {
return this.getAtomsFrom(1).map(num => parseInt(num))
}
example
aftertext
hello ello ello
bold ello
match 0 2
extends abstractMarkupParameterParser
boldParser
popularity 0.000096
cruxFromId
description Bold matching text.
extends abstractMarkupParser
javascript
tag = "b"
italicsParser
popularity 0.000241
cruxFromId
description Italicize matching text.
extends abstractMarkupParser
javascript
tag = "i"
underlineParser
popularity 0.000024
description Underline matching text.
cruxFromId
extends abstractMarkupParser
javascript
tag = "u"
afterTextCenterParser
popularity 0.000193
description Center paragraph.
crux center
extends abstractMarkupParser
javascript
tag = "center"
aftertextCodeParser
popularity 0.000145
description Wrap matching text in code span.
crux code
extends abstractMarkupParser
javascript
tag = "code"
aftertextStrikeParser
popularity 0.000048
description Wrap matching text in s span.
crux strike
extends abstractMarkupParser
javascript
tag = "s"
abstractHtmlAttributeParser
javascript
compile() {
return ""
}
classMarkupParser
popularity 0.000772
description Add a custom class to the parent element instead. If matching text provided, a span with the class will be added around the matching text.
extends abstractMarkupParser
atoms cueAtom classNameAtom
crux class
javascript
tag = "span"
get applyToParentElement() {
return this.atoms.length === 2
}
getInserts(text) {
// If no select text is added, set the class on the parent element.
if (this.applyToParentElement) return []
return super.getInserts(text)
}
get className() {
return this.getAtom(1)
}
get attributes() {
return [`class="${this.className}"`]
}
get matchWholeLine() {
return this.applyToParentElement
}
get pattern() {
return this.matchWholeLine ? this.parent.content : this.getAtomsFrom(2).join(" ")
}
classesMarkupParser
extends classMarkupParser
crux classes
javascript
applyToParentElement = true
get className() {
return this.content
}
aftertextIdParser
popularity 0.000145
crux id
description Provide an ID to be output in the generated HTML tag.
extends abstractAftertextAttributeParser
atoms cueAtom htmlIdAtom
single
aftertextStyleParser
popularity 0.000217
crux style
description Provide code for the generated HTML tag's "style" attribute.
extends abstractAftertextAttributeParser
atoms cueAtom
catchAllAtomType cssAnyAtom
aftertextHiddenParser
crux hidden
atoms cueAtom
description Do not compile this particle to HTML.
extends abstractAftertextAttributeParser
single