fig 2.5.0

Parse, edit, and convert config files while preserving comments. Supports JSON, YAML, TOML, and more.
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
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
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
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
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
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
731
732
733
734
735
736
737
738
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
862
863
864
865
866
867
868
869
870
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
//! Embedded config extraction: pull a config document out of a host file
//! (e.g. YAML frontmatter inside markdown) and parse it correctly.
const std = @import("std");
const Allocator = std.mem.Allocator;

const Language = @import("languages/language.zig");
const Document = @import("document.zig");
const Span = @import("util/span.zig");
const build_options = @import("build_options");

const Embed = @This();

const Delimiter = struct {
    /// The exact line bodies this delimiter matches — used only by the FIXED
    /// match modes (`whole_line`/`line_trimmed`/`prefix`). The PARAMETRIC modes
    /// (`fenced_open`/`frontmatter_open`/`script_open`) ignore `tokens`: they
    /// parse a language tag out of the line and resolve it, carrying the expected
    /// format in the mode payload instead.
    tokens: []const []const u8 = &.{},
    match: Match,
    /// The exact text emitted for this delimiter when *synthesizing* a region
    /// (`initRegion`/`retype`), when it differs from `tokens[0]`. Required for the
    /// parametric opens, whose match logic reads a tag rather than a literal —
    /// e.g. `fenced` emits ```` ```toml ````, `frontmatter` emits `---toml` (or a
    /// bare `---` for YAML), `script` emits the full `<script type="">` tag.
    /// Null ⇒ emit `tokens[0]` verbatim.
    literal: ?[]const u8 = null,
};

/// How a `Delimiter` decides whether a host line is that delimiter.
const Match = union(enum) {
    /// The line, trimmed of trailing space/tab, equals a token exactly. Leading
    /// whitespace is significant (a markdown fence must sit at column 0), so an
    /// indented line never matches.
    whole_line,
    /// The line (untrimmed) starts with a token.
    prefix,
    /// The line, trimmed of leading AND trailing whitespace, equals a token
    /// exactly — the indentation-tolerant `whole_line`, for delimiters that may
    /// sit indented inside a host (e.g. a `</script>` close tag nested in a
    /// `<head>`).
    line_trimmed,
    /// The line opens a ```` ```<lang> ```` fenced block whose `<lang>` resolves
    /// (`formatFromLangTag`) to this exact format. A bare ```` ``` ```` (no tag)
    /// is a close, not an open, and never matches.
    fenced_open: InnerFormat,
    /// The line opens a `---<lang>` markdown-frontmatter block whose `<lang>`
    /// resolves to this format; a bare `---` (no tag) resolves to `.yaml`.
    frontmatter_open: InnerFormat,
    /// The line, trimmed both sides, is an HTML `<script >` open tag (on its own
    /// line) whose `type` attribute's MIME resolves (`formatFromScriptMime`) to
    /// this format. Attribute order, quoting, and whitespace are tolerated; a
    /// same-line block (`<script>body</script>`) is deliberately not matched.
    script_open: InnerFormat,
    /// The line opens an HTML `<code>` block (optionally wrapped in `<pre>`) whose
    /// `class` list carries a `language-<lang>` token resolving to this format.
    /// See `parseCodeOpen`.
    code_open: InnerFormat,
};

/// The format an archetype's content is written in. A parametric archetype
/// carries one of these as its parameter (`fenced`/`frontmatter`/`html_script`);
/// the blessed presets each pin one. Named so callers outside this file can name
/// it too — see `innerFormat`.
pub const InnerFormat = enum { yaml, json, fig, toml };

/// Resolve a `fenced`/`frontmatter` language tag (a code-fence info string's
/// first token, or a `---<tag>`) to a config format — or null when it is not one
/// this project understands, so a ```` ```python ```` code sample or a `---foo`
/// line is left alone rather than mistaken for embedded config. `yml`/`figl` are
/// accepted spellings of `yaml`/`fig`.
fn formatFromLangTag(tag: []const u8) ?InnerFormat {
    const eq = std.ascii.eqlIgnoreCase;
    if (eq(tag, "yaml") or eq(tag, "yml")) return .yaml;
    if (eq(tag, "json")) return .json;
    if (eq(tag, "toml")) return .toml;
    if (eq(tag, "fig") or eq(tag, "figl")) return .fig;
    return null;
}

/// Resolve an HTML `<script type>` MIME to a config format, or null. Accepts the
/// canonical `application/<lang>` plus a few established aliases (`ld+json` for
/// JSON-LD, `application/figl` for the fig authoring dialect, `x-yaml`/`text/*`
/// for YAML). Mirrors `formatFromLangTag` for the HTML projection.
fn formatFromScriptMime(mime: []const u8) ?InnerFormat {
    const eq = std.ascii.eqlIgnoreCase;
    if (eq(mime, "application/yaml") or eq(mime, "application/x-yaml") or eq(mime, "text/yaml")) return .yaml;
    if (eq(mime, "application/json") or eq(mime, "application/ld+json")) return .json;
    if (eq(mime, "application/toml")) return .toml;
    if (eq(mime, "application/figl") or eq(mime, "application/fig")) return .fig;
    return null;
}

/// The canonical ```` ```<lang> ```` open fence a `fenced` archetype emits.
fn fencedLiteral(f: InnerFormat) []const u8 {
    return switch (f) {
        .yaml => "```yaml",
        .json => "```json",
        .toml => "```toml",
        .fig => "```fig",
    };
}

/// The canonical `---<lang>` open a `frontmatter` archetype emits — a bare `---`
/// for YAML (the ecosystem default), a tagged `---<lang>` otherwise.
fn frontmatterLiteral(f: InnerFormat) []const u8 {
    return switch (f) {
        .yaml => "---",
        .json => "---json",
        .toml => "---toml",
        .fig => "---fig",
    };
}

/// The canonical `<script type="…">` open tag an `html_script` archetype emits.
fn scriptLiteral(f: InnerFormat) []const u8 {
    return switch (f) {
        .yaml => "<script type=\"application/yaml\">",
        .json => "<script type=\"application/json\">",
        .toml => "<script type=\"application/toml\">",
        .fig => "<script type=\"application/figl\">",
    };
}

/// The canonical `<pre><code class="language-…">` open an `html_code` archetype
/// emits (closed by `</code></pre>`).
fn codeLiteral(f: InnerFormat) []const u8 {
    return switch (f) {
        .yaml => "<pre><code class=\"language-yaml\">",
        .json => "<pre><code class=\"language-json\">",
        .toml => "<pre><code class=\"language-toml\">",
        .fig => "<pre><code class=\"language-figl\">",
    };
}

/// The first whitespace-delimited token of `s` (a code-fence info string may
/// carry extra words after the language; "first token wins").
fn firstToken(s: []const u8) []const u8 {
    const t = std.mem.trim(u8, s, " \t");
    const end = std.mem.indexOfAny(u8, t, " \t") orelse return t;
    return t[0..end];
}

/// If already-trimmed `t` opens a ```` ```<lang> ```` config fence, the format;
/// else null (a bare ```` ``` ```` close, or a non-config info string).
fn parseFencedOpen(t: []const u8) ?InnerFormat {
    if (!std.mem.startsWith(u8, t, "```")) return null;
    const tag = std.mem.trim(u8, t["```".len..], " \t");
    if (tag.len == 0) return null; // bare ``` is a close
    return formatFromLangTag(firstToken(tag));
}

/// If already-trimmed `t` opens a `---<lang>` frontmatter block, the format
/// (bare `---` ⇒ `.yaml`); else null. `----`, `--- somescalar`, and other
/// non-tag lines resolve to null, preserving the "only bare `---` or a known
/// `---<lang>`" rule.
fn parseFrontmatterOpen(t: []const u8) ?InnerFormat {
    if (!std.mem.startsWith(u8, t, "---")) return null;
    const rest = std.mem.trim(u8, t["---".len..], " \t");
    if (rest.len == 0) return .yaml;
    return formatFromLangTag(firstToken(rest));
}

/// If already-trimmed `t` is an HTML `<script …>` open tag (on its own line)
/// with a config `type`, the resolved format; else null. See `Match.script_open`.
fn parseScriptOpen(t: []const u8) ?InnerFormat {
    const tag = "<script";
    if (!std.ascii.startsWithIgnoreCase(t, tag)) return null; // HTML folds tag-name case
    const rest = t[tag.len..];
    // The char after `<script` must be whitespace or `>` — never a name char, so
    // `<scripts …>` (a different element) is not mistaken for `<script …>`.
    if (rest.len == 0 or (!isHspace(rest[0]) and rest[0] != '>')) return null;
    const gt = std.mem.indexOfScalar(u8, rest, '>') orelse return null;
    if (gt != rest.len - 1) return null; // `>` must end the line
    const mime = scriptAttrValue(rest[0..gt], "type") orelse return null;
    return formatFromScriptMime(mime);
}

