@cardenelabs/dragon 0.41.0 → 0.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +21 -1
  2. package/dist/cli.cjs +4122 -969
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +4122 -969
  5. package/dist/cli.js.map +1 -1
  6. package/dist/export.cjs +3704 -942
  7. package/dist/export.cjs.map +1 -1
  8. package/dist/export.d.cts +1 -1
  9. package/dist/export.d.ts +1 -1
  10. package/dist/export.js +3704 -942
  11. package/dist/export.js.map +1 -1
  12. package/dist/index.cjs +4016 -815
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.cts +159 -3
  15. package/dist/index.d.ts +159 -3
  16. package/dist/index.js +4016 -815
  17. package/dist/index.js.map +1 -1
  18. package/dist/{ink-B_7OXxtb.d.cts → ink-B8EZL7JV.d.cts} +95 -3
  19. package/dist/{ink-B_7OXxtb.d.ts → ink-B8EZL7JV.d.ts} +95 -3
  20. package/dist/node.cjs +2461 -726
  21. package/dist/node.cjs.map +1 -1
  22. package/dist/node.js +2461 -726
  23. package/dist/node.js.map +1 -1
  24. package/dist/player.global.js +2 -2
  25. package/package.json +1 -1
  26. package/src/export/capture.ts +17 -5
  27. package/src/export/fonts.ts +19 -1
  28. package/src/export/index.ts +2 -2
  29. package/src/field-messages.ts +12 -0
  30. package/src/figure-text.ts +26 -0
  31. package/src/index.ts +11 -1
  32. package/src/layout/scene.ts +23 -2
  33. package/src/layout/time.ts +680 -0
  34. package/src/look/values.ts +142 -2
  35. package/src/messages.ts +108 -5
  36. package/src/parser.ts +427 -6
  37. package/src/player.ts +48 -10
  38. package/src/printer.ts +63 -12
  39. package/src/render/icons.ts +2 -0
  40. package/src/render/parts.ts +29 -9
  41. package/src/render/svg.ts +129 -21
  42. package/src/render/time.ts +291 -0
  43. package/src/schema.ts +109 -10
  44. package/src/schemas/diagram.schema.json +224 -12
  45. package/src/schemas/validate-diagram.js +1 -1
  46. package/src/timeline.ts +147 -10
  47. package/src/words.ts +39 -6
package/src/parser.ts CHANGED
@@ -6,13 +6,14 @@
6
6
  *
7
7
  * sequence <題> [view: fixed か follow] 書かなければ字の大きさで選ぶ。図の種類は sequence と architecture と state
8
8
  * group <名札> as <名前> [note: <添え書き>, color: <色>, icon: <印>]
9
- * box <名札> as <名前> [note: <下の小さな字>, color: <色>, icon: <印>]
9
+ * box <名札> as <名前> [note: <下の小さな字>, color: <色>, icon: <印>, link: <飛び先>]
10
10
  * end 同じ名札の group はもう一度開ける
11
11
  * box <名札> as <名前> [...] as が無ければ名札を名前にする
12
12
  * strip <題>: <欄>, <欄>
13
13
  * step <段の題>
14
14
  * say <段の本文> say の下に字下げした行は本文の続き
15
15
  * code <段で打つ命令>
16
+ * source <出典の名前> [url: <URL>] 1 つの段に何行でも書ける
16
17
  * <名札> -> <名札>: <線の名前> [carry: <運ぶ物>, tag: <値>, side: <側>]
17
18
  * act <名札>: <処理の名前> 箱の中の処理
18
19
  * note <名札>: <字> 箱の下の小さな字の書き換え
@@ -22,6 +23,9 @@
22
23
  * 空白を含む名前と文は行の終わりか `[` の前に置き、`[` を含む名前と `,` か `]` を含む属性の値だけを
23
24
  * `"` で囲む。囲みの中では `\"` が `"`、`\\` が `\` になる。字下げは `say` の続きの行を見分ける時だけ意味を持つ。
24
25
  *
26
+ * 箱の飛び先 (`link`) と段の出典の `url` は描いた図と段の一覧の link にそのまま入るため、頁を移る形
27
+ * (`/` で始まる path か、`http://` か `https://` で始まる URL) だけを受ける (#3199)。
28
+ *
25
29
  * 状態遷移図 (`state`) では、線の送る側に始まりの印 `start`、受ける側に終わりの印 `finish` を書ける。
