| ︙ | | |
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
-
+
-
-
+
-
-
|
return nil
}
plist := sx.Nil()
keys := a.Keys()
for i := len(keys) - 1; i >= 0; i-- {
key := keys[i]
if key != zsx.DefaultAttribute && isValidName(key) {
plist = plist.Cons(sx.Cons(sx.MakeSymbol(key), sx.MakeString(a[key])))
plist = plist.Cons(sx.Cons(sxhtml.MakeSymbol(key), sx.MakeString(a[key])))
}
}
if plist == nil {
return nil
return plist
}
return plist.Cons(sxhtml.SymAttr)
}
// Evaluate a metadata s-expression into a list of HTML s-expressions.
func (ev *Evaluator) Evaluate(lst *sx.Pair, env *Environment) (*sx.Pair, error) {
result := ev.Eval(lst, env)
if err := env.err; err != nil {
return nil, err
|
| ︙ | | |
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
|
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
|
-
+
-
+
-
-
+
-
|
if env.err != nil || len(env.endnotes) == 0 {
return nil
}
var result sx.ListBuilder
result.AddN(
SymOL,
sx.Nil().Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnotes"))).Cons(sxhtml.SymAttr),
sx.Nil().Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnotes"))),
)
for i, fni := range env.endnotes {
noteNum := strconv.Itoa(i + 1)
attrs := fni.attrs.Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnote"))).
Cons(sx.Cons(SymAttrValue, sx.MakeString(noteNum))).
Cons(sx.Cons(SymAttrID, sx.MakeString("fn:"+fni.noteID))).
Cons(sx.Cons(SymAttrRole, sx.MakeString("doc-endnote"))).
Cons(sx.Cons(SymAttrRole, sx.MakeString("doc-endnote")))
Cons(sxhtml.SymAttr)
backref := sx.Nil().Cons(sx.MakeString("\u21a9\ufe0e")).
Cons(sx.Nil().
Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnote-backref"))).
Cons(sx.Cons(SymAttrHref, sx.MakeString("#fnref:"+fni.noteID))).
Cons(sx.Cons(SymAttrRole, sx.MakeString("doc-backlink"))).
Cons(sx.Cons(SymAttrRole, sx.MakeString("doc-backlink")))).
Cons(sxhtml.SymAttr)).
Cons(SymA)
var li sx.ListBuilder
li.AddN(SymLI, attrs)
li.ExtendBang(fni.noteHx)
li.AddN(sx.MakeString(" "), backref)
result.Add(li.List())
|
| ︙ | | |
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
|
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
|
-
-
-
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
-
-
+
+
|
}
ev.fns[symVal] = fn
}
func (ev *Evaluator) bindMetadata() {
ev.bind(sz.SymMeta, 0, ev.evalList)
evalMetaString := func(args sx.Vector, env *Environment) sx.Object {
a := make(zsx.Attributes, 2).
Set("name", getSymbol(args[0], env).GetValue()).
Set("content", getString(args[1], env).GetValue())
return ev.evalMetaString(args[0], getString(args[1], env).GetValue(), env)
return ev.EvaluateMeta(a)
}
ev.bind(sz.SymTypeCredential, 2, evalMetaString)
ev.bind(sz.SymTypeEmpty, 2, evalMetaString)
ev.bind(sz.SymTypeID, 2, evalMetaString)
ev.bind(sz.SymTypeNumber, 2, evalMetaString)
ev.bind(sz.SymTypeString, 2, evalMetaString)
ev.bind(sz.SymTypeTimestamp, 2, evalMetaString)
ev.bind(sz.SymTypeURL, 2, evalMetaString)
ev.bind(sz.SymTypeWord, 2, evalMetaString)
evalMetaSet := func(args sx.Vector, env *Environment) sx.Object {
var sb strings.Builder
for obj := range getList(args[1], env).Values() {
sb.WriteByte(' ')
sb.WriteString(getString(obj, env).GetValue())
}
s := sb.String()
if len(s) > 0 {
s = s[1:]
}
a := make(zsx.Attributes, 2).
Set("name", getSymbol(args[0], env).GetValue()).
Set("content", s)
return ev.EvaluateMeta(a)
return ev.evalMetaString(args[0], s, env)
}
ev.bind(sz.SymTypeIDSet, 2, evalMetaSet)
ev.bind(sz.SymTypeTagSet, 2, evalMetaSet)
}
func (ev *Evaluator) evalMetaString(nameObj sx.Object, content string, env *Environment) sx.Object {
if env.err == nil {
if nameSym, ok := sx.GetSymbol(nameObj); ok {
a := make(zsx.Attributes, 2).
Set("name", nameSym.GetValue()).
Set("content", content)
return ev.EvaluateMeta(a)
}
env.err = fmt.Errorf("%v/%T is not a symbol", nameObj, nameObj)
}
return sx.Nil()
}
// EvaluateMeta returns HTML meta object for an attribute.
func (ev *Evaluator) EvaluateMeta(a zsx.Attributes) *sx.Pair {
return sx.Nil().Cons(EvaluateAttributes(a)).Cons(SymMeta)
}
func (ev *Evaluator) bindBlocks() {
ev.bind(zsx.SymBlock, 0, ev.evalList)
ev.bind(zsx.SymPara, 0, func(args sx.Vector, env *Environment) sx.Object {
return ev.evalSlice(args, env).Cons(SymP)
if sl := ev.evalSlice(args, env); sl != nil {
return sl.Cons(SymP)
}
return nil
})
ev.bind(zsx.SymHeading, 5, func(args sx.Vector, env *Environment) sx.Object {
nLevel := getInt64(args[0], env)
if nLevel <= 0 {
env.err = fmt.Errorf("%v is a negative heading level", nLevel)
return sx.Nil()
}
hLevel := nLevel + ev.headingOffset
if hLevel > 6 {
env.err = fmt.Errorf("%v is a too large heading level", hLevel)
return sx.Nil()
}
level := strconv.FormatInt(nLevel+ev.headingOffset, 10)
headingSymbol := sx.MakeSymbol("h" + level)
sLevel := strconv.FormatInt(hLevel, 10)
headingSymbol := sxhtml.MakeSymbol("h" + sLevel)
a := GetAttributes(args[1], env)
env.pushAttributes(a)
defer env.popAttributes()
if fragment := getString(args[3], env).GetValue(); fragment != "" {
a = a.Set("id", ev.unique+fragment)
}
|
| ︙ | | |
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
+
-
+
|
return sx.Nil()
}
var result sx.ListBuilder
result.Add(symBLOCKQUOTE)
if attrs := EvaluateAttributes(GetAttributes(args[0], env)); attrs != nil {
result.Add(attrs)
}
isCompact := isCompactList(args[1:])
for _, elem := range args[1:] {
if quote, isPair := sx.GetPair(ev.Eval(elem, env)); isPair {
if quote, isPair := sx.GetPair(ev.Eval(makeCompactItem(isCompact, elem), env)); isPair {
result.Add(quote.Cons(sxhtml.SymListSplice))
}
}
return result.List()
})
ev.bind(zsx.SymDescription, 1, func(args sx.Vector, env *Environment) sx.Object {
|
| ︙ | | |
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
-
+
-
+
-
+
-
+
|
dditem := getList(ddlst, env)
result.Add(dditem.Cons(symDD))
}
}
return result.List()
})
ev.bind(zsx.SymTable, 1, func(args sx.Vector, env *Environment) sx.Object {
ev.bind(zsx.SymTable, 2, func(args sx.Vector, env *Environment) sx.Object {
thead := sx.Nil()
if header := getList(args[0], env); !sx.IsNil(header) {
if header := getList(args[1], env); !sx.IsNil(header) {
thead = sx.Nil().Cons(ev.evalTableRow(symTH, header, env)).Cons(symTHEAD)
}
var tbody sx.ListBuilder
if len(args) > 1 {
if len(args) > 2 {
tbody.Add(symTBODY)
for _, row := range args[1:] {
for _, row := range args[2:] {
tbody.Add(ev.evalTableRow(symTD, getList(row, env), env))
}
}
table := sx.Nil()
if !tbody.IsEmpty() {
table = table.Cons(tbody.List())
|
| ︙ | | |
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
-
+
-
+
|
content := getString(args[1], env)
if a.HasDefault() {
content = sx.MakeString(visibleReplacer.Replace(content.GetValue()))
}
return evalVerbatim(a, content)
})
ev.bind(zsx.SymVerbatimZettel, 0, nilFn)
ev.bind(zsx.SymBLOB, 4, func(args sx.Vector, env *Environment) sx.Object {
ev.bind(zsx.SymBLOB, 3, func(args sx.Vector, env *Environment) sx.Object {
a := GetAttributes(args[0], env)
return evalBLOB(a, getList(args[1], env), getString(args[2], env), getString(args[3], env))
return evalBLOB(a, ev.evalSlice(args[3:], env), getString(args[1], env), getString(args[2], env))
})
ev.bind(zsx.SymTransclude, 2, func(args sx.Vector, env *Environment) sx.Object {
if refSym, refValue := GetReference(args[1], env); refSym != nil {
if refSym.IsEqualSymbol(zsx.SymRefStateExternal) {
a := GetAttributes(args[0], env).Set("src", refValue).AddClass("external")
// TODO: if len(args) > 2, add "alt" attr based on args[2:], as in SymEmbed
return sx.Nil().Cons(sx.Nil().Cons(EvaluateAttributes(a)).Cons(SymIMG)).Cons(SymP)
|
| ︙ | | |
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
+
+
-
-
-
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
return func(args sx.Vector, env *Environment) sx.Object {
var result sx.ListBuilder
result.Add(sym)
if attrs := EvaluateAttributes(GetAttributes(args[0], env)); attrs != nil {
result.Add(attrs)
}
if len(args) > 1 {
isCompact := isCompactList(args[1:])
for _, elem := range args[1:] {
var itemLb sx.ListBuilder
item := sx.Nil().Cons(SymLI)
if res, isPair := sx.GetPair(ev.Eval(elem, env)); isPair {
item.ExtendBang(res)
itemLb.Add(SymLI)
if res, isPair := sx.GetPair(ev.Eval(makeCompactItem(isCompact, elem), env)); isPair {
itemLb.ExtendBang(res)
}
result.Add(item)
result.Add(itemLb.List())
}
}
return result.List()
}
}
func isCompactList(elems sx.Vector) bool {
for _, elem := range elems {
item, isPair := sx.GetPair(elem)
if !isPair {
return false
}
if !zsx.SymBlock.IsEqual(item.Car()) {
return false
}
item = item.Tail()
if item.Tail() != nil { // more than two elements -> multiple paragraphs in item
return false
}
head := item.Head()
if !zsx.SymPara.IsEqual(head.Car()) {
return false
}
}
return true
}
func makeCompactItem(isCompact bool, elem sx.Object) sx.Object {
if isCompact {
if item, isPair := sx.GetPair(elem); isPair {
elem = item.Tail().Head().Tail().Cons(zsx.SymInline)
}
}
return elem
}
func (ev *Evaluator) evalDescriptionTerm(term *sx.Pair, env *Environment) *sx.Pair {
var result sx.ListBuilder
for obj := range term.Values() {
elem := ev.Eval(obj, env)
result.Add(elem)
|
| ︙ | | |
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
|
-
+
-
+
+
+
+
-
+
+
+
-
+
-
-
+
|
)
})
ev.bind(zsx.SymCite, 2, func(args sx.Vector, env *Environment) sx.Object {
a := GetAttributes(args[0], env)
env.pushAttributes(a)
defer env.popAttributes()
result := sx.Nil()
var result *sx.Pair
if key := getString(args[1], env); key.GetValue() != "" {
if len(args) > 2 {
result = ev.evalSlice(args[2:], env).Cons(sx.MakeString(", "))
if sl := ev.evalSlice(args[2:], env); sl != nil {
result = sl.Cons(sx.MakeString(", "))
}
}
result = result.Cons(key)
}
if len(a) > 0 {
result = result.Cons(EvaluateAttributes(a))
}
if result == nil {
return nil
}
return result.Cons(SymSPAN)
})
ev.bind(zsx.SymMark, 3, func(args sx.Vector, env *Environment) sx.Object {
result := ev.evalSlice(args[3:], env)
if !ev.noLinks {
if fragment := getString(args[2], env).GetValue(); fragment != "" {
a := zsx.Attributes{"id": fragment + ev.unique}
return result.Cons(EvaluateAttributes(a)).Cons(SymA)
}
}
if result != nil {
return result.Cons(SymSPAN)
return result.Cons(SymSPAN)
}
return nil
})
ev.bind(zsx.SymEndnote, 1, func(args sx.Vector, env *Environment) sx.Object {
a := GetAttributes(args[0], env)
env.pushAttributes(a)
defer env.popAttributes()
attrPlist := sx.Nil()
if len(a) > 0 {
if attrs := EvaluateAttributes(a); attrs != nil {
attrPlist = attrs.Tail()
}
}
noteNum := strconv.Itoa(len(env.endnotes) + 1)
noteID := ev.unique + noteNum
env.endnotes = append(env.endnotes, endnoteInfo{
noteID: noteID, noteAST: args[1:], noteHx: nil, attrs: attrPlist})
hrefAttr := sx.Nil().Cons(sx.Cons(SymAttrRole, sx.MakeString("doc-noteref"))).
Cons(sx.Cons(SymAttrHref, sx.MakeString("#fn:"+noteID))).
Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-noteref"))).
Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-noteref")))
Cons(sxhtml.SymAttr)
href := sx.Nil().Cons(sx.MakeString(noteNum)).Cons(hrefAttr).Cons(SymA)
supAttr := sx.Nil().Cons(sx.Cons(SymAttrID, sx.MakeString("fnref:"+noteID))).Cons(sxhtml.SymAttr)
supAttr := sx.Nil().Cons(sx.Cons(SymAttrID, sx.MakeString("fnref:"+noteID)))
return sx.Nil().Cons(href).Cons(supAttr).Cons(symSUP)
})
ev.bind(zsx.SymFormatDelete, 1, ev.makeFormatFn(symDEL))
ev.bind(zsx.SymFormatEmph, 1, ev.makeFormatFn(symEM))
ev.bind(zsx.SymFormatInsert, 1, ev.makeFormatFn(symINS))
ev.bind(zsx.SymFormatMark, 1, ev.makeFormatFn(symMARK))
|
| ︙ | | |
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
|
+
-
+
+
+
|
if val, hasClass := a.Get(""); hasClass {
a = a.Remove("").AddClass(val)
}
res := ev.evalSlice(args[1:], env)
if len(a) > 0 {
res = res.Cons(EvaluateAttributes(a))
}
if res != nil {
return res.Cons(sym)
return res.Cons(sym)
}
return nil
}
}
func (ev *Evaluator) evalQuote(args sx.Vector, env *Environment) sx.Object {
a := GetAttributes(args[0], env)
env.pushAttributes(a)
defer env.popAttributes()
|
| ︙ | | |
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
|
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
|
+
-
+
+
+
|
res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ)))
}
}
if len(a) > 0 {
res = res.Cons(EvaluateAttributes(a))
return res.Cons(SymSPAN)
}
if res != nil {
return res.Cons(sxhtml.SymListSplice)
return res.Cons(sxhtml.SymListSplice)
}
return nil
}
var visibleReplacer = strings.NewReplacer(" ", "\u2423")
func evalLiteral(args sx.Vector, a zsx.Attributes, sym *sx.Symbol, env *Environment) sx.Object {
if a == nil {
a = GetAttributes(args[0], env)
|
| ︙ | | |
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
|
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
|
-
+
-
-
-
-
-
-
-
-
-
|
return result.List()
}
return nil
}
func (ev *Evaluator) evalLink(a zsx.Attributes, refValue string, inline sx.Vector, env *Environment) sx.Object {
result := ev.evalSlice(inline, env)
if len(inline) == 0 {
if result == nil {
result = sx.Nil().Cons(sx.MakeString(refValue))
}
if ev.noLinks {
return result.Cons(SymSPAN)
}
return result.Cons(EvaluateAttributes(a)).Cons(SymA)
}
func getSymbol(obj sx.Object, env *Environment) *sx.Symbol {
if env.err == nil {
if sym, ok := sx.GetSymbol(obj); ok {
return sym
}
env.err = fmt.Errorf("%v/%T is not a symbol", obj, obj)
}
return sx.MakeSymbol("???")
}
func getString(val sx.Object, env *Environment) sx.String {
if env.err == nil {
if s, ok := sx.GetString(val); ok {
return s
}
env.err = fmt.Errorf("%v/%T is not a string", val, val)
}
|
| ︙ | | |
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
|
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
|
-
+
|
return zsx.GetAttributes(getList(arg, env))
}
// GetReference returns the reference symbol and the reference value of a reference pair.
func GetReference(val sx.Object, env *Environment) (*sx.Symbol, string) {
if env.err == nil {
if p := getList(val, env); env.err == nil {
sym, val := sz.GetReference(p)
sym, val := zsx.GetReference(p)
if sym != nil {
return sym, val
}
env.err = fmt.Errorf("%v/%T is not a reference", val, val)
}
}
return nil, ""
|
| ︙ | | |
| ︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
)
var symInVerse = sx.MakeSymbol("in-verse")
var symNoBlock = sx.MakeSymbol("no-block")
type postProcessor struct{}
func (pp *postProcessor) VisitBefore(lst *sx.Pair, env *sx.Pair) (sx.Object, bool) {
func (pp *postProcessor) VisitBefore(lst *sx.Pair, alst *sx.Pair) (sx.Object, bool) {
if lst == nil {
return nil, true
}
sym, isSym := sx.GetSymbol(lst.Car())
if !isSym {
panic(lst)
}
if fn, found := symMap[sym]; found {
return fn(pp, lst, env), true
return fn(pp, lst, alst), true
}
return nil, false
}
func (pp *postProcessor) VisitAfter(lst *sx.Pair, _ *sx.Pair) sx.Object { return lst }
func (pp *postProcessor) visitPairList(lst *sx.Pair, env *sx.Pair) *sx.Pair {
func (pp *postProcessor) visitPairList(lst *sx.Pair, alst *sx.Pair) *sx.Pair {
var pList sx.ListBuilder
for node := range lst.Pairs() {
if elem, isPair := sx.GetPair(zsx.Walk(pp, node.Head(), env)); isPair && elem != nil {
if elem, isPair := sx.GetPair(zsx.Walk(pp, node.Head(), alst)); isPair && elem != nil {
pList.Add(elem)
}
}
return pList.List()
}
var symMap map[*sx.Symbol]func(*postProcessor, *sx.Pair, *sx.Pair) *sx.Pair
|
| ︙ | | |
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
|
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
|
-
-
+
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
|
symSeparator: ignoreProcess,
}
}
func ignoreProcess(*postProcessor, *sx.Pair, *sx.Pair) *sx.Pair { return nil }
func postProcessBlockList(pp *postProcessor, lst *sx.Pair, env *sx.Pair) *sx.Pair {
result := pp.visitPairList(lst.Tail(), env)
func postProcessBlockList(pp *postProcessor, lst *sx.Pair, alst *sx.Pair) *sx.Pair {
result := pp.visitPairList(lst.Tail(), alst)
if result == nil {
if noBlockPair := env.Assoc(symNoBlock); noBlockPair == nil || sx.IsTrue(noBlockPair.Cdr()) {
if noBlockPair := alst.Assoc(symNoBlock); noBlockPair == nil || sx.IsTrue(noBlockPair.Cdr()) {
return nil
}
}
return result.Cons(lst.Car())
}
func postProcessInlineList(pp *postProcessor, lst *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessInlineList(pp *postProcessor, lst *sx.Pair, alst *sx.Pair) *sx.Pair {
sym := lst.Car()
if rest := pp.visitInlines(lst.Tail(), env); rest != nil {
if rest := pp.visitInlines(lst.Tail(), alst); rest != nil {
return rest.Cons(sym)
}
return nil
}
func postProcessRegion(pp *postProcessor, rn *sx.Pair, env *sx.Pair) *sx.Pair {
return doPostProcessRegion(pp, rn, env, env)
func postProcessRegion(pp *postProcessor, rn *sx.Pair, alst *sx.Pair) *sx.Pair {
return doPostProcessRegion(pp, rn, alst, alst)
}
func postProcessRegionVerse(pp *postProcessor, rn *sx.Pair, env *sx.Pair) *sx.Pair {
return doPostProcessRegion(pp, rn, env.Cons(sx.Cons(symInVerse, nil)), env)
func postProcessRegionVerse(pp *postProcessor, rn *sx.Pair, alst *sx.Pair) *sx.Pair {
return doPostProcessRegion(pp, rn, alst.Cons(sx.Cons(symInVerse, nil)), alst)
}
func doPostProcessRegion(pp *postProcessor, rn *sx.Pair, envBlock, envInline *sx.Pair) *sx.Pair {
func doPostProcessRegion(pp *postProcessor, rn *sx.Pair, alstBlock, alstInline *sx.Pair) *sx.Pair {
sym := rn.Car().(*sx.Symbol)
next := rn.Tail()
attrs := next.Car().(*sx.Pair)
next = next.Tail()
blocks := pp.visitPairList(next.Head(), envBlock)
text := pp.visitInlines(next.Tail(), envInline)
blocks := pp.visitPairList(next.Head(), alstBlock)
text := pp.visitInlines(next.Tail(), alstInline)
if blocks == nil && text == nil {
return nil
}
return zsx.MakeRegion(sym, attrs, blocks, text)
}
func postProcessVerbatim(_ *postProcessor, verb *sx.Pair, _ *sx.Pair) *sx.Pair {
if content, isString := sx.GetString(verb.Tail().Tail().Car()); isString && content.GetValue() != "" {
return verb
}
return nil
}
func postProcessHeading(pp *postProcessor, hn *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessHeading(pp *postProcessor, hn *sx.Pair, alst *sx.Pair) *sx.Pair {
next := hn.Tail()
level := next.Car().(sx.Int64)
next = next.Tail()
attrs := next.Car().(*sx.Pair)
next = next.Tail()
slug := next.Car().(sx.String)
next = next.Tail()
fragment := next.Car().(sx.String)
if text := pp.visitInlines(next.Tail(), env); text != nil {
if text := pp.visitInlines(next.Tail(), alst); text != nil {
return zsx.MakeHeading(int(level), attrs, text, slug.GetValue(), fragment.GetValue())
}
return nil
}
func postProcessItemList(pp *postProcessor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessItemList(pp *postProcessor, ln *sx.Pair, alst *sx.Pair) *sx.Pair {
attrs := ln.Tail().Head()
elems := pp.visitListElems(ln.Tail(), env)
elems := pp.visitListElems(ln.Tail(), alst)
if elems == nil {
return nil
}
return zsx.MakeList(ln.Car().(*sx.Symbol), attrs, elems)
}
func postProcessQuoteList(pp *postProcessor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessQuoteList(pp *postProcessor, ln *sx.Pair, alst *sx.Pair) *sx.Pair {
attrs := ln.Tail().Head()
elems := pp.visitListElems(ln.Tail(), env.Cons(sx.Cons(symNoBlock, nil)))
elems := pp.visitListElems(ln.Tail(), alst.Cons(sx.Cons(symNoBlock, nil)))
// Collect multiple paragraph items into one item.
var newElems sx.ListBuilder
var newPara sx.ListBuilder
addtoParagraph := func() {
if !newPara.IsEmpty() {
newElems.Add(sx.MakeList(zsx.SymBlock, newPara.List().Cons(zsx.SymPara)))
newPara.Reset()
}
}
for node := range elems.Pairs() {
item := node.Head()
if !item.Car().IsEqual(zsx.SymBlock) {
if !zsx.SymBlock.IsEqual(item.Car()) {
continue
}
itemTail := item.Tail()
if itemTail == nil || itemTail.Tail() != nil {
addtoParagraph()
newElems.Add(item)
continue
}
if pn := itemTail.Head(); pn.Car().IsEqual(zsx.SymPara) {
if pn := itemTail.Head(); zsx.SymPara.IsEqual(pn.Car()) {
if !newPara.IsEmpty() {
newPara.Add(sx.Cons(zsx.SymSoft, nil))
}
newPara.ExtendBang(pn.Tail())
continue
}
addtoParagraph()
newElems.Add(item)
}
addtoParagraph()
return zsx.MakeList(ln.Car().(*sx.Symbol), attrs, newElems.List())
}
func (pp *postProcessor) visitListElems(ln *sx.Pair, env *sx.Pair) *sx.Pair {
func (pp *postProcessor) visitListElems(ln *sx.Pair, alst *sx.Pair) *sx.Pair {
var pList sx.ListBuilder
for node := range ln.Tail().Pairs() {
if elem := zsx.Walk(pp, node.Head(), env); elem != nil {
if elem := zsx.Walk(pp, node.Head(), alst); elem != nil {
pList.Add(elem)
}
}
return pList.List()
}
func postProcessDescription(pp *postProcessor, dl *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessDescription(pp *postProcessor, dl *sx.Pair, alst *sx.Pair) *sx.Pair {
attrs := dl.Tail().Head()
var dList sx.ListBuilder
isTerm := false
for node := range dl.Tail().Tail().Pairs() {
isTerm = !isTerm
if isTerm {
dList.Add(pp.visitInlines(node.Head(), env))
dList.Add(pp.visitInlines(node.Head(), alst))
} else {
dList.Add(zsx.Walk(pp, node.Head(), env))
dList.Add(zsx.Walk(pp, node.Head(), alst.Cons(sx.Cons(symNoBlock, nil))))
}
}
return dList.List().Cons(attrs).Cons(dl.Car())
}
func postProcessTable(pp *postProcessor, tbl *sx.Pair, env *sx.Pair) *sx.Pair {
sym := tbl.Car()
next := tbl.Tail()
func postProcessTable(pp *postProcessor, tbl *sx.Pair, alst *sx.Pair) *sx.Pair {
sym, next := tbl.Car(), tbl.Tail()
attrs := next.Head()
next = next.Tail()
header := next.Head()
if header != nil {
// Already post-processed
return tbl
}
rows, width := pp.visitRows(next.Tail(), env)
rows, width := pp.visitRows(next.Tail(), alst)
if rows == nil {
// Header and row are nil -> no table
return nil
}
header, rows, align := splitTableHeader(rows, width)
alignRow(header, align)
for node := range rows.Pairs() {
alignRow(node.Head(), align)
}
return rows.Cons(header).Cons(sym)
return rows.Cons(header).Cons(attrs).Cons(sym)
}
func (pp *postProcessor) visitRows(rows *sx.Pair, env *sx.Pair) (*sx.Pair, int) {
func (pp *postProcessor) visitRows(rows *sx.Pair, alst *sx.Pair) (*sx.Pair, int) {
maxWidth := 0
var pRows sx.ListBuilder
for node := range rows.Pairs() {
row := node.Head()
row, width := pp.visitCells(row, env)
row, width := pp.visitCells(row, alst)
if maxWidth < width {
maxWidth = width
}
pRows.Add(row)
}
return pRows.List(), maxWidth
}
func (pp *postProcessor) visitCells(cells *sx.Pair, env *sx.Pair) (*sx.Pair, int) {
func (pp *postProcessor) visitCells(cells *sx.Pair, alst *sx.Pair) (*sx.Pair, int) {
width := 0
var pCells sx.ListBuilder
for node := range cells.Pairs() {
cell := node.Head()
rest := cell.Tail()
attrs := rest.Head()
ins := pp.visitInlines(rest.Tail(), env)
ins := pp.visitInlines(rest.Tail(), alst)
pCells.Add(zsx.MakeCell(attrs, ins))
width++
}
return pCells.List(), width
}
func splitTableHeader(rows *sx.Pair, width int) (header, realRows *sx.Pair, align []byte) {
|
| ︙ | | |
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
|
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
|
-
+
-
+
|
cellInlines := rest.Tail()
if cellInlines == nil {
continue
}
// elem is first cell inline element
elem := cellInlines.Head()
if elem.Car().IsEqual(zsx.SymText) {
if zsx.SymText.IsEqual(elem.Car()) {
if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" {
str := s.GetValue()
if str[0] == '=' {
foundHeader = true
elem.SetCdr(sx.Cons(sx.MakeString(str[1:]), nil))
}
}
}
// move to the last cell inline element
for {
next := cellInlines.Tail()
if next == nil {
break
}
cellInlines = next
}
elem = cellInlines.Head()
if elem.Car().IsEqual(zsx.SymText) {
if zsx.SymText.IsEqual(elem.Car()) {
if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" {
str := s.GetValue()
lastByte := str[len(str)-1]
if cellAlign, isValid := getCellAlignment(lastByte); isValid {
elem.SetCdr(sx.Cons(sx.MakeString(str[0:len(str)-1]), nil))
rest.SetCar(makeCellAttrs(cellAlign))
}
|
| ︙ | | |
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
-
+
|
cellInlines := rest.Tail()
if cellInlines == nil {
continue
}
// elem is first cell inline element
elem := cellInlines.Head()
if elem.Car().IsEqual(zsx.SymText) {
if zsx.SymText.IsEqual(elem.Car()) {
if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" {
str := s.GetValue()
cellAlign, isValid := getCellAlignment(str[0])
if isValid {
elem.SetCdr(sx.Cons(sx.MakeString(str[1:]), nil))
rest.SetCar(makeCellAttrs(cellAlign))
}
|
| ︙ | | |
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
-
+
-
+
-
+
-
+
-
+
|
case '>':
return zsx.AttrAlignRight, true
default:
return sx.MakeString(""), false
}
}
func (pp *postProcessor) visitInlines(lst *sx.Pair, env *sx.Pair) *sx.Pair {
func (pp *postProcessor) visitInlines(lst *sx.Pair, alst *sx.Pair) *sx.Pair {
length := lst.Length()
if length <= 0 {
return nil
}
inVerse := env.Assoc(symInVerse) != nil
inVerse := alst.Assoc(symInVerse) != nil
vector := make([]*sx.Pair, 0, length)
// 1st phase: process all childs, ignore ' ' / '\t' at start, and merge some elements
for node := range lst.Pairs() {
elem, isPair := sx.GetPair(zsx.Walk(pp, node.Head(), env))
elem, isPair := sx.GetPair(zsx.Walk(pp, node.Head(), alst))
if !isPair || elem == nil {
continue
}
elemSym := elem.Car()
elemTail := elem.Tail()
if inVerse && elemSym.IsEqual(zsx.SymText) {
if inVerse && zsx.SymText.IsEqual(elemSym) {
if s, isString := sx.GetString(elemTail.Car()); isString {
verseText := s.GetValue()
verseText = strings.ReplaceAll(verseText, " ", "\u00a0")
elemTail.SetCar(sx.MakeString(verseText))
}
}
if len(vector) == 0 {
// If the 1st element is a TEXT, remove all ' ', '\t' at the beginning, if outside a verse block.
if !elemSym.IsEqual(zsx.SymText) {
if !zsx.SymText.IsEqual(elemSym) {
vector = append(vector, elem)
continue
}
elemText := elemTail.Car().(sx.String).GetValue()
if elemText != "" && (elemText[0] == ' ' || elemText[0] == '\t') {
for elemText != "" {
|
| ︙ | | |
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
-
+
-
+
|
vector = append(vector, elem)
}
continue
}
last := vector[len(vector)-1]
lastSym := last.Car()
if lastSym.IsEqual(zsx.SymText) && elemSym.IsEqual(zsx.SymText) {
if zsx.SymText.IsEqual(lastSym) && zsx.SymText.IsEqual(elemSym) {
// Merge two TEXT elements into one
lastText := last.Tail().Car().(sx.String).GetValue()
elemText := elem.Tail().Car().(sx.String).GetValue()
last.SetCdr(sx.Cons(sx.MakeString(lastText+elemText), sx.Nil()))
continue
}
if lastSym.IsEqual(zsx.SymText) && elemSym.IsEqual(zsx.SymSoft) {
if zsx.SymText.IsEqual(lastSym) && zsx.SymSoft.IsEqual(elemSym) {
// Merge (TEXT "... ") (SOFT) to (TEXT "...") (HARD)
lastTail := last.Tail()
if lastText := lastTail.Car().(sx.String).GetValue(); strings.HasSuffix(lastText, " ") {
newText := removeTrailingSpaces(lastText)
if newText == "" {
vector[len(vector)-1] = sx.Cons(zsx.SymHard, sx.Nil())
continue
|
| ︙ | | |
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
-
+
|
}
// 2nd phase: remove (SOFT), (HARD) at the end, remove trailing spaces in (TEXT "...")
lastPos := len(vector) - 1
for lastPos >= 0 {
elem := vector[lastPos]
elemSym := elem.Car()
if elemSym.IsEqual(zsx.SymText) {
if zsx.SymText.IsEqual(elemSym) {
elemTail := elem.Tail()
elemText := elemTail.Car().(sx.String).GetValue()
newText := removeTrailingSpaces(elemText)
if newText != "" {
elemTail.SetCar(sx.MakeString(newText))
break
}
|
| ︙ | | |
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
if content, isString := sx.GetString(tail.Car()); isString && content.GetValue() != "" {
return txt
}
}
return nil
}
func postProcessSoft(_ *postProcessor, sn *sx.Pair, env *sx.Pair) *sx.Pair {
if env.Assoc(symInVerse) == nil {
func postProcessSoft(_ *postProcessor, sn *sx.Pair, alst *sx.Pair) *sx.Pair {
if alst.Assoc(symInVerse) == nil {
return sn
}
return sx.Cons(zsx.SymHard, nil)
}
func postProcessEndnote(pp *postProcessor, en *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessEndnote(pp *postProcessor, en *sx.Pair, alst *sx.Pair) *sx.Pair {
next := en.Tail()
attrs := next.Car().(*sx.Pair)
if text := pp.visitInlines(next.Tail(), env); text != nil {
if text := pp.visitInlines(next.Tail(), alst); text != nil {
return zsx.MakeEndnote(attrs, text)
}
return zsx.MakeEndnote(attrs, sx.Nil())
}
func postProcessMark(pp *postProcessor, en *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessMark(pp *postProcessor, en *sx.Pair, alst *sx.Pair) *sx.Pair {
next := en.Tail()
mark := next.Car().(sx.String)
next = next.Tail()
slug := next.Car().(sx.String)
next = next.Tail()
fragment := next.Car().(sx.String)
text := pp.visitInlines(next.Tail(), env)
text := pp.visitInlines(next.Tail(), alst)
return zsx.MakeMark(mark.GetValue(), slug.GetValue(), fragment.GetValue(), text)
}
func postProcessInlines4(pp *postProcessor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessInlines4(pp *postProcessor, ln *sx.Pair, alst *sx.Pair) *sx.Pair {
sym := ln.Car()
next := ln.Tail()
attrs := next.Car()
next = next.Tail()
val3 := next.Car()
text := pp.visitInlines(next.Tail(), env)
text := pp.visitInlines(next.Tail(), alst)
return text.Cons(val3).Cons(attrs).Cons(sym)
}
func postProcessEmbed(pp *postProcessor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessEmbed(pp *postProcessor, ln *sx.Pair, alst *sx.Pair) *sx.Pair {
next := ln.Tail()
attrs := next.Car().(*sx.Pair)
next = next.Tail()
ref := next.Car()
next = next.Tail()
syntax := next.Car().(sx.String)
text := pp.visitInlines(next.Tail(), env)
text := pp.visitInlines(next.Tail(), alst)
return zsx.MakeEmbed(attrs, ref, syntax.GetValue(), text)
}
func postProcessFormat(pp *postProcessor, fn *sx.Pair, env *sx.Pair) *sx.Pair {
func postProcessFormat(pp *postProcessor, fn *sx.Pair, alst *sx.Pair) *sx.Pair {
symFormat := fn.Car().(*sx.Symbol)
next := fn.Tail() // Attrs
attrs := next.Car().(*sx.Pair)
next = next.Tail() // Possible inlines
if next == nil {
return fn
}
inlines := pp.visitInlines(next, env)
inlines := pp.visitInlines(next, alst)
return zsx.MakeFormat(symFormat, attrs, inlines)
}
|
| ︙ | | |
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
|
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
|
-
-
+
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
-
-
+
-
-
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
|
"t73f.de/r/sx"
"t73f.de/r/zsc/sz/zmk"
"t73f.de/r/zsx"
"t73f.de/r/zsx/input"
)
type TestCase struct{ source, want string }
type TestCases []TestCase
type testCase struct{ src, exp string }
type testCases []testCase
type symbolMap map[string]*sx.Symbol
func replace(s string, sm symbolMap, tcs TestCases) TestCases {
func replace(s string, sm symbolMap, tcs testCases) testCases {
var sym string
if len(sm) > 0 {
sym = sm[s].GetValue()
}
var testCases TestCases
var testcases testCases
for _, tc := range tcs {
source := strings.ReplaceAll(tc.source, "$", s)
want := tc.want
source := strings.ReplaceAll(tc.src, "$", s)
want := tc.exp
if sym != "" {
want = strings.ReplaceAll(want, "$%", sym)
}
want = strings.ReplaceAll(want, "$", s)
testCases = append(testCases, TestCase{source, want})
testcases = append(testcases, testCase{source, want})
}
return testCases
return testcases
}
func checkTcs(t *testing.T, tcs TestCases) {
func checkTcs(t *testing.T, tcs testCases) {
t.Helper()
var parser zmk.Parser
for tcn, tc := range tcs {
t.Run(fmt.Sprintf("TC=%02d,src=%q", tcn, tc.source), func(st *testing.T) {
t.Run(fmt.Sprintf("TC=%02d,src=%q", tcn, tc.src), func(st *testing.T) {
st.Helper()
inp := input.NewInput([]byte(tc.source))
inp := input.NewInput([]byte(tc.src))
parser.Initialize(inp)
ast := parser.Parse()
found := false
if got := ast.String(); tc.exp != got {
st.Errorf("none\nwant=%q\n got=%q", tc.exp, got)
found = true
}
zsx.Walk(astWalker{}, ast, nil)
got := ast.String()
copyAST := zsx.Walk(astWalker{}, ast, nil)
if got := copyAST.String(); tc.exp != got && !found {
st.Errorf("copy\nwant=%q\n got=%q", tc.exp, got)
found = true
}
zsx.WalkIt(astWalkerIt{}, ast, nil)
if got := ast.String(); tc.exp != got && !found {
if tc.want != got {
st.Errorf("\nwant=%q\n got=%q", tc.want, got)
st.Errorf("itit\nwant=%q\n got=%q", tc.exp, got)
}
})
}
}
type astWalker struct{}
func (astWalker) VisitBefore(node *sx.Pair, env *sx.Pair) (sx.Object, bool) { return sx.Nil(), false }
func (astWalker) VisitAfter(node *sx.Pair, env *sx.Pair) sx.Object { return node }
func (astWalker) VisitBefore(*sx.Pair, *sx.Pair) (sx.Object, bool) { return sx.Nil(), false }
func (astWalker) VisitAfter(node *sx.Pair, _ *sx.Pair) sx.Object { return node }
type astWalkerIt struct{}
func (astWalkerIt) VisitItBefore(*sx.Pair, *sx.Pair) bool { return false }
func (astWalkerIt) VisitItAfter(*sx.Pair, *sx.Pair) {}
func TestEdges(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"\"\"\"\n; \n0{{0}}{0}\n\"\"\"", "(BLOCK (REGION-VERSE () ((DESCRIPTION () ()) (PARA (TEXT \"0\") (EMBED ((\"0\" . \"\")) (HOSTED \"0\") \"\")))))"},
})
}
func TestEOL(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"", "()"},
{"\n", "()"},
{"\r", "()"},
{"\r\n", "()"},
{"\n\n", "()"},
checkTcs(t, testCases{
{"", "(BLOCK)"},
{"\n", "(BLOCK)"},
{"\r", "(BLOCK)"},
{"\r\n", "(BLOCK)"},
{"\n\n", "(BLOCK)"},
})
}
func TestText(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"abcd", "(BLOCK (PARA (TEXT \"abcd\")))"},
{"ab cd", "(BLOCK (PARA (TEXT \"ab cd\")))"},
{"abcd ", "(BLOCK (PARA (TEXT \"abcd\")))"},
{" abcd", "(BLOCK (PARA (TEXT \"abcd\")))"},
{"\\", "(BLOCK (PARA (TEXT \"\\\\\")))"},
{"\\\n", "()"},
{"\\\n", "(BLOCK)"},
{"\\\ndef", "(BLOCK (PARA (HARD) (TEXT \"def\")))"},
{"\\\r", "()"},
{"\\\r", "(BLOCK)"},
{"\\\rdef", "(BLOCK (PARA (HARD) (TEXT \"def\")))"},
{"\\\r\n", "()"},
{"\\\r\n", "(BLOCK)"},
{"\\\r\ndef", "(BLOCK (PARA (HARD) (TEXT \"def\")))"},
{"\\a", "(BLOCK (PARA (TEXT \"a\")))"},
{"\\aa", "(BLOCK (PARA (TEXT \"aa\")))"},
{"a\\a", "(BLOCK (PARA (TEXT \"aa\")))"},
{"\\+", "(BLOCK (PARA (TEXT \"+\")))"},
{"\\ ", "(BLOCK (PARA (TEXT \"\u00a0\")))"},
{"http://a, http://b", "(BLOCK (PARA (TEXT \"http://a, http://b\")))"},
})
}
func TestSpace(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{" ", "()"},
{"\t", "()"},
{" ", "()"},
checkTcs(t, testCases{
{" ", "(BLOCK)"},
{"\t", "(BLOCK)"},
{" ", "(BLOCK)"},
})
}
func TestSoftBreak(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"x\ny", "(BLOCK (PARA (TEXT \"x\") (SOFT) (TEXT \"y\")))"},
{"z\n", "(BLOCK (PARA (TEXT \"z\")))"},
{" \n ", "()"},
{" \n", "()"},
{" \n ", "(BLOCK)"},
{" \n", "(BLOCK)"},
})
}
func TestHardBreak(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"x \ny", "(BLOCK (PARA (TEXT \"x\") (HARD) (TEXT \"y\")))"},
{"z \n", "(BLOCK (PARA (TEXT \"z\")))"},
{" \n ", "()"},
{" \n", "()"},
{" \n ", "(BLOCK)"},
{" \n", "(BLOCK)"},
})
}
func TestLink(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"[", "(BLOCK (PARA (TEXT \"[\")))"},
{"[[", "(BLOCK (PARA (TEXT \"[[\")))"},
{"[[|", "(BLOCK (PARA (TEXT \"[[|\")))"},
{"[[]", "(BLOCK (PARA (TEXT \"[[]\")))"},
{"[[|]", "(BLOCK (PARA (TEXT \"[[|]\")))"},
{"[[]]", "(BLOCK (PARA (TEXT \"[[]]\")))"},
{"[[|]]", "(BLOCK (PARA (TEXT \"[[|]]\")))"},
|
| ︙ | | |
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
-
+
|
{"[[Text|query:title syntax]]", "(BLOCK (PARA (LINK () (QUERY \"title syntax\") (TEXT \"Text\"))))"},
{"[[Text|query:title | action]]", "(BLOCK (PARA (LINK () (QUERY \"title | action\") (TEXT \"Text\"))))"},
})
}
func TestEmbed(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"{", "(BLOCK (PARA (TEXT \"{\")))"},
{"{{", "(BLOCK (PARA (TEXT \"{{\")))"},
{"{{|", "(BLOCK (PARA (TEXT \"{{|\")))"},
{"{{}", "(BLOCK (PARA (TEXT \"{{}\")))"},
{"{{|}", "(BLOCK (PARA (TEXT \"{{|}\")))"},
{"{{}}", "(BLOCK (PARA (TEXT \"{{}}\")))"},
{"{{|}}", "(BLOCK (PARA (TEXT \"{{|}}\")))"},
|
| ︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
|
{"{{http://a|http://a}}", "(BLOCK (PARA (EMBED () (EXTERNAL \"http://a\") \"\" (TEXT \"http://a\"))))"},
{"{{{{a}}}}", "(BLOCK (PARA (TEXT \"{{\") (EMBED () (HOSTED \"a\") \"\") (TEXT \"}}\")))"},
})
}
func TestCite(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"[@", "(BLOCK (PARA (TEXT \"[@\")))"},
{"[@]", "(BLOCK (PARA (TEXT \"[@]\")))"},
{"[@a]", "(BLOCK (PARA (CITE () \"a\")))"},
{"[@ a]", "(BLOCK (PARA (TEXT \"[@ a]\")))"},
{"[@a ]", "(BLOCK (PARA (CITE () \"a\")))"},
{"[@a\n]", "(BLOCK (PARA (CITE () \"a\")))"},
{"[@a\nx]", "(BLOCK (PARA (CITE () \"a\" (SOFT) (TEXT \"x\"))))"},
{"[@a\n\n]", "(BLOCK (PARA (TEXT \"[@a\")) (PARA (TEXT \"]\")))"},
{"[@a,\n]", "(BLOCK (PARA (CITE () \"a\")))"},
{"[@a,n]", "(BLOCK (PARA (CITE () \"a\" (TEXT \"n\"))))"},
{"[@a| n]", "(BLOCK (PARA (CITE () \"a\" (TEXT \"n\"))))"},
{"[@a|n ]", "(BLOCK (PARA (CITE () \"a\" (TEXT \"n\"))))"},
{"[@a,[@b]]", "(BLOCK (PARA (CITE () \"a\" (CITE () \"b\"))))"},
{"[@a]{color=green}", "(BLOCK (PARA (CITE ((\"color\" . \"green\")) \"a\")))"},
})
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"[@a\n\n]", "(BLOCK (PARA (TEXT \"[@a\")) (PARA (TEXT \"]\")))"},
})
}
func TestEndnote(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"[^", "(BLOCK (PARA (TEXT \"[^\")))"},
{"[^]", "(BLOCK (PARA (ENDNOTE ())))"},
{"[^]", "(BLOCK)"},
{"[^abc]", "(BLOCK (PARA (ENDNOTE () (TEXT \"abc\"))))"},
{"[^abc ]", "(BLOCK (PARA (ENDNOTE () (TEXT \"abc\"))))"},
{"[^abc\ndef]", "(BLOCK (PARA (ENDNOTE () (TEXT \"abc\") (SOFT) (TEXT \"def\"))))"},
{"[^abc\n\ndef]", "(BLOCK (PARA (TEXT \"[^abc\")) (PARA (TEXT \"def]\")))"},
{"[^abc[^def]]", "(BLOCK (PARA (ENDNOTE () (TEXT \"abc\") (ENDNOTE () (TEXT \"def\")))))"},
{"[^abc]{-}", "(BLOCK (PARA (ENDNOTE ((\"-\" . \"\")) (TEXT \"abc\"))))"},
})
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"[^abc\n\ndef]", "(BLOCK (PARA (TEXT \"[^abc\")) (PARA (TEXT \"def]\")))"},
})
}
func TestMark(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"[!", "(BLOCK (PARA (TEXT \"[!\")))"},
{"[!\n", "(BLOCK (PARA (TEXT \"[!\")))"},
{"[!]", "(BLOCK (PARA (MARK \"\" \"\" \"\")))"},
{"[!][!]", "(BLOCK (PARA (MARK \"\" \"\" \"\") (MARK \"\" \"\" \"\")))"},
{"[! ]", "(BLOCK (PARA (TEXT \"[! ]\")))"},
{"[!a]", "(BLOCK (PARA (MARK \"a\" \"\" \"\")))"},
{"[!a][!a]", "(BLOCK (PARA (MARK \"a\" \"\" \"\") (MARK \"a\" \"\" \"\")))"},
|
| ︙ | | |
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
-
+
|
{"[!|b ]", "(BLOCK (PARA (MARK \"\" \"\" \"\" (TEXT \"b\"))))"},
{"[!|b c]", "(BLOCK (PARA (MARK \"\" \"\" \"\" (TEXT \"b c\"))))"},
})
}
func TestComment(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"%", "(BLOCK (PARA (TEXT \"%\")))"},
{"%%", "(BLOCK (PARA (LITERAL-COMMENT () \"\")))"},
{"%\n", "(BLOCK (PARA (TEXT \"%\")))"},
{"%%\n", "(BLOCK (PARA (LITERAL-COMMENT () \"\")))"},
{"%%a", "(BLOCK (PARA (LITERAL-COMMENT () \"a\")))"},
{"%%%a", "(BLOCK (PARA (LITERAL-COMMENT () \"a\")))"},
{"%% a", "(BLOCK (PARA (LITERAL-COMMENT () \"a\")))"},
|
| ︙ | | |
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
"#": zsx.SymFormatMark,
":": zsx.SymFormatSpan,
}
t.Parallel()
// Not for Insert / '>', because collision with quoted list
// Not for Quote / '"', because escaped representation.
for _, ch := range []string{"_", "*", "~", "^", ",", "#", ":"} {
checkTcs(t, replace(ch, symMap, TestCases{
checkTcs(t, replace(ch, symMap, testCases{
{"$", "(BLOCK (PARA (TEXT \"$\")))"},
{"$$", "(BLOCK (PARA (TEXT \"$$\")))"},
{"$$$", "(BLOCK (PARA (TEXT \"$$$\")))"},
{"$$$$", "(BLOCK (PARA ($% ())))"},
}))
}
// Not for Quote / '"', because escaped representation.
for _, ch := range []string{"_", "*", ">", "~", "^", ",", "#", ":"} {
checkTcs(t, replace(ch, symMap, TestCases{
checkTcs(t, replace(ch, symMap, testCases{
{"$$a$$", "(BLOCK (PARA ($% () (TEXT \"a\"))))"},
{"$$a$$$", "(BLOCK (PARA ($% () (TEXT \"a\")) (TEXT \"$\")))"},
{"$$$a$$", "(BLOCK (PARA ($% () (TEXT \"$a\"))))"},
{"$$$a$$$", "(BLOCK (PARA ($% () (TEXT \"$a\")) (TEXT \"$\")))"},
{"$\\$", "(BLOCK (PARA (TEXT \"$$\")))"},
{"$\\$$", "(BLOCK (PARA (TEXT \"$$$\")))"},
{"$$\\$", "(BLOCK (PARA (TEXT \"$$$\")))"},
{"$$a\\$$", "(BLOCK (PARA (TEXT \"$$a$$\")))"},
{"$$a$\\$", "(BLOCK (PARA (TEXT \"$$a$$\")))"},
{"$$a\\$$$", "(BLOCK (PARA ($% () (TEXT \"a$\"))))"},
{"$$a\na$$", "(BLOCK (PARA ($% () (TEXT \"a\") (SOFT) (TEXT \"a\"))))"},
{"$$a\n\na$$", "(BLOCK (PARA (TEXT \"$$a\")) (PARA (TEXT \"a$$\")))"},
{"$$a$${go}", "(BLOCK (PARA ($% ((\"go\" . \"\")) (TEXT \"a\"))))"},
}))
checkTcs(t, replace(ch, symMap, TestCases{
checkTcs(t, replace(ch, symMap, testCases{
{"$$a\n\na$$", "(BLOCK (PARA (TEXT \"$$a\")) (PARA (TEXT \"a$$\")))"},
}))
}
checkTcs(t, replace(`"`, symbolMap{`"`: zsx.SymFormatQuote}, TestCases{
checkTcs(t, replace(`"`, symbolMap{`"`: zsx.SymFormatQuote}, testCases{
{"$", "(BLOCK (PARA (TEXT \"\\\"\")))"},
{"$$", "(BLOCK (PARA (TEXT \"\\\"\\\"\")))"},
{"$$$", "(BLOCK (PARA (TEXT \"\\\"\\\"\\\"\")))"},
{"$$$$", "(BLOCK (PARA ($% ())))"},
{"$$a$$", "(BLOCK (PARA ($% () (TEXT \"a\"))))"},
{"$$a$$$", "(BLOCK (PARA ($% () (TEXT \"a\")) (TEXT \"\\\"\")))"},
{"$$$a$$", "(BLOCK (PARA ($% () (TEXT \"\\\"a\"))))"},
{"$$$a$$$", "(BLOCK (PARA ($% () (TEXT \"\\\"a\")) (TEXT \"\\\"\")))"},
{"$\\$", "(BLOCK (PARA (TEXT \"\\\"\\\"\")))"},
{"$\\$$", "(BLOCK (PARA (TEXT \"\\\"\\\"\\\"\")))"},
{"$$\\$", "(BLOCK (PARA (TEXT \"\\\"\\\"\\\"\")))"},
{"$$a\\$$", "(BLOCK (PARA (TEXT \"\\\"\\\"a\\\"\\\"\")))"},
{"$$a$\\$", "(BLOCK (PARA (TEXT \"\\\"\\\"a\\\"\\\"\")))"},
{"$$a\\$$$", "(BLOCK (PARA ($% () (TEXT \"a\\\"\"))))"},
{"$$a\na$$", "(BLOCK (PARA ($% () (TEXT \"a\") (SOFT) (TEXT \"a\"))))"},
{"$$a\n\na$$", "(BLOCK (PARA (TEXT \"\\\"\\\"a\")) (PARA (TEXT \"a\\\"\\\"\")))"},
{"$$a$${go}", "(BLOCK (PARA ($% ((\"go\" . \"\")) (TEXT \"a\"))))"},
}))
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"__****__", "(BLOCK (PARA (FORMAT-EMPH () (FORMAT-STRONG ()))))"},
{"__**a**__", "(BLOCK (PARA (FORMAT-EMPH () (FORMAT-STRONG () (TEXT \"a\")))))"},
{"__**__**", "(BLOCK (PARA (TEXT \"__\") (FORMAT-STRONG () (TEXT \"__\"))))"},
})
}
func TestLiteral(t *testing.T) {
symMap := symbolMap{
"`": zsx.SymLiteralCode,
"'": zsx.SymLiteralInput,
"=": zsx.SymLiteralOutput,
}
t.Parallel()
for _, ch := range []string{"`", "'", "="} {
checkTcs(t, replace(ch, symMap, TestCases{
checkTcs(t, replace(ch, symMap, testCases{
{"$", "(BLOCK (PARA (TEXT \"$\")))"},
{"$$", "(BLOCK (PARA (TEXT \"$$\")))"},
{"$$$", "(BLOCK (PARA (TEXT \"$$$\")))"},
{"$$$$", "(BLOCK (PARA ($% () \"\")))"},
{"$$a$$", "(BLOCK (PARA ($% () \"a\")))"},
{"$$a$$$", "(BLOCK (PARA ($% () \"a\") (TEXT \"$\")))"},
{"$$$a$$", "(BLOCK (PARA ($% () \"$a\")))"},
{"$$$a$$$", "(BLOCK (PARA ($% () \"$a\") (TEXT \"$\")))"},
{"$\\$", "(BLOCK (PARA (TEXT \"$$\")))"},
{"$\\$$", "(BLOCK (PARA (TEXT \"$$$\")))"},
{"$$\\$", "(BLOCK (PARA (TEXT \"$$$\")))"},
{"$$a\\$$", "(BLOCK (PARA (TEXT \"$$a$$\")))"},
{"$$a$\\$", "(BLOCK (PARA (TEXT \"$$a$$\")))"},
{"$$a\\$$$", "(BLOCK (PARA ($% () \"a$\")))"},
{"$$a$${go}", "(BLOCK (PARA ($% ((\"go\" . \"\")) \"a\")))"},
}))
}
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"``<script `` abc", "(BLOCK (PARA (LITERAL-CODE () \"<script \") (TEXT \" abc\")))"},
{"''````''", "(BLOCK (PARA (LITERAL-INPUT () \"````\")))"},
{"''``a``''", "(BLOCK (PARA (LITERAL-INPUT () \"``a``\")))"},
{"''``''``", "(BLOCK (PARA (LITERAL-INPUT () \"``\") (TEXT \"``\")))"},
{"''\\'''", "(BLOCK (PARA (LITERAL-INPUT () \"'\")))"},
})
}
func TestLiteralMath(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"$", "(BLOCK (PARA (TEXT \"$\")))"},
{"$$", "(BLOCK (PARA (TEXT \"$$\")))"},
{"$$$", "(BLOCK (PARA (TEXT \"$$$\")))"},
{"$$$$", "(BLOCK (PARA (LITERAL-MATH () \"\")))"},
{"$$a$$", "(BLOCK (PARA (LITERAL-MATH () \"a\")))"},
{"$$a$$$", "(BLOCK (PARA (LITERAL-MATH () \"a\") (TEXT \"$\")))"},
{"$$$a$$", "(BLOCK (PARA (LITERAL-MATH () \"$a\")))"},
{"$$$a$$$", "(BLOCK (PARA (LITERAL-MATH () \"$a\") (TEXT \"$\")))"},
{`$\$`, "(BLOCK (PARA (TEXT \"$$\")))"},
{`$\$$`, "(BLOCK (PARA (TEXT \"$$$\")))"},
{`$$\$`, "(BLOCK (PARA (TEXT \"$$$\")))"},
{`$$a\$$`, "(BLOCK (PARA (LITERAL-MATH () \"a\\\\\")))"},
{`$$a$\$`, "(BLOCK (PARA (TEXT \"$$a$$\")))"},
{`$$a\$$$`, "(BLOCK (PARA (LITERAL-MATH () \"a\\\\\") (TEXT \"$\")))"},
{"$$a$${go}", "(BLOCK (PARA (LITERAL-MATH ((\"go\" . \"\")) \"a\")))"},
})
}
func TestMixFormatCode(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"__abc__\n**def**", "(BLOCK (PARA (FORMAT-EMPH () (TEXT \"abc\")) (SOFT) (FORMAT-STRONG () (TEXT \"def\"))))"},
{"''abc''\n==def==", "(BLOCK (PARA (LITERAL-INPUT () \"abc\") (SOFT) (LITERAL-OUTPUT () \"def\")))"},
{"__abc__\n==def==", "(BLOCK (PARA (FORMAT-EMPH () (TEXT \"abc\")) (SOFT) (LITERAL-OUTPUT () \"def\")))"},
{"__abc__\n``def``", "(BLOCK (PARA (FORMAT-EMPH () (TEXT \"abc\")) (SOFT) (LITERAL-CODE () \"def\")))"},
{
"\"\"ghi\"\"\n::abc::\n``def``\n",
"(BLOCK (PARA (FORMAT-QUOTE () (TEXT \"ghi\")) (SOFT) (FORMAT-SPAN () (TEXT \"abc\")) (SOFT) (LITERAL-CODE () \"def\")))",
},
})
}
func TestNDash(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"--", "(BLOCK (PARA (TEXT \"\u2013\")))"},
{"a--b", "(BLOCK (PARA (TEXT \"a\u2013b\")))"},
})
}
func TestEntity(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"&", "(BLOCK (PARA (TEXT \"&\")))"},
{"&;", "(BLOCK (PARA (TEXT \"&;\")))"},
{"&#;", "(BLOCK (PARA (TEXT \"&#;\")))"},
{"a;", "(BLOCK (PARA (TEXT \"a;\")))"},
{"&#x;", "(BLOCK (PARA (TEXT \"&#x;\")))"},
{"�z;", "(BLOCK (PARA (TEXT \"�z;\")))"},
{"&1;", "(BLOCK (PARA (TEXT \"&1;\")))"},
|
| ︙ | | |
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
|
{" ", "(BLOCK (PARA (TEXT \"\u00a0\")))"},
{"E: &,?;c.", "(BLOCK (PARA (TEXT \"E: &,?;c.\")))"},
})
}
func TestVerbatimZettel(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"@@@\n@@@", "()"},
checkTcs(t, testCases{
{"@@@\n@@@", "(BLOCK)"},
{"@@@\nabc\n@@@", "(BLOCK (VERBATIM-ZETTEL () \"abc\"))"},
{"@@@@def\nabc\n@@@@", "(BLOCK (VERBATIM-ZETTEL ((\"\" . \"def\")) \"abc\"))"},
})
}
func TestVerbatimCode(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"```\n```", "()"},
checkTcs(t, testCases{
{"```\n```", "(BLOCK)"},
{"```\nabc\n```", "(BLOCK (VERBATIM-CODE () \"abc\"))"},
{"```\nabc\n````", "(BLOCK (VERBATIM-CODE () \"abc\"))"},
{"````\nabc\n````", "(BLOCK (VERBATIM-CODE () \"abc\"))"},
{"````\nabc\n```\n````", "(BLOCK (VERBATIM-CODE () \"abc\\n```\"))"},
{"````go\nabc\n````", "(BLOCK (VERBATIM-CODE ((\"\" . \"go\")) \"abc\"))"},
})
}
func TestVerbatimEval(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"~~~\n~~~", "()"},
checkTcs(t, testCases{
{"~~~\n~~~", "(BLOCK)"},
{"~~~\nabc\n~~~", "(BLOCK (VERBATIM-EVAL () \"abc\"))"},
{"~~~\nabc\n~~~~", "(BLOCK (VERBATIM-EVAL () \"abc\"))"},
{"~~~~\nabc\n~~~~", "(BLOCK (VERBATIM-EVAL () \"abc\"))"},
{"~~~~\nabc\n~~~\n~~~~", "(BLOCK (VERBATIM-EVAL () \"abc\\n~~~\"))"},
{"~~~~go\nabc\n~~~~", "(BLOCK (VERBATIM-EVAL ((\"\" . \"go\")) \"abc\"))"},
})
}
func TestVerbatimMath(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"$$$\n$$$", "()"},
checkTcs(t, testCases{
{"$$$\n$$$", "(BLOCK)"},
{"$$$\nabc\n$$$", "(BLOCK (VERBATIM-MATH () \"abc\"))"},
{"$$$\nabc\n$$$$", "(BLOCK (VERBATIM-MATH () \"abc\"))"},
{"$$$$\nabc\n$$$$", "(BLOCK (VERBATIM-MATH () \"abc\"))"},
{"$$$$\nabc\n$$$\n$$$$", "(BLOCK (VERBATIM-MATH () \"abc\\n$$$\"))"},
{"$$$$go\nabc\n$$$$", "(BLOCK (VERBATIM-MATH ((\"\" . \"go\")) \"abc\"))"},
})
}
func TestVerbatimComment(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"%%%\n%%%", "()"},
checkTcs(t, testCases{
{"%%%\n%%%", "(BLOCK)"},
{"%%%\nabc\n%%%", "(BLOCK (VERBATIM-COMMENT () \"abc\"))"},
{"%%%%go\nabc\n%%%%", "(BLOCK (VERBATIM-COMMENT ((\"\" . \"go\")) \"abc\"))"},
})
}
func TestPara(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"a\n\nb", "(BLOCK (PARA (TEXT \"a\")) (PARA (TEXT \"b\")))"},
{"a\n \nb", "(BLOCK (PARA (TEXT \"a\")) (PARA (TEXT \"b\")))"},
})
}
func TestSpanRegion(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{":::\n:::", "()"},
checkTcs(t, testCases{
{":::\n:::", "(BLOCK)"},
{":::\nabc\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\")))))"},
{":::\nabc\n::::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\")))))"},
{"::::\nabc\n::::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\")))))"},
{"::::\nabc\n:::\ndef\n:::\n::::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\")) (REGION-BLOCK () ((PARA (TEXT \"def\")))))))"},
{":::{go}\n:::a", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) () (TEXT \"a\")))"},
{":::\nabc\n::: def ", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\"))) (TEXT \"def\")))"},
})
}
func TestQuoteRegion(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"<<<\n<<<", "()"},
checkTcs(t, testCases{
{"<<<\n<<<", "(BLOCK)"},
{"<<<\nabc\n<<<", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\")))))"},
{"<<<\nabc\n<<<<", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\")))))"},
{"<<<<\nabc\n<<<<", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\")))))"},
{"<<<<\nabc\n<<<\ndef\n<<<\n<<<<", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\")) (REGION-QUOTE () ((PARA (TEXT \"def\")))))))"},
{"<<<go\n<<< a", "(BLOCK (REGION-QUOTE ((\"\" . \"go\")) () (TEXT \"a\")))"},
{"<<<\nabc\n<<< def ", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\"))) (TEXT \"def\")))"},
})
}
func TestVerseRegion(t *testing.T) {
t.Parallel()
checkTcs(t, replace("\"", nil, TestCases{
{"$$$\n$$$", "()"},
checkTcs(t, replace("\"", nil, testCases{
{"$$$\n$$$", "(BLOCK)"},
{"$$$\nabc\n$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\")))))"},
{"$$$\nabc\n$$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\")))))"},
{"$$$$\nabc\n$$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\")))))"},
{"$$$\nabc\ndef\n$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\") (HARD) (TEXT \"def\")))))"},
{"$$$$\nabc\n$$$\ndef\n$$$\n$$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\")) (REGION-VERSE () ((PARA (TEXT \"def\")))))))"},
{"$$$go\n$$$x", "(BLOCK (REGION-VERSE ((\"\" . \"go\")) () (TEXT \"x\")))"},
{"$$$\nabc\n$$$ def ", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\"))) (TEXT \"def\")))"},
{"$$$\n space \n$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"\u00a0space\u00a0\")))))"},
{"$$$\n spaces \n$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"\u00a0\u00a0spaces\u00a0\u00a0\")))))"},
{"$$$\n spaces \n space \n$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"\u00a0\u00a0spaces\u00a0\u00a0\") (HARD) (TEXT \"\u00a0space\u00a0\u00a0\")))))"},
{"$$$\n space space \n$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"\u00a0space\u00a0space\u00a0\")))))"},
}))
}
func TestHeading(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"=h", "(BLOCK (PARA (TEXT \"=h\")))"},
{"= h", "(BLOCK (PARA (TEXT \"= h\")))"},
{"==h", "(BLOCK (PARA (TEXT \"==h\")))"},
{"== h", "(BLOCK (PARA (TEXT \"== h\")))"},
{"===h", "(BLOCK (PARA (TEXT \"===h\")))"},
{"===", "(BLOCK (PARA (TEXT \"===\")))"},
{"=== h", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\")))"},
|
| ︙ | | |
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
-
+
-
+
-
+
|
{"=== h{id=abc}", "(BLOCK (HEADING 1 ((\"id\" . \"abc\")) \"\" \"\" (TEXT \"h\")))"},
{"=== h\n=== h", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\")) (HEADING 1 () \"\" \"\" (TEXT \"h\")))"},
})
}
func TestHRule(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"-", "(BLOCK (PARA (TEXT \"-\")))"},
{"---", "(BLOCK (THEMATIC ()))"},
{"----", "(BLOCK (THEMATIC ()))"},
{"---A", "(BLOCK (THEMATIC ((\"\" . \"A\"))))"},
{"---A-", "(BLOCK (THEMATIC ((\"\" . \"A-\"))))"},
{"-1", "(BLOCK (PARA (TEXT \"-1\")))"},
{"2-1", "(BLOCK (PARA (TEXT \"2-1\")))"},
{"--- { go } ", "(BLOCK (THEMATIC ((\"go\" . \"\"))))"},
{"--- { .go } ", "(BLOCK (THEMATIC ((\"class\" . \"go\"))))"},
{`---{lang=zmk}`, "(BLOCK (THEMATIC ((\"lang\" . \"zmk\"))))"},
{`---{lang="zmk"}`, "(BLOCK (THEMATIC ((\"lang\" . \"zmk\"))))"},
})
}
func TestList(t *testing.T) {
t.Parallel()
// No ">" in the following, because quotation lists may have empty items.
for _, ch := range []string{"*", "#"} {
checkTcs(t, replace(ch, nil, TestCases{
checkTcs(t, replace(ch, nil, testCases{
{"$", "(BLOCK (PARA (TEXT \"$\")))"},
{"$$", "(BLOCK (PARA (TEXT \"$$\")))"},
{"$$$", "(BLOCK (PARA (TEXT \"$$$\")))"},
{"$ ", "(BLOCK (PARA (TEXT \"$\")))"},
{"$$ ", "(BLOCK (PARA (TEXT \"$$\")))"},
{"$$$ ", "(BLOCK (PARA (TEXT \"$$$\")))"},
}))
}
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"* abc", "(BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"abc\")))))"},
{"** abc", "(BLOCK (UNORDERED () (BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"abc\")))))))"},
{"*** abc", "(BLOCK (UNORDERED () (BLOCK (UNORDERED () (BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"abc\")))))))))"},
{"**** abc", "(BLOCK (UNORDERED () (BLOCK (UNORDERED () (BLOCK (UNORDERED () (BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"abc\")))))))))))"},
{"** abc\n**** def", "(BLOCK (UNORDERED () (BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"abc\")) (UNORDERED () (BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"def\")))))))))))"},
{"* abc\ndef", "(BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"abc\")))) (PARA (TEXT \"def\")))"},
{"* abc\n def", "(BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"abc\")))) (PARA (TEXT \"def\")))"},
|
| ︙ | | |
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
-
+
-
+
-
+
-
+
|
// Empty continuation
{"* abc\n ", "(BLOCK (UNORDERED () (BLOCK (PARA (TEXT \"abc\")))))"},
})
}
func TestQuoteList(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"> w1 w2", "(BLOCK (QUOTATION () (BLOCK (PARA (TEXT \"w1 w2\")))))"},
{"> w1\n> w2", "(BLOCK (QUOTATION () (BLOCK (PARA (TEXT \"w1\") (SOFT) (TEXT \"w2\")))))"},
{"> w1\n>w2", "(BLOCK (QUOTATION () (BLOCK (PARA (TEXT \"w1\")))) (PARA (TEXT \">w2\")))"},
{"> w1\n>\n>w2", "(BLOCK (QUOTATION () (BLOCK (PARA (TEXT \"w1\"))) (BLOCK)) (PARA (TEXT \">w2\")))"},
{"> w1\n> \n> w2", "(BLOCK (QUOTATION () (BLOCK (PARA (TEXT \"w1\"))) (BLOCK) (BLOCK (PARA (TEXT \"w2\")))))"},
})
}
func TestEnumAfterPara(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"abc\n* def", "(BLOCK (PARA (TEXT \"abc\")) (UNORDERED () (BLOCK (PARA (TEXT \"def\")))))"},
{"abc\n*def", "(BLOCK (PARA (TEXT \"abc\") (SOFT) (TEXT \"*def\")))"},
})
}
func TestDefinition(t *testing.T) {
func TestDescription(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{";", "(BLOCK (PARA (TEXT \";\")))"},
{"; ", "(BLOCK (PARA (TEXT \";\")))"},
{"; abc", "(BLOCK (DESCRIPTION () ((TEXT \"abc\"))))"},
{"; abc\ndef", "(BLOCK (DESCRIPTION () ((TEXT \"abc\"))) (PARA (TEXT \"def\")))"},
{"; abc\n def", "(BLOCK (DESCRIPTION () ((TEXT \"abc\"))) (PARA (TEXT \"def\")))"},
{"; abc\n def", "(BLOCK (DESCRIPTION () ((TEXT \"abc\") (SOFT) (TEXT \"def\"))))"},
{"; abc\n def\n ghi", "(BLOCK (DESCRIPTION () ((TEXT \"abc\") (SOFT) (TEXT \"def\") (SOFT) (TEXT \"ghi\"))))"},
|
| ︙ | | |
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
|
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
|
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
|
{"; abc\n: def\n\n ghi\n\n jkl", "(BLOCK (DESCRIPTION () ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\")) (PARA (TEXT \"ghi\")) (PARA (TEXT \"jkl\"))))))"},
{"; abc\n:", "(BLOCK (DESCRIPTION () ((TEXT \"abc\"))) (PARA (TEXT \":\")))"},
{"; abc\n: def\n: ghi", "(BLOCK (DESCRIPTION () ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\"))) (BLOCK (PARA (TEXT \"ghi\"))))))"},
{"; abc\n: def\n; ghi\n: jkl", "(BLOCK (DESCRIPTION () ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\")))) ((TEXT \"ghi\")) (BLOCK (BLOCK (PARA (TEXT \"jkl\"))))))"},
// Empty description
{"; abc\n: ", "(BLOCK (DESCRIPTION () ((TEXT \"abc\"))) (PARA (TEXT \":\")))"},
{"; abc\n; def\n: ghi", "(BLOCK (DESCRIPTION () ((TEXT \"abc\")) (BLOCK) ((TEXT \"def\")) (BLOCK (BLOCK (PARA (TEXT \"ghi\"))))))"},
// Empty continuation of definition
{"; abc\n: def\n ", "(BLOCK (DESCRIPTION () ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\"))))))"},
{src: "; T1\n: D1\n\n D2\n: T2\n T3",
exp: `(BLOCK (DESCRIPTION () ((TEXT "T1")) (BLOCK (BLOCK (PARA (TEXT "D1")) (PARA (TEXT "D2"))) (BLOCK (PARA (TEXT "T2") (SOFT) (TEXT "T3"))))))`},
{src: "; T1\n: D1\n\n D2\n\n D3\n: T2\n T3",
exp: `(BLOCK (DESCRIPTION () ((TEXT "T1")) (BLOCK (BLOCK (PARA (TEXT "D1")) (PARA (TEXT "D2")) (PARA (TEXT "D3"))) (BLOCK (PARA (TEXT "T2") (SOFT) (TEXT "T3"))))))`},
})
}
func TestTable(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"|", "()"},
{"||", "(BLOCK (TABLE () ((CELL ()))))"},
{"| |", "(BLOCK (TABLE () ((CELL ()))))"},
{"|a", "(BLOCK (TABLE () ((CELL () (TEXT \"a\")))))"},
{"|a|", "(BLOCK (TABLE () ((CELL () (TEXT \"a\")))))"},
{"|a| ", "(BLOCK (TABLE () ((CELL () (TEXT \"a\")) (CELL ()))))"},
{"|a|b", "(BLOCK (TABLE () ((CELL () (TEXT \"a\")) (CELL () (TEXT \"b\")))))"},
{"|a\n|b", "(BLOCK (TABLE () ((CELL () (TEXT \"a\"))) ((CELL () (TEXT \"b\")))))"},
{"|a|b\n|c|d", "(BLOCK (TABLE () ((CELL () (TEXT \"a\")) (CELL () (TEXT \"b\"))) ((CELL () (TEXT \"c\")) (CELL () (TEXT \"d\")))))"},
{"|%", "()"},
{"|=a", "(BLOCK (TABLE ((CELL () (TEXT \"a\")))))"},
{"|=a\n|b", "(BLOCK (TABLE ((CELL () (TEXT \"a\"))) ((CELL () (TEXT \"b\")))))"},
{"|a|b\n|%---\n|c|d", "(BLOCK (TABLE () ((CELL () (TEXT \"a\")) (CELL () (TEXT \"b\"))) ((CELL () (TEXT \"c\")) (CELL () (TEXT \"d\")))))"},
{"|a|b\n|c", "(BLOCK (TABLE () ((CELL () (TEXT \"a\")) (CELL () (TEXT \"b\"))) ((CELL () (TEXT \"c\")) (CELL ()))))"},
{"|=<a>\n|b|c", "(BLOCK (TABLE ((CELL ((align . \"left\")) (TEXT \"a\")) (CELL ())) ((CELL ((align . \"right\")) (TEXT \"b\")) (CELL () (TEXT \"c\")))))"},
{"|=<a|=b>\n||", "(BLOCK (TABLE ((CELL ((align . \"left\")) (TEXT \"a\")) (CELL ((align . \"right\")) (TEXT \"b\"))) ((CELL ()) (CELL ((align . \"right\"))))))"},
checkTcs(t, testCases{
{"|", "(BLOCK)"},
{"||", "(BLOCK (TABLE () () ((CELL ()))))"},
{"| |", "(BLOCK (TABLE () () ((CELL ()))))"},
{"|a", "(BLOCK (TABLE () () ((CELL () (TEXT \"a\")))))"},
{"|a|", "(BLOCK (TABLE () () ((CELL () (TEXT \"a\")))))"},
{"|a| ", "(BLOCK (TABLE () () ((CELL () (TEXT \"a\")) (CELL ()))))"},
{"|a|b", "(BLOCK (TABLE () () ((CELL () (TEXT \"a\")) (CELL () (TEXT \"b\")))))"},
{"|a\n|b", "(BLOCK (TABLE () () ((CELL () (TEXT \"a\"))) ((CELL () (TEXT \"b\")))))"},
{"|a|b\n|c|d", "(BLOCK (TABLE () () ((CELL () (TEXT \"a\")) (CELL () (TEXT \"b\"))) ((CELL () (TEXT \"c\")) (CELL () (TEXT \"d\")))))"},
{"|%", "(BLOCK)"},
{"|=a", "(BLOCK (TABLE () ((CELL () (TEXT \"a\")))))"},
{"|=a\n|b", "(BLOCK (TABLE () ((CELL () (TEXT \"a\"))) ((CELL () (TEXT \"b\")))))"},
{"|a|b\n|%---\n|c|d", "(BLOCK (TABLE () () ((CELL () (TEXT \"a\")) (CELL () (TEXT \"b\"))) ((CELL () (TEXT \"c\")) (CELL () (TEXT \"d\")))))"},
{"|a|b\n|c", "(BLOCK (TABLE () () ((CELL () (TEXT \"a\")) (CELL () (TEXT \"b\"))) ((CELL () (TEXT \"c\")) (CELL ()))))"},
{"|=<a>\n|b|c", "(BLOCK (TABLE () ((CELL ((align . \"left\")) (TEXT \"a\")) (CELL ())) ((CELL ((align . \"right\")) (TEXT \"b\")) (CELL () (TEXT \"c\")))))"},
{"|=<a|=b>\n||", "(BLOCK (TABLE () ((CELL ((align . \"left\")) (TEXT \"a\")) (CELL ((align . \"right\")) (TEXT \"b\"))) ((CELL ()) (CELL ((align . \"right\"))))))"},
})
}
func TestTransclude(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"{{{a}}}", "(BLOCK (TRANSCLUDE () (HOSTED \"a\")))"},
{"{{{a}}}b", "(BLOCK (TRANSCLUDE ((\"\" . \"b\")) (HOSTED \"a\")))"},
{"{{{a}}}}", "(BLOCK (TRANSCLUDE () (HOSTED \"a\")))"},
{"{{{a\\}}}}", "(BLOCK (TRANSCLUDE () (INVALID \"a\\\\}\")))"},
{"{{{a\\}}}}b", "(BLOCK (TRANSCLUDE ((\"\" . \"b\")) (INVALID \"a\\\\}\")))"},
{"{{{a}}", "(BLOCK (PARA (TEXT \"{\") (EMBED () (HOSTED \"a\") \"\")))"},
{"{{{a}}}{go=b}", "(BLOCK (TRANSCLUDE ((\"go\" . \"b\")) (HOSTED \"a\")))"},
})
}
func TestBlockAttr(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{":::go\na\n:::", "(BLOCK (REGION-BLOCK ((\"\" . \"go\")) ((PARA (TEXT \"a\")))))"},
{":::go=\na\n:::", "(BLOCK (REGION-BLOCK ((\"\" . \"go\")) ((PARA (TEXT \"a\")))))"},
{":::{}\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
{":::{ }\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
{":::{.go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go\")) ((PARA (TEXT \"a\")))))"},
{":::{=go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"\" . \"go\")) ((PARA (TEXT \"a\")))))"},
{":::{go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
{":::{go=py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"py\")) ((PARA (TEXT \"a\")))))"},
{":::{.go=py}\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
{":::{go=}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
{":::{.go=}\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
{":::{go py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\") (\"py\" . \"\")) ((PARA (TEXT \"a\")))))"},
{":::{go\npy}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\") (\"py\" . \"\")) ((PARA (TEXT \"a\")))))"},
{":::{.go py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go\") (\"py\" . \"\")) ((PARA (TEXT \"a\")))))"},
{":::{go .py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"py\") (\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
{":::{.go py=3}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go\") (\"py\" . \"3\")) ((PARA (TEXT \"a\")))))"},
{"::: { go } \na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
{"::: { .go } \na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go\")) ((PARA (TEXT \"a\")))))"},
})
checkTcs(t, replace("\"", nil, TestCases{
checkTcs(t, replace("\"", nil, testCases{
{":::{py=3}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"3\")) ((PARA (TEXT \"a\")))))"},
{":::{py=$2 3$}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2 3\")) ((PARA (TEXT \"a\")))))"},
{":::{py=$2\\$3$}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2\\\"3\")) ((PARA (TEXT \"a\")))))"},
{":::{py=2$3}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2\\\"3\")) ((PARA (TEXT \"a\")))))"},
{":::{py=$2\n3$}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2\\n3\")) ((PARA (TEXT \"a\")))))"},
{":::{py=$2 3}\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
{":::{py=2 py=3}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2 3\")) ((PARA (TEXT \"a\")))))"},
{":::{.go .py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go py\")) ((PARA (TEXT \"a\")))))"},
{":::{go go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
{":::{=py =go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"\" . \"go\")) ((PARA (TEXT \"a\")))))"},
}))
}
func TestInlineAttr(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"::a::{}", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\"))))"},
{"::a::{ }", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\"))))"},
{"::a::{.go}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"go\")) (TEXT \"a\"))))"},
{"::a::{=go}", "(BLOCK (PARA (FORMAT-SPAN ((\"\" . \"go\")) (TEXT \"a\"))))"},
{"::a::{go}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\")) (TEXT \"a\"))))"},
{"::a::{go=py}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"py\")) (TEXT \"a\"))))"},
{"::a::{.go=py}", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\")) (TEXT \"{.go=py}\")))"},
{"::a::{go=}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\")) (TEXT \"a\"))))"},
{"::a::{.go=}", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\")) (TEXT \"{.go=}\")))"},
{"::a::{go py}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\") (\"py\" . \"\")) (TEXT \"a\"))))"},
{"::a::{go\npy}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\") (\"py\" . \"\")) (TEXT \"a\"))))"},
{"::a::{.go py}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"go\") (\"py\" . \"\")) (TEXT \"a\"))))"},
{"::a::{go .py}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"py\") (\"go\" . \"\")) (TEXT \"a\"))))"},
{"::a::{ \n go \n .py\n \n}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"py\") (\"go\" . \"\")) (TEXT \"a\"))))"},
{"::a::{ \n go \n .py\n\n}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"py\") (\"go\" . \"\")) (TEXT \"a\"))))"},
{"::a::{\ngo\n}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\")) (TEXT \"a\"))))"},
})
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"::a::{py=3}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"3\")) (TEXT \"a\"))))"},
{"::a::{py=\"2 3\"}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2 3\")) (TEXT \"a\"))))"},
{"::a::{py=\"2\\\"3\"}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2\\\"3\")) (TEXT \"a\"))))"},
{"::a::{py=2\"3}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2\\\"3\")) (TEXT \"a\"))))"},
{"::a::{py=\"2\n3\"}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2\\n3\")) (TEXT \"a\"))))"},
{"::a::{py=\"2 3}", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\")) (TEXT \"{py=\\\"2 3}\")))"},
})
checkTcs(t, TestCases{
checkTcs(t, testCases{
{"::a::{py=2 py=3}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2 3\")) (TEXT \"a\"))))"},
{"::a::{.go .py}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"go py\")) (TEXT \"a\"))))"},
})
}
func TestTemp(t *testing.T) {
t.Parallel()
checkTcs(t, TestCases{
{"", "()"},
checkTcs(t, testCases{
{"", "(BLOCK)"},
})
}
|