/// If already-trimmed `t` opens an HTML `<code>` block (optionally wrapped in a
/// leading `<pre>`) whose `class` list contains a `language-<lang>`/`lang-<lang>`
/// token resolving to a config format, that format; else null. The tag(s) must
/// stand on their own line, closing with `>` — a same-line block is rejected, as
/// for `<script>`.
fn parseCodeOpen(t: []const u8) ?InnerFormat {
    var s = t;
    // Strip an optional leading `<pre …>` wrapper (`<pre><code …>` on one line).
    if (std.ascii.startsWithIgnoreCase(s, "<pre")) {
        if (s.len < 4 or (!isHspace(s[4]) and s[4] != '>')) return null;
        const gt = std.mem.indexOfScalar(u8, s, '>') orelse return null;
        s = std.mem.trimStart(u8, s[gt + 1 ..], " \t");
    }
    const tag = "<code";
    if (!std.ascii.startsWithIgnoreCase(s, tag)) return null;
    const rest = s[tag.len..];
    if (rest.len == 0 or (!isHspace(rest[0]) and rest[0] != '>')) return null;
    const gt = std.mem.indexOfScalar(u8, rest, '>') orelse return null;
    if (gt != rest.len - 1) return null; // `>` must end the line
    const class = scriptAttrValue(rest[0..gt], "class") orelse return null;
    // The class attribute is a whitespace-separated token list; the highlighter
    // convention is `language-<lang>` (also `lang-<lang>`).
    var it = std.mem.tokenizeAny(u8, class, " \t");
    while (it.next()) |tok| {
        const lang = if (std.mem.startsWith(u8, tok, "language-"))
            tok["language-".len..]
        else if (std.mem.startsWith(u8, tok, "lang-"))
            tok["lang-".len..]
        else
            continue;
        if (formatFromLangTag(lang)) |f| return f;
    }
    return null;
}

// --- HTML entity codec (span-aware) --------------------------------------

/// One decoded↔source correspondence within a `Decoded`. `text[d_start..][0..
/// d_len]` was produced from `src[s_start..][0..s_len]`. An identity run has
/// `d_len == s_len`; an entity has `s_len` = the entity's source length and
/// `d_len` = its decoded UTF-8 length (`&lt;`→`<` is d_len 1, s_len 4).
const Seg = struct { d_start: usize, d_len: usize, s_start: usize, s_len: usize };

/// The decode of a codec content slice: the decoded `text` plus a `segs`
/// provenance map. The map lets an edit re-encode only the bytes it changed
/// (keeping every untouched byte's ORIGINAL encoding) and lets a parsed node span
/// lift back to source coordinates. For `identity` content `text` borrows the
/// slice, `owned_text` is null, and `segs` is empty.
pub const Decoded = struct {
    text: []const u8,
    owned_text: ?[]u8 = null,
    segs: []const Seg = &.{},

    pub fn deinit(self: Decoded, allocator: Allocator) void {
        if (self.owned_text) |t| allocator.free(t);
        if (self.segs.len != 0) allocator.free(@constCast(self.segs));
    }

    /// The source offset (within the content slice) for decoded offset `d`.
    /// Linear inside an identity run; an entity is atomic, so a `d` that lands
    /// inside a multi-byte entity expansion snaps to the entity's source start —
    /// harmless because a cut never lands there (see `reencodeEdited`). With no
    /// segments (identity content) the mapping is the identity `d`.
    fn srcAt(self: Decoded, d: usize) usize {
        for (self.segs) |g| {
            if (d < g.d_start + g.d_len)
                return if (g.d_len == g.s_len) g.s_start + (d - g.d_start) else g.s_start;
        }
        if (self.segs.len == 0) return d;
        const last = self.segs[self.segs.len - 1];
        return last.s_start + last.s_len;
    }
};

const Entity = struct {
    val: union(enum) { literal: []const u8, codepoint: u21 },
    src_len: usize,
};

/// Parse an HTML entity starting at `s[0] == '&'`, or null if `s` doesn't open a
/// recognized one (then `&` is a literal). Handles the five core named entities
/// and numeric `&#dd;` / `&#xhh;`; other named entities are intentionally not
/// decoded (rare in config — documented).
fn parseEntity(s: []const u8) ?Entity {
    const semi = std.mem.indexOfScalarPos(u8, s, 1, ';') orelse return null;
    if (semi > 12) return null; // longer than any entity we accept
    const body = s[1..semi];
    if (body.len == 0) return null;
    const src_len = semi + 1;
    if (std.mem.eql(u8, body, "lt")) return .{ .val = .{ .literal = "<" }, .src_len = src_len };
    if (std.mem.eql(u8, body, "gt")) return .{ .val = .{ .literal = ">" }, .src_len = src_len };
    if (std.mem.eql(u8, body, "amp")) return .{ .val = .{ .literal = "&" }, .src_len = src_len };
    if (std.mem.eql(u8, body, "quot")) return .{ .val = .{ .literal = "\"" }, .src_len = src_len };
    if (std.mem.eql(u8, body, "apos")) return .{ .val = .{ .literal = "'" }, .src_len = src_len };
    if (body[0] == '#') {
        const cp: u21 = blk: {
            if (body.len > 1 and (body[1] == 'x' or body[1] == 'X'))
                break :blk std.fmt.parseInt(u21, body[2..], 16) catch return null
            else
                break :blk std.fmt.parseInt(u21, body[1..], 10) catch return null;
        };
        if (cp > 0x10FFFF or (cp >= 0xD800 and cp <= 0xDFFF)) return null; // invalid scalar
        return .{ .val = .{ .codepoint = cp }, .src_len = src_len };
    }
    return null;
}

/// Decode HTML-entity-encoded `src` into decoded text plus its provenance map.
fn decodeEntities(allocator: Allocator, src: []const u8) !Decoded {
    var text: std.ArrayList(u8) = .empty;
    errdefer text.deinit(allocator);
    var segs: std.ArrayList(Seg) = .empty;
    errdefer segs.deinit(allocator);

    var i: usize = 0;
    var run_s: usize = 0; // source start of the pending identity run
    var run_d: usize = 0; // decoded start of the pending identity run
    while (i < src.len) {
        if (src[i] == '&') {
            if (parseEntity(src[i..])) |ent| {
                if (i > run_s) // flush the identity run before this entity
                    try segs.append(allocator, .{ .d_start = run_d, .d_len = i - run_s, .s_start = run_s, .s_len = i - run_s });
                const d0 = text.items.len;
                switch (ent.val) {
                    .literal => |b| try text.appendSlice(allocator, b),
                    .codepoint => |cp| {
                        var buf: [4]u8 = undefined;
                        const n = std.unicode.utf8Encode(cp, &buf) catch unreachable; // validated in parseEntity
                        try text.appendSlice(allocator, buf[0..n]);
                    },
                }
                try segs.append(allocator, .{ .d_start = d0, .d_len = text.items.len - d0, .s_start = i, .s_len = ent.src_len });
                i += ent.src_len;
                run_s = i;
                run_d = text.items.len;
                continue;
            }
        }
        try text.append(allocator, src[i]);
        i += 1;
    }
    if (i > run_s)
        try segs.append(allocator, .{ .d_start = run_d, .d_len = i - run_s, .s_start = run_s, .s_len = i - run_s });

    const owned = try text.toOwnedSlice(allocator);
    return .{ .text = owned, .owned_text = owned, .segs = try segs.toOwnedSlice(allocator) };
}

/// Canonically HTML-entity-encode element-text `t` (`&`, `<`, `>` only — the
/// characters that are significant in `<code>` content; `"`/`'` matter only in
/// attributes). Caller owns the result.
fn encodeEntities(allocator: Allocator, t: []const u8) ![]u8 {
    var out: std.ArrayList(u8) = .empty;
    errdefer out.deinit(allocator);
    for (t) |c| switch (c) {
        '&' => try out.appendSlice(allocator, "&amp;"),
        '<' => try out.appendSlice(allocator, "&lt;"),
        '>' => try out.appendSlice(allocator, "&gt;"),
        else => try out.append(allocator, c),
    };
    return out.toOwnedSlice(allocator);
}

/// The codec of `t`'s archetype — `.identity` for all but `<code>`.
pub fn codecOf(t: Type) Codec {
    return archetypeOf(t).codec;
}

/// Decode a content slice for parsing/editing under `codec`. `identity` borrows
/// `content` unchanged (no allocation); `html_entities` decodes with provenance.
/// Caller owns the result and must `deinit` it (a no-op for `identity`).
pub fn decodeForParse(allocator: Allocator, content: []const u8, codec: Codec) !Decoded {
    return switch (codec) {
        .identity => .{ .text = content },
        .html_entities => try decodeEntities(allocator, content),
    };
}

/// Reassemble the content bytes to splice between the fences after editing.
///
/// For `identity`, the editor already worked on the raw content, so this is its
/// output copied. For `html_entities` it is SPAN-AWARE: the editor worked in
/// decoded space, so the unchanged head and tail keep their ORIGINAL source
/// encoding byte-for-byte (mapped back through `decoded`'s provenance), and only
/// the region the editor actually changed is canonically re-encoded — so an
/// untouched `&#60;` never normalizes to `&lt;`. `orig` is the original
/// (encoded) content slice; `decoded` its `decodeForParse` result; `edited` the
/// editor's decoded output. Caller owns the result.
pub fn reencodeEdited(allocator: Allocator, codec: Codec, orig: []const u8, decoded: Decoded, edited: []const u8) ![]u8 {
    if (codec == .identity) return allocator.dupe(u8, edited);

    // fig's editor is minimal-diff in decoded space (it rewrites only touched
    // spans), so a common-prefix/suffix scan recovers exactly the changed region.
    const before = decoded.text;
    var p: usize = 0;
    while (p < before.len and p < edited.len and before[p] == edited[p]) p += 1;
    var s: usize = 0;
    while (s < before.len - p and s < edited.len - p and
        before[before.len - 1 - s] == edited[edited.len - 1 - s]) s += 1;

    const head_src = decoded.srcAt(p); // original bytes [0, head_src) unchanged
    const tail_src = decoded.srcAt(before.len - s); // original bytes [tail_src, len) unchanged
    const enc_mid = try encodeEntities(allocator, edited[p .. edited.len - s]);
    defer allocator.free(enc_mid);

    var out: std.ArrayList(u8) = .empty;
    errdefer out.deinit(allocator);
    try out.appendSlice(allocator, orig[0..head_src]);
    try out.appendSlice(allocator, enc_mid);
    try out.appendSlice(allocator, orig[tail_src..]);
    return out.toOwnedSlice(allocator);
}