26
30
  * 最初の `step` より前に書いた線は、移れるが段で通るとは限らない道 (`state.paths`) になり、属性を持たない (#3163)。
27
31
  * 状態遷移図の `start` と `finish` は箱の名札に使えない。時系列図と構成図では宣言して使う普通の名札のまま。
@@ -32,6 +36,21 @@
32
36
  * 線の行は札を動かす行になり、送る側に枝の名札か `HEAD`、受ける側に積んだ commit を書く。運ぶ物は書かなければ送る側の名前の札。
33
37
  * 枝分かれの図では `HEAD` を名札に使えず、区画 (`group`) は書けない。`branch` と `commit` の行は枝分かれの図でだけ書ける。
34
38
  *
39
+ * 時間軸の図 (`time`、#3165) は、最初の `step` より前に目盛りと区切りと印の列と節目と拡大の範囲を宣言する。
40
+ *
41
+ * scale <単位> <長さ> <数> [from: <最初の番号>] 長さは数と単位 (ms、s、min、h、d) の組 (`12s`)
42
+ * period <名前> <目盛りの数> [from: <最初の番号>]
43
+ * row <名札> as <名前> [color: <色>, empty: <目盛り> <目盛り>]
44
+ * point <名札> at <目盛り>
45
+ * zoom <目盛り>-<目盛り>
46
+ *
47
+ * 段の中では `now -> <目盛り>` で針をその目盛りの終わりまで進め、`mark <列> <範囲> [color: <色>]` で印の色を変え、
48
+ * `span <範囲>: <字> [shape: box, color: <色>]` で範囲に括弧か囲みを出す。目盛りは 0 から数え、範囲は `0-31` か `0` と書く。
49
+ * 箱は拡大の帯に置き、箱どうしの線は `[at: <目盛り>]` で列を選び、受ける箱を別の列から選ぶ時は `[into: <目盛り>]` を足す。
50
+ * 節目どうしの線は軸の上に引き、`note <節目>: <状態> [color: <色>]` で節目の状態と丸の色を変える。
51
+ * 中身の帯の欄の名前の後ろに `[meter: <割合>]` を書くと物差しになり、`set` の値を割合 (`97%` か `2/3`) で書く。
52
+ * 印の列と箱は同じ名札を持てる (空いた目盛りの列でその箱を空きの枠にするため)。時間軸の図では `now` を名札に使えない。
53
+ *
35
54
  * 行の頭の語と同じ綴りの名札 (`end` や `step`) も書ける。名札の次に `->` が来る行は線と読む。
36
55
  * 誤りは最初の 1 つで止め、行と列を示す。誤りの文の言葉は呼ぶ側が渡す。
37
56
  */
@@ -49,11 +68,38 @@ import {
49
68
  type Region,
50
69
  type StatePath,
51
70
  type Step,
71
+ type StepSource,
72
+ type StripMeter,
73
+ type TimePeriod,
74
+ type TimePoint,
75
+ type TimeRow,
76
+ type TimeScale,
52
77
  type ViewMode,
53
78
  } from "./schema";
54
79
  import { columnOf, isBlankOrComment, type SourceLine, tokenize } from "./tokenize";
55
80
  import { FORMAT_VERSION } from "./version";
56
- import { AS, ATTRS, CARRIES, COPY, FROM, HEAD, HEADS, KINDS, MERGE, type NotationLang, OLD_HEADS, ON, SIDES, STATE_ENDS, VIEWS } from "./words";
81
+ import {
82
+ AS,
83
+ AT,
84
+ ATTRS,
85
+ CARRIES,
86
+ COPY,
87
+ FROM,
88
+ HEAD,
89
+ HEADS,
90
+ KINDS,
91
+ MERGE,
92
+ type NotationLang,
93
+ NOW,
94
+ OLD_HEADS,
95
+ ON,
96
+ SIDES,
97
+ SPAN_SHAPES,
98
+ STATE_ENDS,
99
+ TIME_EDGE_ATTRS,
100
+ TIME_HEADS,
101
+ VIEWS,
102
+ } from "./words";
57
103
 
58
104
  export interface LineError {
59
105
  line: number;
@@ -91,6 +137,27 @@ const COMMIT = new RegExp(`^commit\\s+([^\\s[]+)(?:\\s+${ON}\\s+([^\\s[]+))?(?:\
91
137
  const CUT = new RegExp(`^([^\\s[]+)\\s+${FROM}(?:\\s+([^\\s[]+))?\\s*$`, "du");
92
138
  /** commit の hash。原本の JSON の hash の形と同じ */
93
139
  const HASH = /^[0-9a-f]{4,40}$/u;
140
+ /** 箱の飛び先と段の出典の URL。原本の JSON の URL の形と同じ */
141
+ const URL_FORM = /^(\/|https?:\/\/)/u;
142
+ /** 時間軸の図の宣言の行。`scale <単位> <長さ> <数>`、`period <名前> <数>`、`point <名札> at <目盛り>`、`zoom <範囲>` */
143
+ const SCALE = /^scale\s+(\S+)\s+(\S+)\s+([^\s[]+)/du;
144
+ const PERIOD = /^period\s+(\S+)\s+([^\s[]+)/du;
145
+ const POINT = new RegExp(`^point\\s+(\\S+)\\s+${AT}\\s+(\\S+)\\s*$`, "du");
146
+ const ZOOM = /^zoom\s+(\S+)\s*$/du;
147
+ /** 時間軸の図の段の中の行。`now -> <目盛り>`、`mark <列> <範囲>`、`span <範囲>:` */
148
+ const NEEDLE = new RegExp(`^${NOW}\\s*->`, "u");
149
+ const NOW_LINE = new RegExp(`^${NOW}\\s*->\\s*(\\S+)\\s*$`, "du");
150
+ const MARK = /^mark\s+([^\s[]+)\s+([^\s[]+)/du;
151
+ const SPAN = /^span\s+([^\s:[]+)\s*:/du;
152
+ /** 目盛りの長さ。原本の JSON の長さの形と同じ */
153
+ const LENGTH = /^[0-9]+(\.[0-9]+)?(ms|s|min|h|d)$/u;
154
+ /** 割合。原本の JSON の割合の形と同じ */
155
+ const RATIO = /^([0-9]+\/[0-9]+|[0-9]+(\.[0-9]+)?%)$/u;
156
+ /** 1 以上の整数と、0 以上の整数 */
157
+ const COUNT = /^[1-9][0-9]*$/u;
158
+ const NUMBER = /^(0|[1-9][0-9]*)$/u;
159
+ /** 目盛りの範囲。`0-31` か `0` */
160
+ const RANGE = /^(0|[1-9][0-9]*)(?:-(0|[1-9][0-9]*))?$/u;
94
161
 
95
162
  const isOneOf = <T extends string>(list: readonly T[], word: string): word is T => (list as readonly string[]).includes(word);
96
163
 
@@ -266,6 +333,7 @@ interface Building {
266
333
  title: string;
267
334
  body?: string[];
268
335
  code?: string;
336
+ sources: StepSource[];
269
337
  events: DiagramEvent[];
270
338
  }
271
339
 
@@ -287,6 +355,21 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
287
355
 
288
356
  const isState = kind === "state";
289
357
  const isGit = kind === "git";
358
+ const isTime = kind === "time";
359
+ /** 時間軸の図の目盛りと区切りと印の列と節目と拡大の範囲 (#3165) */
360
+ let scale: TimeScale | undefined;
361
+ let period: TimePeriod | undefined;
362
+ const rows: TimeRow[] = [];
363
+ const points: TimePoint[] = [];
364
+ let zoom: { from: number; to: number } | undefined;
365
+ const meters: StripMeter[] = [];
366
+ /** 目盛りの数が決まってから確かめる、宣言の行の目盛り。最初の step で確かめる */
367
+ const pending: (() => void)[] = [];
368
+ let settled = false;
369
+ /** 最初に宣言した箱の行。時間軸の図で拡大の範囲が無い時に名指す */
370
+ let firstBox: SourceLine | undefined;
371
+ /** 針が今いる目盛りの境 (0 から数える) */
372
+ let needle = 0;
290
373
  const branches: GitBranch[] = [];
291
374
  /** 枝分かれの図で積んだ commit と、枝の札が今指す commit (まだ何も指さない枝は `null`) */
292
375
  const commits = new Set<string>();
@@ -327,6 +410,10 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
327
410
  if (id !== HEAD && !tips.has(id)) throw fail(source, at, m.undeclaredRef(id));
328
411
  return id;
329
412
  }
413
+ if (isTime) {
414
+ if (!boxes.some((box) => box.id === id) && !points.some((point) => point.id === id)) throw fail(source, at, m.undeclaredNote(id));
415
+ return id;
416
+ }
330
417
  if (!isState || !isOneOf(STATE_ENDS, id)) return boxAt(source, id, at);
331
418
  if ((id === "start") !== (side === "from")) throw fail(source, at, id === "start" ? m.intoStart : m.outOfFinish);
332
419
  return id;
@@ -335,6 +422,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
335
422
  const notReserved = (source: SourceLine, id: string, at: number): void => {
336
423
  if (isState && isOneOf(STATE_ENDS, id)) throw fail(source, at, m.reservedId(id));
337
424
  if (isGit && id === HEAD) throw fail(source, at, m.headReserved(id));
425
+ if (isTime && id === NOW) throw fail(source, at, m.nowReserved(id));
338
426
  };
339
427
  /** 段の中に書く行の、書き先の段。最初の `step` より前なら誤り */
340
428
  const inStep = (source: SourceLine): Building => {
@@ -352,6 +440,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
352
440
  title: building.title,
353
441
  body: building.body.join("\n"),
354
442
  ...(building.code !== undefined ? { code: building.code } : {}),
443
+ ...(building.sources.length > 0 ? { sources: building.sources } : {}),
355
444
  events: building.events,
356
445
  });
357
446
  };
@@ -361,6 +450,11 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
361
450
  const source = rest[i];
362
451
  if (!source || isBlankOrComment(source)) continue;
363
452
  const text = source.text;
453
+ // 時間軸の図の `now -> <目盛り>` は針を進める行で、線と同じ形でも箱どうしの線と読まない
454
+ if (isTime && NEEDLE.test(text)) {
455
+ inStep(source).events.push(readNow(source));
456
+ continue;
457
+ }
364
458
  // 名札の次に `->` が来る行は、名札が行の頭の語と同じ綴りでも線と読む
365
459
  const edge = EDGE.exec(text);
366
460
  if (edge) {
@@ -377,6 +471,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
377
471
  continue;
378
472
  }
379
473
  if (!isGit && (word === "branch" || word === "commit")) throw fail(source, 0, m.gitOnly(word));
474
+ if (!isTime && isOneOf(TIME_HEADS, word)) throw fail(source, 0, m.timeOnly(word));
380
475
  if (!isOneOf(HEADS, word)) {
381
476
  const old = oldForm(text, building !== undefined);
382
477
  throw fail(source, 0, old ? m.oldForm(old.word, old.shape) : m.unknownHead(word));
@@ -398,10 +493,11 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
398
493
  break;
399
494
  case "step": {
400
495
  if (group) throw fail(group.source, 0, m.unclosedGroup(group.id));
496
+ settle();
401
497
  finish();
402
498
  const stepTitle = text.slice(word.length).trim();
403
499
  if (stepTitle === "") throw fail(source, 0, m.emptyStepTitle);
404
- building = { source, title: stepTitle, events: [] };
500
+ building = { source, title: stepTitle, sources: [], events: [] };
405
501
  break;
406
502
  }
407
503
  case "say":
@@ -415,6 +511,9 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
415
511
  step.code = command;
416
512
  break;
417
513
  }
514
+ case "source":
515
+ inStep(source).sources.push(readSource(source));
516
+ break;
418
517
  case "act": {
419
518
  const step = inStep(source);
420
519
  const on = readOnBox(source, word, m.actShape);
@@ -423,6 +522,10 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
423
522
  }
424
523
  case "note": {
425
524
  const step = inStep(source);
525
+ if (isTime) {
526
+ step.events.push(readTimeNote(source));
527
+ break;
528
+ }
426
529
  const on = readOnBox(source, word, m.noteShape);
427
530
  step.events.push({ type: "swap", box: on.box, text: on.text });
428
531
  break;
@@ -438,13 +541,35 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
438
541
  step.events.push(readCommit(source));
439
542
  break;
440
543
  }
544
+ case "scale":
545
+ readScale(source);
546
+ break;
547
+ case "period":
548
+ readPeriod(source);
549
+ break;
550
+ case "row":
551
+ readRow(source);
552
+ break;
553
+ case "point":
554
+ readPoint(source);
555
+ break;
556
+ case "zoom":
557
+ readZoom(source);
558
+ break;
559
+ case "mark":
560
+ inStep(source).events.push(readMark(source));
561
+ break;
562
+ case "span":
563
+ inStep(source).events.push(readSpan(source));
564
+ break;
441
565
  }
442
566
  }
443
567
  if (group) throw fail(group.source, 0, m.unclosedGroup(group.id));
568
+ settle();
444
569
  finish();
445
570
 
446
571
  if (isGit && branches.length === 0) throw fail(head, 0, m.noBranches);
447
- if (boxes.length === 0 && !isGit) throw fail(head, 0, m.noBoxes);
572
+ if (boxes.length === 0 && !isGit && !isTime) throw fail(head, 0, m.noBoxes);
448
573
  if (steps.length === 0) throw fail(head, 0, m.noSteps);
449
574
 
450
575
  return {
@@ -454,13 +579,209 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
454
579
  ...(view ? { view } : {}),
455
580
  ...(regions.length > 0 ? { regions } : {}),
456
581
  boxes,
457
- ...(strip ? { strip } : {}),
582
+ ...(strip ? { strip: meters.length > 0 ? { ...strip, meters } : strip } : {}),
458
583
  steps,
459
584
  ...(kind === "sequence" ? { sequence: { lifelines: boxes.map((box) => box.id) } } : {}),
460
585
  ...(paths.length > 0 ? { state: { paths } } : {}),
461
586
  ...(isGit ? { git: { branches } } : {}),
587
+ ...(isTime && scale
588
+ ? {
589
+ time: {
590
+ scale,
591
+ ...(period ? { period } : {}),
592
+ rows,
593
+ ...(points.length > 0 ? { points } : {}),
594
+ ...(zoom ? { zoom } : {}),
595
+ },
596
+ }
597
+ : {}),
462
598
  };
463
599
 
600
+ /**
601
+ * 時間軸の図の宣言を、目盛りの数が決まってから確かめる。最初の step と記法の終わりで 1 度だけ呼ぶ。
602
+ * 目盛りと印の列が無い図と、拡大の範囲の無い図の箱は誤り
603
+ */
604
+ function settle(): void {
605
+ if (!isTime || settled) return;
606
+ settled = true;
607
+ if (!scale) throw fail(head as SourceLine, 0, m.noScale);
608
+ if (rows.length === 0) throw fail(head as SourceLine, 0, m.noRows);
609
+ for (const check of pending) check();
610
+ if (firstBox && !zoom) throw fail(firstBox, 0, m.boxNeedsZoom);
611
+ }
612
+
613
+ /** 目盛りの数。目盛りを宣言する前は 0 */
614
+ function countOf(): number {
615
+ return scale?.count ?? 0;
616
+ }
617
+
618
+ /** `text` を目盛りの位置として読む。0 から目盛りの数より 1 小さい数まで */
619
+ function unitAt(source: SourceLine, at: number, text: string): number {
620
+ if (!NUMBER.test(text) || Number(text) >= countOf()) throw fail(source, at, m.badUnit(text, countOf()));
621
+ return Number(text);
622
+ }
623
+
624
+ /** `text` を目盛りの範囲として読む。`0-31` か `0` */
625
+ function rangeAt(source: SourceLine, at: number, text: string): { from: number; to: number } {
626
+ const found = RANGE.exec(text);
627
+ const from = Number(found?.[1]);
628
+ const to = Number(found?.[2] ?? found?.[1]);
629
+ if (!found || from > to || to >= countOf()) throw fail(source, at, m.badUnit(text, countOf()));
630
+ return { from, to };
631
+ }
632
+
633
+ /** `scale <単位> <長さ> <数> [from: <最初の番号>]` の行 */
634
+ function readScale(source: SourceLine): void {
635
+ beforeSteps(source);
636
+ if (scale) throw fail(source, 0, m.timeTwice("scale"));
637
+ const text = source.text;
638
+ const found = SCALE.exec(text);
639
+ const at = (k: number): number => found?.indices?.[k]?.[0] ?? 0;
640
+ if (!found) throw fail(source, 0, m.scaleShape);
641
+ const rest = skipSpaces(text, found[0].length);
642
+ if (rest < text.length && text[rest] !== "[") throw fail(source, rest, m.scaleShape);
643
+ const length = found[2] ?? "";
644
+ if (!LENGTH.test(length)) throw fail(source, at(2), m.badLength);
645
+ const count = found[3] ?? "";
646
+ if (!COUNT.test(count)) throw fail(source, at(3), m.badCount("scale"));
647
+ const first = rest < text.length ? readAttrs(source, rest, ATTRS.scale, m).get("from") : undefined;
648
+ if (first && !NUMBER.test(first.value)) throw fail(source, first.at, m.scaleShape);
649
+ scale = { unit: found[1] ?? "", length, count: Number(count), ...(first ? { from: Number(first.value) } : {}) };
650
+ }
651
+
652
+ /** `period <名前> <目盛りの数> [from: <最初の番号>]` の行 */
653
+ function readPeriod(source: SourceLine): void {
654
+ beforeSteps(source);
655
+ if (period) throw fail(source, 0, m.timeTwice("period"));
656
+ const text = source.text;
657
+ const found = PERIOD.exec(text);
658
+ if (!found) throw fail(source, 0, m.periodShape);
659
+ const rest = skipSpaces(text, found[0].length);
660
+ if (rest < text.length && text[rest] !== "[") throw fail(source, rest, m.periodShape);
661
+ const length = found[2] ?? "";
662
+ if (!COUNT.test(length)) throw fail(source, found.indices?.[2]?.[0] ?? 0, m.badCount("period"));
663
+ const first = rest < text.length ? readAttrs(source, rest, ATTRS.period, m).get("from") : undefined;
664
+ if (first && !NUMBER.test(first.value)) throw fail(source, first.at, m.periodShape);
665
+ period = { name: found[1] ?? "", length: Number(length), ...(first ? { from: Number(first.value) } : {}) };
666
+ }
667
+
668
+ /** `row <名札> as <名前> [color: <色>, empty: <目盛り> <目盛り>]` の行。箱と同じ名札を持てる */
669
+ function readRow(source: SourceLine): void {
670
+ beforeSteps(source);
671
+ const declared = readDeclared(source, "row", ATTRS.row, m);
672
+ notReserved(source, declared.id, declared.idAt);
673
+ if (rows.some((row) => row.id === declared.id) || points.some((point) => point.id === declared.id)) throw fail(source, declared.idAt, m.duplicate(declared.id));
674
+ const color = declared.attrs.get("color");
675
+ if (color && !isOneOf(COLORS, color.value)) throw fail(source, color.at, m.badColor);
676
+ const empty = declared.attrs.get("empty");
677
+ const row: TimeRow = { id: declared.id, name: declared.name ?? declared.id, ...(color ? { color: color.value as Color } : {}) };
678
+ rows.push(row);
679
+ if (empty) {
680
+ const words = empty.value.split(/\s+/u);
681
+ // 目盛りの数が決まってから確かめる。欄の並びを宣言の形で残すため、先に空の並びを置く
682
+ row.empty = [];
683
+ pending.push(() => {
684
+ row.empty = words.map((word) => unitAt(source, empty.at, word));
685
+ });
686
+ }
687
+ }
688
+
689
+ /** `point <名札> at <目盛り>` の行。箱と印の列と同じ名札は持てない */
690
+ function readPoint(source: SourceLine): void {
691
+ beforeSteps(source);
692
+ const found = POINT.exec(source.text);
693
+ const id = found?.[1];
694
+ const unit = found?.[2];
695
+ if (!found || id === undefined || unit === undefined) throw fail(source, 0, m.pointShape);
696
+ const idAt = found.indices?.[1]?.[0] ?? 0;
697
+ const unitStart = found.indices?.[2]?.[0] ?? 0;
698
+ if (!ID.test(id)) throw fail(source, idAt, m.badId(id));
699
+ notReserved(source, id, idAt);
700
+ if (rows.some((row) => row.id === id)) throw fail(source, idAt, m.duplicate(id));
701
+ declare(source, id, idAt);
702
+ const point: TimePoint = { id, at: 0 };
703
+ points.push(point);
704
+ pending.push(() => {
705
+ point.at = unitAt(source, unitStart, unit);
706
+ });
707
+ }
708
+
709
+ /** `zoom <目盛り>-<目盛り>` の行 */
710
+ function readZoom(source: SourceLine): void {
711
+ beforeSteps(source);
712
+ if (zoom) throw fail(source, 0, m.timeTwice("zoom"));
713
+ const found = ZOOM.exec(source.text);
714
+ const range = found?.[1];
715
+ if (!found || range === undefined) throw fail(source, 0, m.zoomShape);
716
+ const rangeStart = found.indices?.[1]?.[0] ?? 0;
717
+ const placed = { from: 0, to: 0 };
718
+ zoom = placed;
719
+ pending.push(() => {
720
+ Object.assign(placed, rangeAt(source, rangeStart, range));
721
+ });
722
+ }
723
+
724
+ /** `now -> <目盛り>` の行。針をその目盛りの終わりまで進める。前の針より後の目盛りに限る */
725
+ function readNow(source: SourceLine): DiagramEvent {
726
+ const found = NOW_LINE.exec(source.text);
727
+ const unit = found?.[1];
728
+ if (!found || unit === undefined) throw fail(source, 0, m.nowShape);
729
+ const at = found.indices?.[1]?.[0] ?? 0;
730
+ const to = unitAt(source, at, unit);
731
+ if (to + 1 <= needle) throw fail(source, at, m.nowBack(unit, needle - 1));
732
+ needle = to + 1;
733
+ return { type: "now", to };
734
+ }
735
+
736
+ /** `mark <列> <範囲> [color: <色>]` の行 */
737
+ function readMark(source: SourceLine): DiagramEvent {
738
+ const text = source.text;
739
+ const found = MARK.exec(text);
740
+ const row = found?.[1];
741
+ const range = found?.[2];
742
+ if (!found || row === undefined || range === undefined) throw fail(source, 0, m.markShape);
743
+ if (!rows.some((r) => r.id === row)) throw fail(source, found.indices?.[1]?.[0] ?? 0, m.undeclaredRow(row));
744
+ const { from, to } = rangeAt(source, found.indices?.[2]?.[0] ?? 0, range);
745
+ const rest = skipSpaces(text, found[0].length);
746
+ if (rest >= text.length || text[rest] !== "[") throw fail(source, rest, m.markShape);
747
+ const color = readAttrs(source, rest, ATTRS.mark, m).get("color");
748
+ if (!color) throw fail(source, rest, m.markShape);
749
+ if (!isOneOf(COLORS, color.value)) throw fail(source, color.at, m.badColor);
750
+ return { type: "mark", row, from, to, color: color.value };
751
+ }
752
+
753
+ /** `span <範囲>: <字> [shape: box, color: <色>]` の行 */
754
+ function readSpan(source: SourceLine): DiagramEvent {
755
+ const found = SPAN.exec(source.text);
756
+ const range = found?.[1];
757
+ if (!found || range === undefined) throw fail(source, 0, m.spanShape);
758
+ const { from, to } = rangeAt(source, found.indices?.[1]?.[0] ?? 0, range);
759
+ const named = readName(source, found[0].length, m);
760
+ if (named.value === "") throw fail(source, 0, m.spanShape);
761
+ const attrs = named.rest < source.text.length ? readAttrs(source, named.rest, ATTRS.span, m) : new Map<string, Attr>();
762
+ const shape = attrs.get("shape");
763
+ if (shape && !isOneOf(SPAN_SHAPES, shape.value)) throw fail(source, shape.at, m.badShape);
764
+ const color = attrs.get("color");
765
+ if (color && !isOneOf(COLORS, color.value)) throw fail(source, color.at, m.badColor);
766
+ return { type: "span", from, to, text: named.value, ...(shape ? { shape: "box" as const } : {}), ...(color ? { color: color.value as Color } : {}) };
767
+ }
768
+
769
+ /** 時間軸の図の `note <箱か節目>: <字> [color: <色>]` の行。色は節目の丸の色 */
770
+ function readTimeNote(source: SourceLine): DiagramEvent {
771
+ const found = /^note\s+([^\s:]+)\s*:/du.exec(source.text);
772
+ const id = found?.[1];
773
+ if (!found || id === undefined) throw fail(source, 0, m.noteShape);
774
+ const idAt = found.indices?.[1]?.[0] ?? 0;
775
+ const isPoint = points.some((point) => point.id === id);
776
+ if (!isPoint && !boxes.some((box) => box.id === id)) throw fail(source, idAt, m.undeclaredNote(id));
777
+ const named = readName(source, found[0].length, m);
778
+ if (named.value === "") throw fail(source, 0, m.noteShape);
779
+ const color = named.rest < source.text.length ? readAttrs(source, named.rest, ATTRS.note, m).get("color") : undefined;
780
+ if (color && !isPoint) throw fail(source, color.at, m.noteColorPointOnly);
781
+ if (color && !isOneOf(COLORS, color.value)) throw fail(source, color.at, m.badColor);
782
+ return { type: "swap", box: id, text: named.value, ...(color ? { color: color.value as Color } : {}) };
783
+ }
784
+
464
785
  /** `group` の行。同じ名札をもう一度開いた時は、名前も属性も書かない */
465
786
  function readGroup(source: SourceLine): void {
466
787
  beforeSteps(source);
@@ -483,16 +804,33 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
483
804
  const declared = readDeclared(source, "box", ATTRS.box, m);
484
805
  notReserved(source, declared.id, declared.idAt);
485
806
  declare(source, declared.id, declared.idAt);
807
+ firstBox ??= source;
486
808
  const { note, ...look } = lookOf(source, declared.attrs, m);
809
+ const link = declared.attrs.get("link");
487
810
  boxes.push({
488
811
  id: declared.id,
489
812
  name: declared.name ?? declared.id,
490
813
  ...(note !== undefined ? { sub: note } : {}),
491
814
  ...look,
492
815
  ...(group ? { region: group.id } : {}),
816
+ ...(link ? { link: urlOf(source, "link", link) } : {}),
493
817
  });
494
818
  }
495
819
 
820
+ /** `source <名前> [url: <URL>]` の行 */
821
+ function readSource(source: SourceLine): StepSource {
822
+ const named = readName(source, "source".length, m);
823
+ const url = named.rest < source.text.length ? readAttrs(source, named.rest, ATTRS.source, m).get("url") : undefined;
824
+ if (named.value === "" || !url) throw fail(source, 0, m.sourceShape);
825
+ return { name: named.value, url: urlOf(source, "url", url) };
826
+ }
827
+
828
+ /** 頁を移る形の URL。ほかの形 (`javascript:` など) は、その属性の位置を示して断る */
829
+ function urlOf(source: SourceLine, key: string, attr: Attr): string {
830
+ if (!URL_FORM.test(attr.value)) throw fail(source, attr.at, m.badUrl(key));
831
+ return attr.value;
832
+ }
833
+
496
834
  /** 枝分かれの図の `branch <名札> [note: <添え書き>]` の行。枝は宣言した順に上から帯に並ぶ */
497
835
  function readBranch(source: SourceLine): void {
498
836
  beforeSteps(source);
@@ -577,7 +915,55 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
577
915
  stripTitle = colon < 0 ? "" : text.slice(start, colon).trim();
578
916
  }
579
917
  if (stripTitle === "" || text[colon] !== ":") throw fail(source, 0, m.stripShape);
580
- strip = { title: stripTitle, fields: readList(source, colon + 1, m) };
918
+ if (isTime) {
919
+ strip = { title: stripTitle, fields: readFields(source, colon + 1) };
920
+ return;
921
+ }
922
+ const fields = readList(source, colon + 1, m);
923
+ // 物差しは時間軸の図だけが書ける。ほかの図では欄の名前の一部として読んでいた形なので、欄が属性の形で終わる時だけ断る
924
+ const metered = /\[\s*meter\s*:/u.exec(text);
925
+ if (metered && fields.some((f) => /\[\s*meter\s*:[^\]]*\]$/u.test(f))) throw fail(source, metered.index + 1, m.timeOnly("meter"));
926
+ strip = { title: stripTitle, fields };
927
+ }
928
+
929
+ /**
930
+ * 時間軸の図の `strip` の行の欄。`,` で区切り、欄の名前の後ろに `[meter: <割合>]` を書いた欄は物差しにする。
931
+ * `[` を含む名前は `"` で囲む
932
+ */
933
+ function readFields(source: SourceLine, at: number): string[] {
934
+ const text = source.text;
935
+ const items: string[] = [];
936
+ for (let i = at; ; ) {
937
+ const start = skipSpaces(text, i);
938
+ let value: string;
939
+ let end: number;
940
+ if (text[start] === '"') {
941
+ const quoted = readQuoted(source, start, m);
942
+ value = quoted.value;
943
+ end = skipSpaces(text, quoted.end);
944
+ } else {
945
+ end = start;
946
+ while (end < text.length && text[end] !== "," && text[end] !== "[") end++;
947
+ value = text.slice(start, end).trim();
948
+ }
949
+ if (value === "") throw fail(source, start, m.emptyStripField);
950
+ if (text[end] === "[") {
951
+ const close = text.indexOf("]", end);
952
+ if (close < 0) throw fail(source, end, m.attrUnclosed);
953
+ const inside = text.slice(end + 1, close);
954
+ const inner = /^\s*([a-z]*)\s*:\s*(.*?)\s*$/u.exec(inside);
955
+ if (!inner) throw fail(source, end + 1, m.attrNoColon(inside.trim()));
956
+ const key = inner[1] ?? "";
957
+ if (!(ATTRS.strip as readonly string[]).includes(key)) throw fail(source, end + 1, m.badAttr(key, ATTRS.strip));
958
+ if (!RATIO.test(inner[2] ?? "")) throw fail(source, end + 1, m.badRatio);
959
+ meters.push({ field: value, line: inner[2] ?? "" });
960
+ end = skipSpaces(text, close + 1);
961
+ }
962
+ if (end < text.length && text[end] !== ",") throw fail(source, end, m.afterQuote);
963
+ items.push(value);
964
+ if (end >= text.length) return items;
965
+ i = end + 1;
966
+ }
581
967
  }
582
968
 
583
969
  /**
@@ -647,6 +1033,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
647
1033
  if (carried && !isOneOf(CARRIES, carried.value)) throw fail(source, carried.at, m.badCarry);
648
1034
  const side = attrs.get("side");
649
1035
  if (side && !isOneOf(SIDES, side.value)) throw fail(source, side.at, m.badSide);
1036
+ const columns = readColumns(source, from, to, attrs, at);
650
1037
  // 枝分かれの図の線は札を動かす線で、書かなければ送る側の名前の札を運ぶ
651
1038
  let carry: Carry = isGit ? { kind: "tag", text: from } : { kind: "envelope" };
652
1039
  if (tag) carry = { kind: "tag", text: tag.value };
@@ -659,7 +1046,40 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
659
1046
  ...(label !== undefined ? { label } : {}),
660
1047
  carry,
661
1048
  ...(side ? { labelSide: side.value as LabelSide } : {}),
1049
+ ...columns,
1050
+ };
1051
+ }
1052
+
1053
+ /**
1054
+ * 線の列 (`at` と `into`)。時間軸の図の箱どうしの線は `at` で拡大の帯の列を選び、節目どうしの線は列を持たない。
1055
+ * ほかの図の種類には書けない
1056
+ */
1057
+ function readColumns(source: SourceLine, from: string, to: string, attrs: Map<string, Attr>, end: number): { at?: number; into?: number } {
1058
+ const given = TIME_EDGE_ATTRS.flatMap((key) => {
1059
+ const attr = attrs.get(key);
1060
+ return attr ? [{ key, attr }] : [];
1061
+ });
1062
+ const first = given[0];
1063
+ if (!isTime) {
1064
+ if (first) throw fail(source, first.attr.at, m.timeOnly(first.key));
1065
+ return {};
1066
+ }
1067
+ const isBox = (id: string): boolean => boxes.some((box) => box.id === id);
1068
+ if (isBox(from) !== isBox(to)) throw fail(source, 0, m.mixedLine);
1069
+ if (!isBox(from)) {
1070
+ if (first) throw fail(source, first.attr.at, zoom ? m.pointLineNoAt : m.noZoom);
1071
+ return {};
1072
+ }
1073
+ const column = (attr: Attr | undefined): number | undefined => {
1074
+ if (!attr) return undefined;
1075
+ const unit = unitAt(source, attr.at, attr.value);
1076
+ if (!zoom || unit < zoom.from || unit > zoom.to) throw fail(source, attr.at, zoom ? m.outOfZoom(zoom.from, zoom.to) : m.noZoom);
1077
+ return unit;
662
1078
  };
1079
+ const at = column(attrs.get("at"));
1080
+ if (at === undefined) throw fail(source, end, m.boxLineNeedsAt);
1081
+ const into = column(attrs.get("into"));
1082
+ return { at, ...(into !== undefined ? { into } : {}) };
663
1083
  }
664
1084
 
665
1085
  /** `set <欄>: <値> [tag: <札>]` か `set <欄> [tag: <札>]` の行 */
@@ -690,6 +1110,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
690
1110
  if (at < text.length && text[at] !== "[") throw fail(source, at, m.stripNeedsValue);
691
1111
  const tag = at < text.length ? readAttrs(source, at, ATTRS.set, m).get("tag")?.value : undefined;
692
1112
  if (value === undefined && tag === undefined) throw fail(source, 0, m.stripNeedsValue);
1113
+ if (value !== undefined && meters.some((meter) => meter.field === field) && !RATIO.test(value)) throw fail(source, start, m.meterValue(field));
693
1114
  return {
694
1115
  type: "strip",
695
1116
  field,