const Archetype = struct {
    open: Delimiter,
    close: Delimiter,
    location: enum { start, end, middle },
    inner: InnerFormat,
    /// How the content is encoded on disk relative to its parsed form. All
    /// archetypes but the HTML `<code>` block are `identity` (content bytes are
    /// the parse input verbatim); `<code>` is `html_entities` (see `Codec`).
    codec: Codec = .identity,
};

/// How an archetype's content bytes relate to the bytes the inner parser sees.
pub const Codec = enum {
    /// Content is the parse input verbatim — a plain slice, byte-identical splice.
    identity,
    /// Content is HTML-entity-encoded: `&lt;`→`<` on the way in, canonical
    /// re-encode on the way out. Decoding is span-aware (`Decoded` carries a
    /// provenance map) so an edit preserves the original encoding of every byte
    /// it did not touch. Only the `<code class="language-…">` archetype uses it.
    html_entities,
};

fn archetypeOf(t: Type) Archetype {
    return switch (t) {
        // --- parametric families (format carried in the union payload) ---
        .frontmatter => |f| .{
            // Bare `---` (YAML) or `---<lang>`; closes on a bare `---`/`...`.
            .open = .{ .match = .{ .frontmatter_open = f }, .literal = frontmatterLiteral(f) },
            .close = .{ .tokens = &.{ "---", "..." }, .match = .whole_line },
            .location = .start,
            .inner = f,
        },
        .fenced => |f| .{
            // ```` ```<lang> ```` … bare ```` ``` ````, a labeled code block that
            // renders on any markdown viewer instead of a bare-delimiter rule.
            .open = .{ .match = .{ .fenced_open = f }, .literal = fencedLiteral(f) },
            .close = .{ .tokens = &.{"```"}, .match = .whole_line },
            .location = .start,
            .inner = f,
        },
        .html_script => |f| .{
            // `<script type="application/<mime>">` … `</script>` HTML data
            // island — the web's typed inert "data block" (cf. JSON-LD). It sits
            // MID-document (typically in `<head>`), so it is located by scanning.
            .open = .{ .match = .{ .script_open = f }, .literal = scriptLiteral(f) },
            .close = .{ .tokens = &.{"</script>"}, .match = .line_trimmed },
            .location = .middle,
            .inner = f,
        },
        .html_code => |f| .{
            // `<pre><code class="language-<lang>">` … `</code></pre>` — a VISIBLE,
            // highlighted code block that is also the authoritative config. Its
            // content is HTML-entity-encoded, so it is the one archetype with a
            // non-identity codec (span-aware: see `Codec`/`reencodeEdited`).
            .open = .{ .match = .{ .code_open = f }, .literal = codeLiteral(f) },
            .close = .{ .tokens = &.{ "</code></pre>", "</code>" }, .match = .line_trimmed },
            .location = .middle,
            .inner = f,
            .codec = .html_entities,
        },

        // --- blessed presets (distinct fixed delimiters) ---
        .semicolons_json => .{
            .open = .{ .tokens = &.{";;;"}, .match = .whole_line },
            .close = .{ .tokens = &.{";;;"}, .match = .whole_line },
            .location = .start,
            .inner = .json,
        },
        .plus_toml => .{
            .open = .{ .tokens = &.{"+++"}, .match = .whole_line },
            .close = .{ .tokens = &.{"+++"}, .match = .whole_line },
            .location = .start,
            .inner = .toml,
        },
        .endmatter_yaml => .{
            .open = .{ .tokens = &.{"```endmatter"}, .match = .whole_line },
            .close = .{ .tokens = &.{"```"}, .match = .whole_line },
            .location = .end,
            .inner = .yaml,
        },
    };
}

/// An archetypal "config embedded in a host file" pattern. Each value fixes both
/// *where* the config lives (the host's delimiter convention) and *what* inner
/// format it is.
///
/// The three PARAMETRIC families take the format as a payload — the delimiter
/// convention is fixed but reads the format from a language tag (a fence info
/// string, a `---<lang>` tag, or a `<script type>` MIME):
///   - `frontmatter`  `---<lang>`  `---`/`...` (bare `---`  YAML). The whole
///     markdown-frontmatter ecosystem plus the self-describing `---<lang>` form.
///   - `fenced`       ```` ```<lang> ```````` ``` ```` labeled code block.
///   - `html_script`  `<script type="application/<lang>">`  `</script>`.
///
/// The remaining BLESSED presets are the popular, *nonparametric* conventions
/// whose delimiter is its own distinct token (a reader decodes it without a
/// tag): `;;;` (JSON), `+++` (TOML, Hugo/Zola), and the trailing ```` ```endmatter ````
/// block. Keeping only these named — and expressing everything else
/// parametrically — is what shrinks the surface while still making every
/// (container, format) pair reachable.
pub const Type = union(enum) {
    /// `---<lang>` … `---`/`...`; a bare `---` (no tag) is YAML.
    frontmatter: InnerFormat,
    /// ```` ```<lang> ```` … ```` ``` ````.
    fenced: InnerFormat,
    /// `<script type="application/<lang>">` … `</script>` HTML data island.
    html_script: InnerFormat,
    /// `<pre><code class="language-<lang>">` … `</code></pre>` — a visible,
    /// highlighted, entity-encoded code block that is also the source of truth.
    html_code: InnerFormat,
    /// `;;;` … `;;;` JSON frontmatter (blessed; distinct delimiter).
    semicolons_json,
    /// `+++` … `+++` TOML frontmatter — the Hugo/Zola convention (blessed).
    plus_toml,
    /// A trailing ```` ```endmatter ```` … ```` ``` ```` YAML block. For Stephen Deken.
    endmatter_yaml,
};

/// A located region, in *outer-source* byte coordinates. The fence spans are
/// retained so an editor can splice a replacement into `content` while leaving
/// everything else byte-identical. `body` is the host text OUTSIDE the region —
/// the markdown the config is embedded in — computed archetype-aware: the suffix
/// after the close fence for a `.start` (frontmatter) region, the prefix before
/// the open fence for an `.end` (endmatter) region. It is the read-side twin of
/// the `content` slice (frontmatter vs. body) and the target of `replace_body`.
pub const Region = struct {
    open_fence: Span,
    content: Span,
    close_fence: Span,
    body: Span,
};

/// Extraction result. `source` is the borrowed *outer* file; `region` indexes
/// into it. `document`'s node spans are relative to the DECODED content — call
/// `outerSpan` to lift them back into outer-file coordinates (which, for a codec
/// archetype, routes through the decode provenance map). `decoded` holds the
/// content the parser actually saw: a borrow of `source` for identity archetypes,
/// an owned decoded buffer (+ map) for the entity-encoded `<code>` one.
pub const Embedded = struct {
    source: []const u8,
    type: Type,
    region: Region,
    document: Document,
    decoded: Decoded,

    pub fn deinit(self: Embedded, allocator: Allocator) void {
        self.document.deinit(allocator);
        self.decoded.deinit(allocator);
    }

    pub fn outerSpan(self: Embedded, s: Span) Span {
        const base = self.region.content.start;
        // Map decoded coordinates back to source via the provenance map (a plain
        // identity for the borrow case, where `segs` is empty).
        return Span.init(self.decoded.srcAt(s.start) + base, self.decoded.srcAt(s.end) + base);
    }
};

/// One document within a multi-document YAML stream, located in *outer-source*
/// byte coordinates. `content` is the exact slice handed to the single-document
/// parser: it INCLUDES a leading `---` marker line (which the parser consumes)
/// but excludes a trailing `...`. `explicit` records whether the document opened
/// with a `---` marker (vs. a bare document at stream start or after `...`).
pub const StreamDoc = struct {
    content: Span,
    explicit: bool,
    document: Document,

    /// Lift a node span (relative to this document's content) into outer-file
    /// coordinates — mirrors `Embedded.outerSpan`.
    pub fn outerSpan(self: StreamDoc, s: Span) Span {
        const base = self.content.start;
        return Span.init(s.start + base, s.end + base);
    }
};

/// A parsed multi-document YAML stream. `source` is the borrowed outer file;
/// each document's `content` indexes into it. The single-document parser only
/// ever sees one document at a time, so this never trips its multi-document
/// guard — the stream concept lives here, in the splitter, not in the parser.
pub const Stream = struct {
    source: []const u8,
    documents: []const StreamDoc,

    pub fn deinit(self: Stream, allocator: Allocator) void {
        for (self.documents) |d| d.document.deinit(allocator);
        allocator.free(self.documents);
    }
};

pub const Error = error{
    /// No region of this archetype exists (plain markdown, no frontmatter).
    /// Distinct from a region that exists but is malformed.
    NotFound,
    /// An opening delimiter with no matching close.
    Unterminated,
};

/// Locate + parse the embedded document of type `t` in `source`. For a codec
/// archetype the content is decoded (with provenance) before parsing, and the
/// decoded buffer is owned by the returned `Embedded`.
pub fn extract(allocator: Allocator, source: []const u8, t: Type) !Embedded {
    const a = archetypeOf(t);
    const region = try locate(source, a);
    const decoded = try decodeForParse(allocator, Span.of(u8, region.content, source), a.codec);
    errdefer decoded.deinit(allocator);
    const document = try parseSlice(allocator, decoded.text, a.inner);
    return .{ .source = source, .type = t, .region = region, .document = document, .decoded = decoded };
}

/// Locate the region of type `t` in `source` without parsing its content.
/// Useful when a caller only needs the fence/content spans (e.g. to splice).
pub fn locateRegion(source: []const u8, t: Type) Error!Region {
    return locate(source, archetypeOf(t));
}

/// Best-effort content sniffing for which embed archetype `source` uses — the
/// `Embed` counterpart to `Language.detect`. Recognizes an OPEN delimiter (not a
/// full `locate`, which also demands a matching close — an unterminated block
/// should still be *recognized* so the caller's `extract`/`locateRegion` surfaces
/// the real `error.Unterminated` rather than a misleading "nothing found").
///
/// The `.start` conventions are checked on the very first line (after a BOM): a
/// fenced ```` ```<lang> ````, a `---<lang>` (or bare `---`) frontmatter, or a
/// `;;;`/`+++` block — their leading tokens are mutually distinct, so order among
/// them can't change the result. The scanned conventions come last because they
/// need a whole-document sweep: the trailing ```` ```endmatter ```` fence, then an
/// HTML `<script type>` data island anywhere in the page. A fenced tag that is
/// not a known config language (```` ```python ````, ```` ```endmatter ````) resolves to
/// null here, so it falls through to the next candidate rather than being taken
/// for config.
pub fn detect(source: []const u8) ?Type {
    var i: usize = 0;
    if (std.mem.startsWith(u8, source, "\xEF\xBB\xBF")) i += 3; // UTF-8 BOM

    // First-line (`.start`) conventions.
    const eol = lineEnd(source, i);
    const first = std.mem.trim(u8, std.mem.trimEnd(u8, source[i..eol], "\r\n"), " \t");
    if (parseFencedOpen(first)) |f| return .{ .fenced = f };
    if (parseFrontmatterOpen(first)) |f| return .{ .frontmatter = f };
    if (std.mem.eql(u8, first, ";;;")) return .semicolons_json;
    if (std.mem.eql(u8, first, "+++")) return .plus_toml;

    // Scanned conventions.
    if (scanForDelim(source, i, archetypeOf(.endmatter_yaml).open) != null) return .endmatter_yaml;
    var line = i;
    while (line < source.len) : (line = lineEnd(source, line)) {
        const le = lineEnd(source, line);
        const lt = std.mem.trim(u8, std.mem.trimEnd(u8, source[line..le], "\r\n"), " \t");
        if (parseScriptOpen(lt)) |f| return .{ .html_script = f };
        if (parseCodeOpen(lt)) |f| return .{ .html_code = f };
    }
    return null;
}

/// Whether `t`'s body (the host prose) sits BEFORE the open fence (endmatter)
/// rather than after the close fence (frontmatter). Lets a host-coordinate
/// editor pick the side to splice when replacing the body.
pub fn bodyIsBefore(t: Type) bool {
    return archetypeOf(t).location == .end;
}

/// The format `t`'s content is written in. Lets a caller resolve the parser/
/// printer to use for an embed's content  e.g. a `get`-style command that
/// picked an archetype via `--embed` and needs to know what format that
/// implies, rather than trusting a possibly-unrelated file-extension guess.
pub fn innerFormat(t: Type) InnerFormat {
    return archetypeOf(t).inner;
}

/// The exact text to emit for delimiter `d` when synthesizing a region  its
/// `literal` override, or `tokens[0]` when there is none. (`script_open`'s match
/// token is a MIME value, not the `<script>` tag it must emit.)
fn delimLiteral(d: Delimiter) []const u8 {
    return d.literal orelse d.tokens[0];
}

/// Built host for a source that has no region of type `t`  the create half of
/// "open or create".
pub const Initialized = struct { host: []u8, region: Region };

/// Synthesize a host containing an EMPTY region of type `t` around `source`,
/// which is assumed to have none. The fresh block is placed where the archetype
/// dictates  prepended for a `.start` (frontmatter) region, appended for an
/// `.end` (endmatter) region  with the original `source` becoming the region's
/// body. Its content is seeded with an empty inner document (nothing for YAML, an
/// empty object for JSON) so a subsequent insert/set lands the first key. No
/// blank line is inserted between fence and body, so the output matches the
/// hand-rolled `---\n…\n---\n{body}` shape. The returned `host` is caller-owned.
///
pub fn initRegion(allocator: Allocator, source: []const u8, t: Type) !Initialized {
    const a = archetypeOf(t);
    const open_tok = delimLiteral(a.open);
    const close_tok = delimLiteral(a.close);
    // The empty inner document seeded between the fences. JSON gets a trailing
    // newline so the close fence stays on its own line after the flow-mapping
    // insert (which preserves it); YAML's, fig's, and TOML's block inserts emit
    // their own newline, so their empty content needs none. Those three all seed
    // empty because an empty document is a valid empty map / empty root table
    // (see `fig/parser.zig`'s `buildRoot`), so a subsequent set/insert lands the
    // first key into it.
    const seed: []const u8 = switch (a.inner) {
        .yaml, .fig, .toml => "",
        .json => "{}\n",
    };

    var host: std.ArrayList(u8) = .empty;
    errdefer host.deinit(allocator);

    if (a.location == .end) {
        // [ body ][ \n? ][ open\n ][ seed ][ close\n ]
        try host.appendSlice(allocator, source);
        // The open fence must start its own line; add a separating newline when
        // the body doesn't already end in one.
        if (source.len > 0 and source[source.len - 1] != '\n') try host.append(allocator, '\n');
        const open_start = host.items.len;
        try host.appendSlice(allocator, open_tok);
        try host.append(allocator, '\n');
        const content_start = host.items.len;
        try host.appendSlice(allocator, seed);
        const content_end = host.items.len;
        try host.appendSlice(allocator, close_tok);
        try host.append(allocator, '\n');
        const close_end = host.items.len;
        return .{ .host = try host.toOwnedSlice(allocator), .region = .{
            .open_fence = Span.init(open_start, content_start),
            .content = Span.init(content_start, content_end),
            .close_fence = Span.init(content_end, close_end),
            .body = Span.init(0, source.len),
        } };
    }
    // .start: [ open\n ][ seed ][ close\n ][ body ]
    try host.appendSlice(allocator, open_tok);
    try host.append(allocator, '\n');
    const content_start = host.items.len;
    try host.appendSlice(allocator, seed);
    const content_end = host.items.len;
    try host.appendSlice(allocator, close_tok);
    try host.append(allocator, '\n');
    const close_end = host.items.len;
    const body_start = host.items.len;
    try host.appendSlice(allocator, source);
    return .{ .host = try host.toOwnedSlice(allocator), .region = .{
        .open_fence = Span.init(0, content_start),
        .content = Span.init(content_start, content_end),
        .close_fence = Span.init(content_end, close_end),
        .body = Span.init(body_start, body_start + source.len),
    } };
}

/// Rebuild `source`'s embedded region as a DIFFERENT archetype: keep the host
/// prose (`region.body`, byte-identical) but replace the old fences and
/// content with `to`'s convention wrapped around `new_content`  the already
/// re-serialized inner document (e.g. YAML frontmatter content re-printed as
/// JSON, for `fig convert --to-embed`). `region` must be `source`'s existing
/// region of whatever archetype it was located as (`locateRegion`/`extract`);
/// this function doesn't care what that was, only where the body is. Mirrors
/// `initRegion`'s `.start`/`.end` placement, but re-housing real content
/// rather than seeding an empty document. The returned buffer is caller-owned.
pub fn retype(allocator: Allocator, source: []const u8, region: Region, to: Type, new_content: []const u8) ![]u8 {
    const a = archetypeOf(to);
    const open_tok = delimLiteral(a.open);
    const close_tok = delimLiteral(a.close);
    const body = Span.of(u8, region.body, source);

    var host: std.ArrayList(u8) = .empty;
    errdefer host.deinit(allocator);

    if (a.location == .end) {
        // [ body ][ \n? ][ open\n ][ content ][ close\n ]
        try host.appendSlice(allocator, body);
        if (body.len > 0 and body[body.len - 1] != '\n') try host.append(allocator, '\n');
        try host.appendSlice(allocator, open_tok);
        try host.append(allocator, '\n');
        try host.appendSlice(allocator, new_content);
        try host.appendSlice(allocator, close_tok);
        try host.append(allocator, '\n');
        return host.toOwnedSlice(allocator);
    }
    // .start: [ open\n ][ content ][ close\n ][ body ]
    try host.appendSlice(allocator, open_tok);
    try host.append(allocator, '\n');
    try host.appendSlice(allocator, new_content);
    try host.appendSlice(allocator, close_tok);
    try host.append(allocator, '\n');
    try host.appendSlice(allocator, body);
    return host.toOwnedSlice(allocator);
}

/// Parse an explicit content span as `t`'s inner format, no host scanning. Note
/// this parses the span VERBATIM  for a codec archetype the caller must decode
/// first (`extract` does); `parseSlice` is the shared per-format parse.
pub fn parseSpan(allocator: Allocator, source: []const u8, content: Span, t: Type) !Document {
    return parseSlice(allocator, Span.of(u8, content, source), archetypeOf(t).inner);
}

/// Parse a raw content slice as `inner`.
fn parseSlice(allocator: Allocator, slice: []const u8, inner: InnerFormat) !Document {
    return switch (inner) {
        .yaml => if (comptime build_options.lang_yaml) blk: {
            var parser = Language.YAML.Parser{ .allocator = allocator };
            break :blk Language.YAML.parse(&parser, slice, Language.YAML.default_type);
        } else error.FormatDisabled,
        .json => if (comptime build_options.lang_json) blk: {
            var parser = Language.JSON.Parser{ .allocator = allocator };
            break :blk Language.JSON.parse(&parser, slice, Language.JSON.default_type);
        } else error.FormatDisabled,
        .fig => if (comptime build_options.lang_fig) blk: {
            var parser = Language.FIG.Parser{ .allocator = allocator };
            break :blk Language.FIG.parse(&parser, slice, Language.FIG.default_type);
        } else error.FormatDisabled,
        .toml => if (comptime build_options.lang_toml) blk: {
            var parser = Language.TOML.Parser{ .allocator = allocator };
            break :blk Language.TOML.parse(&parser, slice, Language.TOML.default_type);
        } else error.FormatDisabled,
    };
}

// --- multi-document YAML stream splitter ---------------------------------

/// Split a YAML stream into its constituent documents and parse each one
/// independently with the single-document parser.
///
/// A stream is a sequence of documents delimited by `---` (start) and `...`
/// (end) markers at column 0. Rather than teach the core parser to span
/// documents  which would complicate in-place editing and round-tripping  we
/// locate each document's byte range here (the same "locate region, then parse
/// it" shape as `extract`) and hand the slices to the parser one at a time.
///
/// Always returns at least one document: an empty or comment-only stream yields
/// a single `null` document. A parse error in any document propagates (and any
/// already-parsed documents are freed).
pub fn extractStream(allocator: Allocator, source: []const u8) !Stream {
    var docs: std.ArrayList(StreamDoc) = .empty;
    errdefer {
        for (docs.items) |d| d.document.deinit(allocator);
        docs.deinit(allocator);
    }

    var start: usize = 0;
    if (std.mem.startsWith(u8, source, "\xEF\xBB\xBF")) start += 3; // UTF-8 BOM

    var seg_start: usize = start;
    var seg_explicit = false;

    var line = start;
    while (line < source.len) {
        const next = lineEnd(source, line);
        switch (markerKind(source, line)) {
            .start => {
                // A `---` ends the current segment and opens a new explicit
                // document *at* this line  the marker stays in the slice so
                // inline content (`--- foo`) is preserved and the parser eats
                // the marker token.
                //
                // Directives (`%YAML`/`%TAG`) preceding the `---` belong to the
                // document it introduces. If the pending segment is just those
                // directives (and trivia), don't close it here  let the explicit
                // document include them, so the parser sees `%YAML\n---\n` as one
                // document and validates the directive against its marker.
                if (!segmentIsDirectives(source[seg_start..line])) {
                    try pushSegment(allocator, source, &docs, seg_start, line, seg_explicit);
                    seg_start = line;
                }
                seg_explicit = true;
            },
            .end => {
                // A `...` ends the current document; the marker itself is left
                // out of the slice. What follows is a fresh bare document.
                try pushSegment(allocator, source, &docs, seg_start, line, seg_explicit);
                seg_start = next;
                seg_explicit = false;
            },
            .none => {},
        }
        line = next;
    }
    try pushSegment(allocator, source, &docs, seg_start, source.len, seg_explicit);

    if (docs.items.len == 0) {
        // An empty / trivia-only stream is one null document.
        const doc = try parseYamlSlice(allocator, source[start..]);
        try docs.append(allocator, .{ .content = Span.init(start, source.len), .explicit = false, .document = doc });
    }

    return .{ .source = source, .documents = try docs.toOwnedSlice(allocator) };
}

/// Parse `source[seg_start..seg_end]` and append it as a document, unless it is
/// a bare segment with no real content (blank/comment-only)  such a segment is
/// not a document (e.g. a leading comment before the first `---`). An explicit
/// segment (opened by `---`) is always a document, even when its body is empty.
fn pushSegment(
    allocator: Allocator,
    source: []const u8,
    docs: *std.ArrayList(StreamDoc),
    seg_start: usize,
    seg_end: usize,
    explicit: bool,
) !void {
    const slice = source[seg_start..seg_end];
    if (!explicit and !hasContent(slice)) return;
    const doc = try parseYamlSlice(allocator, slice);
    try docs.append(allocator, .{
        .content = Span.init(seg_start, seg_end),
        .explicit = explicit,
        .document = doc,
    });
}

fn parseYamlSlice(allocator: Allocator, slice: []const u8) !Document {
    if (comptime build_options.lang_yaml) {
        var parser = Language.YAML.Parser{ .allocator = allocator };
        return Language.YAML.parse(&parser, slice, Language.YAML.default_type);
    } else return error.FormatDisabled;
}

const MarkerKind = enum { start, end, none };

/// Classify the column-0 line at `at` as a document `---`/`...` marker. A `---`
/// marker is the bare token or a `--- `/`---\t` prefix (inline content allowed);
/// `---foo` is a plain scalar, not a marker. A `...` marker must occupy the
/// whole line. Indented lines never match (callers only pass line starts).
fn markerKind(source: []const u8, at: usize) MarkerKind {
    const eol = lineEnd(source, at);
    const line = std.mem.trimEnd(u8, source[at..eol], "\r\n");
    if (std.mem.eql(u8, line, "---")) return .start;
    if (std.mem.startsWith(u8, line, "--- ") or std.mem.startsWith(u8, line, "---\t")) return .start;
    if (std.mem.eql(u8, std.mem.trimEnd(u8, line, " \t"), "...")) return .end;
    return .none;
}

/// True if every content line of `slice` (ignoring blanks and comments) is a
/// column-0 directive (`%`), and there is at least one. Such a pre-`---`
/// segment is a directives prefix that belongs to the following document, not a
/// document of its own.
fn segmentIsDirectives(slice: []const u8) bool {
    var any = false;
    var i: usize = 0;
    while (i < slice.len) {
        const eol = lineEnd(slice, i);
        const line = std.mem.trimEnd(u8, slice[i..eol], "\r\n");
        i = eol;
        if (line.len == 0) continue;
        if (line[0] == '%') {
            any = true; // a directive sits at column 0
            continue;
        }
        const body = std.mem.trim(u8, line, " \t");
        if (body.len == 0 or body[0] == '#') continue; // blank or comment
        return false; // some other content  not a pure directives prefix
    }
    return any;
}

/// True if `slice` has any line that is neither blank nor a comment.
fn hasContent(slice: []const u8) bool {
    var i: usize = 0;
    while (i < slice.len) {
        const eol = lineEnd(slice, i);
        const trimmed = std.mem.trim(u8, slice[i..eol], " \t\r\n");
        if (trimmed.len != 0 and trimmed[0] != '#') return true;
        i = eol;
    }
    return false;
}

// --- markdown frontmatter locator ---------------------------------------

fn locate(source: []const u8, a: Archetype) Error!Region {
    var i: usize = 0;
    if (std.mem.startsWith(u8, source, "\xEF\xBB\xBF")) i += 3; // UTF-8 BOM

    const open = if (a.location == .start)
        matchDelim(source, i, a.open) orelse return Error.NotFound
    else
        scanForDelim(source, i, a.open) orelse return Error.NotFound;

    var line = open.end;
    while (line < source.len) {
        if (matchDelim(source, line, a.close)) |close| {
            // The body is the host text outside the fences: the prefix before the
            // open fence for endmatter (where the config trails the prose), else
            // the suffix after the close fence  for frontmatter (`.start`) AND
            // for a mid-document block (`.middle`, e.g. an HTML `<script>` data
            // island). A `.middle` block also has host text BEFORE it, which this
            // single-span body doesn't capture; in-place edits splice via the
            // content spans and stay byte-identical on both sides regardless, so
            // only `replace_body` is one-sided (it swaps the suffix).
            const body = if (a.location == .end)
                Span.init(0, open.start)
            else
                Span.init(close.end, source.len);
            return .{ .open_fence = open, .content = Span.init(open.end, close.start), .close_fence = close, .body = body };
        }
        line = lineEnd(source, line);
    }
    return Error.Unterminated;
}

fn lineEnd(source: []const u8, from: usize) usize {
    return if (std.mem.findScalarPos(u8, source, from, '\n')) |nl| nl + 1 else source.len;
}

/// One line vs a Delimiter; returns the line's span (incl. newline) or null.
fn matchDelim(source: []const u8, start: usize, d: Delimiter) ?Span {
    const eol = lineEnd(source, start);
    const line = std.mem.trimEnd(u8, source[start..eol], "\r\n");
    const matched = switch (d.match) {
        .whole_line => blk: {
            const trimmed = std.mem.trimEnd(u8, line, " \t");
            for (d.tokens) |tok| if (std.mem.eql(u8, trimmed, tok)) break :blk true;
            break :blk false;
        },
        .prefix => blk: {
            for (d.tokens) |tok| if (std.mem.startsWith(u8, line, tok)) break :blk true;
            break :blk false;
        },
        .line_trimmed => blk: {
            const trimmed = std.mem.trim(u8, line, " \t");
            for (d.tokens) |tok| if (std.mem.eql(u8, trimmed, tok)) break :blk true;
            break :blk false;
        },
        // Parametric opens: parse the line's tag and require it to resolve to the
        // exact format this delimiter expects.
        .fenced_open => |f| parseFencedOpen(std.mem.trim(u8, line, " \t")) == f,
        .frontmatter_open => |f| parseFrontmatterOpen(std.mem.trim(u8, line, " \t")) == f,
        .script_open => |f| parseScriptOpen(std.mem.trim(u8, line, " \t")) == f,
        .code_open => |f| parseCodeOpen(std.mem.trim(u8, line, " \t")) == f,
    };
    return if (matched) Span.init(start, eol) else null;
}

fn isHspace(c: u8) bool {
    return c == ' ' or c == '\t';
}

/// The value of attribute `name` in an HTML open tag's attribute region (the tag
/// name and the closing `>` stripped), or null if absent. Tolerant of attribute
/// order, extra attributes, single/double/unquoted values, and whitespace around
/// `=`; the attribute name compares case-insensitively (HTML folds them). The
/// value is returned verbatim for the caller to resolve.
fn scriptAttrValue(attrs: []const u8, name: []const u8) ?[]const u8 {
    var i: usize = 0;
    while (i < attrs.len) {
        while (i < attrs.len and isHspace(attrs[i])) i += 1;
        if (i >= attrs.len) break;
        const name_start = i;
        while (i < attrs.len and attrs[i] != '=' and !isHspace(attrs[i])) i += 1;
        const attr_name = attrs[name_start..i];
        while (i < attrs.len and isHspace(attrs[i])) i += 1;
        var value: []const u8 = "";
        if (i < attrs.len and attrs[i] == '=') {
            i += 1;
            while (i < attrs.len and isHspace(attrs[i])) i += 1;
            if (i < attrs.len and (attrs[i] == '"' or attrs[i] == '\'')) {
                const q = attrs[i];
                i += 1;
                const v_start = i;
                while (i < attrs.len and attrs[i] != q) i += 1;
                value = attrs[v_start..i];
                if (i < attrs.len) i += 1; // consume closing quote
            } else {
                const v_start = i;
                while (i < attrs.len and !isHspace(attrs[i])) i += 1;
                value = attrs[v_start..i];
            }
        }
        if (std.ascii.eqlIgnoreCase(attr_name, name)) return value;
    }
    return null;
}

/// Scan forward line-by-line for the first line matching `d`; null at EOF.
fn scanForDelim(source: []const u8, start: usize, d: Delimiter) ?Span {
    var line = start;
    while (line < source.len) {
        if (matchDelim(source, line, d)) |span| return span;
        line = lineEnd(source, line);
    }
    return null;
}
// --- tests ---------------------------------------------------------------

const testing = std.testing;
const AST = @import("ast/ast.zig");

fn rootKind(doc: Document) AST.Node.Kind {
    return doc.ast.nodes[doc.ast.root].kind;
}

test "extract: YAML frontmatter comments survive into the parsed AST" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    // The embed layer slices the source and hands it to the real YAML parser
    // (no AST rebuild), so captured comments must ride through on `node_comments`.
    const src =
        \\---
        \\# the title
        \\title: hi # inline
        \\---
        \\# body
        \\
    ;
    const embedded = try extract(testing.allocator, src, .{ .frontmatter = .yaml });
    defer embedded.deinit(testing.allocator);
    const ast = embedded.document.ast;
    try testing.expect(ast.node_comments.len > 0);
    const kv = ast.nodes[ast.nodes[ast.root].kind.mapping.?].kind.keyvalue;
    try testing.expectEqualStrings("the title", ast.comments(kv.key).leading[0].text);
    try testing.expectEqualStrings("inline", ast.comments(kv.value).trailing.?.text);
}

test "extract: fig frontmatter (```fig fenced block) locates and parses" {
    if (comptime !build_options.lang_fig) return error.SkipZigTest;
    const src =
        \\```fig
        \\# the title
        \\title = hi # inline
        \\tags = [a, b]
        \\```
        \\# body
        \\
    ;
    const embedded = try extract(testing.allocator, src, .{ .fenced = .fig });
    defer embedded.deinit(testing.allocator);
    // Fences excluded from content; body is everything after the close fence.
    try testing.expectEqualStrings("```fig\n", src[embedded.region.open_fence.start..embedded.region.open_fence.end]);
    try testing.expectEqualStrings("```\n", src[embedded.region.close_fence.start..embedded.region.close_fence.end]);
    try testing.expectEqualStrings("# body\n", src[embedded.region.body.start..embedded.region.body.end]);

    const ast = embedded.document.ast;
    try testing.expect(ast.node_comments.len > 0);
    const kv = ast.nodes[ast.nodes[ast.root].kind.mapping.?].kind.keyvalue;
    try testing.expectEqualStrings("the title", ast.comments(kv.key).leading[0].text);
    try testing.expectEqualStrings("inline", ast.comments(kv.value).trailing.?.text);
    try testing.expectEqualSlices(u8, "hi", (try embedded.document.ast.getValByPath(&.{.{ .key = "title" }})).kind.string);
}

test "extract: a generic ```something fence is not mistaken for ```fig" {
    if (comptime !build_options.lang_fig) return error.SkipZigTest;
    // The open token match is whole-line-exact (`.whole_line`), not a prefix
    // match, so a same-family fenced code block with a different/longer info
    // string (an ordinary markdown code fence, not this archetype) must not
    // be located as fig frontmatter.
    const src =
        \\```figure
        \\not fig frontmatter
        \\```
        \\
    ;
    try testing.expectError(Error.NotFound, locateRegion(src, .{ .fenced = .fig }));
}

test "innerFormat reports each archetype's content format" {
    try testing.expectEqual(InnerFormat.yaml, innerFormat(.{ .frontmatter = .yaml }));
    try testing.expectEqual(InnerFormat.yaml, innerFormat(.endmatter_yaml));
    try testing.expectEqual(InnerFormat.json, innerFormat(.semicolons_json));
    try testing.expectEqual(InnerFormat.fig, innerFormat(.{ .fenced = .fig }));
    try testing.expectEqual(InnerFormat.toml, innerFormat(.plus_toml));
    try testing.expectEqual(InnerFormat.yaml, innerFormat(.{ .fenced = .yaml }));
    try testing.expectEqual(InnerFormat.json, innerFormat(.{ .fenced = .json }));
    try testing.expectEqual(InnerFormat.toml, innerFormat(.{ .fenced = .toml }));
}

test "extract: TOML frontmatter (+++ fences) locates and parses" {
    if (comptime !build_options.lang_toml) return error.SkipZigTest;
    const src =
        \\+++
        \\title = "hi"
        \\tags = ["a", "b"]
        \\+++
        \\# body
        \\
    ;
    const embedded = try extract(testing.allocator, src, .plus_toml);
    defer embedded.deinit(testing.allocator);
    try testing.expectEqualStrings("+++\n", src[embedded.region.open_fence.start..embedded.region.open_fence.end]);
    try testing.expectEqualStrings("+++\n", src[embedded.region.close_fence.start..embedded.region.close_fence.end]);
    try testing.expectEqualStrings("# body\n", src[embedded.region.body.start..embedded.region.body.end]);
    try testing.expectEqualSlices(u8, "hi", (try embedded.document.ast.getValByPath(&.{.{ .key = "title" }})).kind.string);
}

test "extract: fenced ```toml / ```yaml / ```json frontmatter locate and parse" {
    if (comptime !build_options.lang_toml or !build_options.lang_yaml or !build_options.lang_json)
        return error.SkipZigTest;

    const toml_src =
        \\```toml
        \\title = "hi"
        \\```
        \\body
        \\
    ;
    const t = try extract(testing.allocator, toml_src, .{ .fenced = .toml });
    defer t.deinit(testing.allocator);
    try testing.expectEqualStrings("```toml\n", toml_src[t.region.open_fence.start..t.region.open_fence.end]);
    try testing.expectEqualSlices(u8, "hi", (try t.document.ast.getValByPath(&.{.{ .key = "title" }})).kind.string);

    const yaml_src =
        \\```yaml
        \\title: hi
        \\```
        \\body
        \\
    ;
    const y = try extract(testing.allocator, yaml_src, .{ .fenced = .yaml });
    defer y.deinit(testing.allocator);
    try testing.expectEqualSlices(u8, "hi", (try y.document.ast.getValByPath(&.{.{ .key = "title" }})).kind.string);

    const json_src =
        \\```json
        \\{"title": "hi"}
        \\```
        \\body
        \\
    ;
    const j = try extract(testing.allocator, json_src, .{ .fenced = .json });
    defer j.deinit(testing.allocator);
    try testing.expectEqualSlices(u8, "hi", (try j.document.ast.getValByPath(&.{.{ .key = "title" }})).kind.string);
}

test "extract: HTML <script type=\"application/figl\"> data island locates and parses" {
    if (comptime !build_options.lang_fig) return error.SkipZigTest;
    // A realistic page: doctype + head, the island indented inside <head>, prose
    // both BEFORE and AFTER the block (the mid-document case).
    const src =
        \\<!doctype html>
        \\<html>
        \\  <head>
        \\    <script type="application/figl">
        \\title = hi
        \\tags = [a, b]
        \\    </script>
        \\  </head>
        \\  <body>page</body>
        \\</html>
        \\
    ;
    const embedded = try extract(testing.allocator, src, .{ .html_script = .fig });
    defer embedded.deinit(testing.allocator);
    // The open/close fence spans cover their whole (indented) lines; content is
    // exactly what sits between them, spliced back byte-for-byte on edit.
    try testing.expectEqualStrings("    <script type=\"application/figl\">\n", src[embedded.region.open_fence.start..embedded.region.open_fence.end]);
    try testing.expectEqualStrings("    </script>\n", src[embedded.region.close_fence.start..embedded.region.close_fence.end]);
    try testing.expectEqualStrings("title = hi\ntags = [a, b]\n", src[embedded.region.content.start..embedded.region.content.end]);
    try testing.expectEqualSlices(u8, "hi", (try embedded.document.ast.getValByPath(&.{.{ .key = "title" }})).kind.string);
}

test "parseScriptOpen: tolerates quoting/order/extra attrs, resolves the format, rejects near-misses" {
    // Accepted variants → the resolved format (figl ⇒ .fig).
    try testing.expectEqual(InnerFormat.fig, parseScriptOpen("<script type=\"application/figl\">"));
    try testing.expectEqual(InnerFormat.fig, parseScriptOpen("<script type='application/figl'>"));
    try testing.expectEqual(InnerFormat.fig, parseScriptOpen("<script id=\"cfg\" type=\"application/figl\">"));
    try testing.expectEqual(InnerFormat.fig, parseScriptOpen("<script type = \"application/figl\" defer>"));
    try testing.expectEqual(InnerFormat.fig, parseScriptOpen("<SCRIPT TYPE=\"application/figl\">")); // HTML folds case
    // Other config MIMEs resolve to their formats.
    try testing.expectEqual(InnerFormat.toml, parseScriptOpen("<script type=\"application/toml\">"));
    try testing.expectEqual(InnerFormat.json, parseScriptOpen("<script type=\"application/ld+json\">"));
    // Rejected (null): wrong element, unknown/absent type, a same-line block.
    try testing.expectEqual(@as(?InnerFormat, null), parseScriptOpen("<scripts type=\"application/figl\">"));
    try testing.expectEqual(@as(?InnerFormat, null), parseScriptOpen("<script type=\"text/javascript\">"));
    try testing.expectEqual(@as(?InnerFormat, null), parseScriptOpen("<script>"));
    try testing.expectEqual(@as(?InnerFormat, null), parseScriptOpen("<script type=\"application/figl\">k = 1</script>"));
}

test "parseCodeOpen: matches language-/lang- tokens and the <pre> wrapper; rejects the rest" {
    try testing.expectEqual(InnerFormat.fig, parseCodeOpen("<code class=\"language-figl\">"));
    try testing.expectEqual(InnerFormat.fig, parseCodeOpen("<pre><code class=\"language-figl\">")); // pre wrapper
    try testing.expectEqual(InnerFormat.fig, parseCodeOpen("<code class=\"lang-figl\">")); // lang- prefix
    try testing.expectEqual(InnerFormat.fig, parseCodeOpen("<code class=\"hljs language-figl\">")); // token in a list
    try testing.expectEqual(InnerFormat.toml, parseCodeOpen("<pre><code class='language-toml'>"));
    // Rejected: no class, a non-config language, a same-line block.
    try testing.expectEqual(@as(?InnerFormat, null), parseCodeOpen("<code>"));
    try testing.expectEqual(@as(?InnerFormat, null), parseCodeOpen("<code class=\"language-python\">"));
    try testing.expectEqual(@as(?InnerFormat, null), parseCodeOpen("<code class=\"language-figl\">k = 1</code>"));
}

test "html entity codec: decode/encode round-trip and provenance" {
    // Decode maps entities back to their chars and records source provenance.
    const dec = try decodeEntities(testing.allocator, "a &lt; b &amp; c &#61; d");
    defer dec.deinit(testing.allocator);
    try testing.expectEqualStrings("a < b & c = d", dec.text);
    // A decoded offset lifts back to the right source offset through the map:
    // the '<' (decoded index 2) came from `&lt;` at source index 2.
    try testing.expectEqual(@as(usize, 2), dec.srcAt(2));
    // Encode only touches the three significant characters.
    const enc = try encodeEntities(testing.allocator, "x < y & z > w");
    defer testing.allocator.free(enc);
    try testing.expectEqualStrings("x &lt; y &amp; z &gt; w", enc);
}

test "reencodeEdited: span-aware — untouched encoding is byte-preserved, only the edit re-encodes" {
    // Original content mixes encodings: `&#60;` (numeric) and a would-be `&lt;`.
    const orig = "title = \"a &#60; b\"\nkeep = \"x &lt; y\"\n";
    const dec = try decodeEntities(testing.allocator, orig);
    defer dec.deinit(testing.allocator);
    try testing.expectEqualStrings("title = \"a < b\"\nkeep = \"x < y\"\n", dec.text);
    // Simulate an editor that changed ONLY the `title` line's value to `p > q`.
    const edited = "title = \"p > q\"\nkeep = \"x < y\"\n";
    const out = try reencodeEdited(testing.allocator, .html_entities, orig, dec, edited);
    defer testing.allocator.free(out);
    // The edited value is canonically encoded (`>`→`&gt;`); the untouched `keep`
    // line keeps its ORIGINAL `&lt;` byte-for-byte (not normalized).
    try testing.expectEqualStrings("title = \"p &gt; q\"\nkeep = \"x &lt; y\"\n", out);
}

test "extract: <code> block decodes entities, parses, and lifts spans to source" {
    if (comptime !build_options.lang_fig) return error.SkipZigTest;
    const src =
        \\<p>see the config:</p>
        \\<pre><code class="language-figl">
        \\title = hi
        \\expr = "a &lt; b"
        \\</code></pre>
        \\<p>after</p>
        \\
    ;
    const embedded = try extract(testing.allocator, src, .{ .html_code = .fig });
    defer embedded.deinit(testing.allocator);
    // The value round-trips through entity decoding.
    try testing.expectEqualStrings("a < b", (try embedded.document.ast.getValByPath(&.{.{ .key = "expr" }})).kind.string);
    try testing.expectEqualStrings("hi", (try embedded.document.ast.getValByPath(&.{.{ .key = "title" }})).kind.string);
}

test "detect: recognizes a <code class=\"language-…\"> block" {
    try testing.expectEqual(@as(?Type, .{ .html_code = .fig }), detect("<pre><code class=\"language-figl\">\nk = v\n</code></pre>\n"));
    try testing.expectEqual(@as(?Type, .{ .html_code = .toml }), detect("<article>\n<code class=\"language-toml\">\nk = 1\n</code>\n</article>\n"));
}

test "initRegion: an HTML-script archetype seeds an empty <script> island" {
    if (comptime !build_options.lang_fig) return error.SkipZigTest;
    const init = try initRegion(testing.allocator, "<html></html>\n", .{ .html_script = .fig });
    defer testing.allocator.free(init.host);
    try testing.expectEqualStrings("<script type=\"application/figl\">\n</script>\n<html></html>\n", init.host);
    try testing.expectEqual(init.region.content.start, init.region.content.end);
}

test "detect: recognizes an HTML <script> data island (scanned mid-document)" {
    if (comptime !build_options.lang_fig) return error.SkipZigTest;
    const src = "<html><head>\n<script type=\"application/figl\">\nk = v\n</script>\n</head></html>\n";
    try testing.expectEqual(@as(?Type, .{ .html_script = .fig }), detect(src));
}

test "detect: recognizes TOML and fenced-label frontmatter" {
    // Content-only sniff (no parser needed): the open delimiters are distinct.
    try testing.expectEqual(@as(?Type, .plus_toml), detect("+++\ntitle = \"hi\"\n+++\nbody\n"));
    try testing.expectEqual(@as(?Type, .{ .fenced = .toml }), detect("```toml\ntitle = \"hi\"\n```\nbody\n"));
    try testing.expectEqual(@as(?Type, .{ .fenced = .yaml }), detect("```yaml\ntitle: hi\n```\nbody\n"));
    try testing.expectEqual(@as(?Type, .{ .fenced = .json }), detect("```json\n{\"t\":1}\n```\nbody\n"));
    // A ```fig fence still wins its own detection, not the new fenced labels.
    try testing.expectEqual(@as(?Type, .{ .fenced = .fig }), detect("```fig\nt = 1\n```\nbody\n"));
}

test "initRegion: a TOML-inner archetype seeds an empty +++ block" {
    const init = try initRegion(testing.allocator, "body text\n", .plus_toml);
    defer testing.allocator.free(init.host);
    try testing.expectEqualStrings("+++\n+++\nbody text\n", init.host);
    try testing.expectEqual(init.region.content.start, init.region.content.end);
    try testing.expectEqualStrings("body text\n", init.host[init.region.body.start..init.region.body.end]);
}

test "initRegion: a fig-inner archetype seeds an empty block from nothing" {
    // An empty fig document is a valid empty map (see `fig/parser.zig`), so a
    // brand-new ```fig``` frontmatter block seeds empty (like YAML) and a
    // subsequent set/insert lands its first key.
    const init = try initRegion(testing.allocator, "body text\n", .{ .fenced = .fig });
    defer testing.allocator.free(init.host);
    try testing.expectEqualStrings("```fig\n```\nbody text\n", init.host);
    // The seeded content is an empty span between the fences.
    try testing.expectEqual(init.region.content.start, init.region.content.end);
    try testing.expectEqualStrings("body text\n", init.host[init.region.body.start..init.region.body.end]);
}

test "extract: fig frontmatter with no host body" {
    if (comptime !build_options.lang_fig) return error.SkipZigTest;
    const src =
        \\```fig
        \\title = hi
        \\```
        \\
    ;
    const embedded = try extract(testing.allocator, src, .{ .fenced = .fig });
    defer embedded.deinit(testing.allocator);
    try testing.expectEqualStrings("", src[embedded.region.body.start..embedded.region.body.end]);
}

test "extractStream: two explicit documents in a stream (JHB9)" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const src =
        \\# Ranking of 1998 home runs
        \\---
        \\- Mark McGwire
        \\- Sammy Sosa
        \\
        \\# Team ranking
        \\---
        \\- Chicago Cubs
        \\- St Louis Cardinals
        \\
    ;
    const stream = try extractStream(testing.allocator, src);
    defer stream.deinit(testing.allocator);

    // The leading comment-only segment is not a document.
    try testing.expectEqual(@as(usize, 2), stream.documents.len);
    try testing.expect(stream.documents[0].explicit);
    try testing.expect(stream.documents[1].explicit);
    try testing.expectEqualSlices(u8, "Mark McGwire", (try stream.documents[0].document.ast.getValByPath(&.{.{ .index = 0 }})).kind.string);
    try testing.expectEqualSlices(u8, "St Louis Cardinals", (try stream.documents[1].document.ast.getValByPath(&.{.{ .index = 1 }})).kind.string);
}

test "extractStream: directives fold into the following document (6ZKB-shaped)" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    // Each `%…` prefix belongs to the `---` it introduces, not a document of its
    // own: `Document` is doc 1, the empty `---` is doc 2, and `%YAML 1.2\n---\n…`
    // is doc 3.
    const src =
        \\Document
        \\---
        \\# Empty
        \\...
        \\%YAML 1.2
        \\---
        \\matches %: 20
        \\
    ;
    const stream = try extractStream(testing.allocator, src);
    defer stream.deinit(testing.allocator);
    try testing.expectEqual(@as(usize, 3), stream.documents.len);
    try testing.expectEqualSlices(u8, "Document", rootKind(stream.documents[0].document).string);
    try testing.expect(rootKind(stream.documents[1].document) == .null_);
    try testing.expectEqualSlices(u8, "20", (try stream.documents[2].document.ast.getValByPath(&.{.{ .key = "matches %" }})).kind.number.raw);
}

test "extractStream: a tag handle scoped to the first document fails later use (QLJ7)" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    // `!prefix!` is declared only for the first document; documents 2 and 3 use
    // it undeclared, so the splitter must reject the stream.
    const src =
        \\%TAG !prefix! tag:example.com,2011:
        \\--- !prefix!A
        \\a: b
        \\--- !prefix!B
        \\c: d
        \\
    ;
    try testing.expectError(error.UndefinedTagHandle, extractStream(testing.allocator, src));
}

test "extractStream: two document start markers yields two null docs (6XDY)" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const stream = try extractStream(testing.allocator, "---\n---\n");
    defer stream.deinit(testing.allocator);
    try testing.expectEqual(@as(usize, 2), stream.documents.len);
    try testing.expect(rootKind(stream.documents[0].document) == .null_);
    try testing.expect(rootKind(stream.documents[1].document) == .null_);
}

test "extractStream: document start on last line (PUW8)" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const stream = try extractStream(testing.allocator, "---\na: b\n---\n");
    defer stream.deinit(testing.allocator);
    try testing.expectEqual(@as(usize, 2), stream.documents.len);
    try testing.expectEqualSlices(u8, "b", (try stream.documents[0].document.ast.getValByPath(&.{.{ .key = "a" }})).kind.string);
    try testing.expect(rootKind(stream.documents[1].document) == .null_);
}

test "extractStream: bare docs separated by ... with a comment-only segment (M7A3)" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const src = "Bare\ndocument\n...\n# No document\n...\n|\n  %!PS-Adobe-2.0 # Not the first line\n";
    const stream = try extractStream(testing.allocator, src);
    defer stream.deinit(testing.allocator);
    // The `# No document` segment is comment-only and produces no document.
    try testing.expectEqual(@as(usize, 2), stream.documents.len);
    try testing.expect(!stream.documents[0].explicit);
    try testing.expectEqualSlices(u8, "Bare document", rootKind(stream.documents[0].document).string);
}

test "extractStream: inline content on the marker line (L383)" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const stream = try extractStream(testing.allocator, "--- foo  # comment\n--- foo  # comment\n");
    defer stream.deinit(testing.allocator);
    try testing.expectEqual(@as(usize, 2), stream.documents.len);
    try testing.expectEqualSlices(u8, "foo", rootKind(stream.documents[0].document).string);
    try testing.expectEqualSlices(u8, "foo", rootKind(stream.documents[1].document).string);
}

test "extractStream: explicit doc then bare doc after ... (7Z25)" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const stream = try extractStream(testing.allocator, "---\nscalar1\n...\nkey: value\n");
    defer stream.deinit(testing.allocator);
    try testing.expectEqual(@as(usize, 2), stream.documents.len);
    try testing.expect(stream.documents[0].explicit);
    try testing.expectEqualSlices(u8, "scalar1", rootKind(stream.documents[0].document).string);
    try testing.expect(!stream.documents[1].explicit);
    try testing.expectEqualSlices(u8, "value", (try stream.documents[1].document.ast.getValByPath(&.{.{ .key = "key" }})).kind.string);
}

test "extractStream: single bare document" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const stream = try extractStream(testing.allocator, "key: value\n");
    defer stream.deinit(testing.allocator);
    try testing.expectEqual(@as(usize, 1), stream.documents.len);
    try testing.expect(!stream.documents[0].explicit);
}

test "extractStream: empty stream is one null document" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const stream = try extractStream(testing.allocator, "");
    defer stream.deinit(testing.allocator);
    try testing.expectEqual(@as(usize, 1), stream.documents.len);
    try testing.expect(rootKind(stream.documents[0].document) == .null_);
}

test "extractStream: outerSpan lifts node spans into outer coordinates" {
    if (comptime !build_options.lang_yaml) return error.SkipZigTest;
    const src = "---\nfoo\n---\nbar\n";
    const stream = try extractStream(testing.allocator, src);
    defer stream.deinit(testing.allocator);
    try testing.expectEqual(@as(usize, 2), stream.documents.len);
    const d1 = stream.documents[1];
    const node = d1.document.ast.nodes[d1.document.ast.root];
    const outer = d1.outerSpan(d1.document.span(node));
    try testing.expectEqualSlices(u8, "bar", src[outer.start..outer.end]);
}

// --- Embed.detect / Embed.retype ------------------------------------------

test "detect: recognizes YAML frontmatter" {
    const src = "---\ntitle: hi\n---\nbody\n";
    try testing.expectEqual(@as(?Type, .{ .frontmatter = .yaml }), detect(src));
}

test "detect: recognizes JSON frontmatter" {
    const src = ";;;\n{\"title\":\"hi\"}\n;;;\nbody\n";
    try testing.expectEqual(@as(?Type, .semicolons_json), detect(src));
}

test "detect: recognizes fig frontmatter" {
    if (comptime !build_options.lang_fig) return error.SkipZigTest;
    const src = "```fig\ntitle = hi\n```\nbody\n";
    try testing.expectEqual(@as(?Type, .{ .fenced = .fig }), detect(src));
}

test "detect: recognizes YAML endmatter" {
    const src = "body prose\n```endmatter\ntitle: hi\n```\n";
    try testing.expectEqual(@as(?Type, .endmatter_yaml), detect(src));
}

test "detect: an unterminated block is still recognized (caller sees Unterminated, not NotFound)" {
    const src = "---\ntitle: hi\n";
    try testing.expectEqual(@as(?Type, .{ .frontmatter = .yaml }), detect(src));
    try testing.expectError(Error.Unterminated, locateRegion(src, detect(src).?));
}

test "detect: plain prose with no fences at all detects nothing" {
    try testing.expectEqual(@as(?Type, null), detect("just some markdown\n\nno frontmatter here\n"));
}

test "retype: YAML frontmatter -> JSON frontmatter, body preserved byte-identical" {
    const src = "---\ntitle: hi\n---\n# body\n";
    const region = try locateRegion(src, .{ .frontmatter = .yaml });
    const out = try retype(testing.allocator, src, region, .semicolons_json, "{\"title\":\"hi\"}\n");
    defer testing.allocator.free(out);
    try testing.expectEqualStrings(";;;\n{\"title\":\"hi\"}\n;;;\n# body\n", out);
}

test "retype: frontmatter -> endmatter moves the fences to the end, body first" {
    const src = "---\ntitle: hi\n---\n# body\n";
    const region = try locateRegion(src, .{ .frontmatter = .yaml });
    const out = try retype(testing.allocator, src, region, .endmatter_yaml, "title: hi\n");
    defer testing.allocator.free(out);
    try testing.expectEqualStrings("# body\n```endmatter\ntitle: hi\n```\n", out);
}