2
0

RangeWrapper.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Services\Markup\Annotation;
  3. use App\Services\AnnotationLayout;
  4. use App\Services\Markup\Ir\Boundary;
  5. use App\Services\Markup\Ir\Document;
  6. use App\Services\Markup\Ir\Element;
  7. use App\Services\Markup\Ir\Node;
  8. use App\Services\Markup\Ir\Scope;
  9. /**
  10. * 把配对的区间边界({@see Boundary})换成包住中间内容的模板调用(issue 高亮)。
  11. *
  12. * {@see AnnotationLayout} 保证边界不会切断模板、术语、`**粗体**`、HTML 标签,
  13. * 但单星号斜体、链接这类没有保护,开、闭可能落在不同层级(一个在 `<em>` 里、一个在外面)。
  14. * 这时把边界提升到两者最近的共同父层:开头挪到包含它的那个子节点之前,结尾挪到包含它的子节点之后——
  15. * 相当于把高亮扩展到完整包住那个元素,与 AnnotationLayout「扩展到完整包住」的规则一致。
  16. *
  17. * 只在 Element 的 children 里找(模板参数里的内容是另一段 Document,批注不会落进去)。
  18. */
  19. final class RangeWrapper
  20. {
  21. /**
  22. * @param list<Node> $nodes 根节点列表(就地修改)
  23. * @return list<Node>
  24. */
  25. public static function apply(array $nodes, Scope $scope): array
  26. {
  27. $root = new Element('#root', [], $nodes);
  28. while (($open = self::find($root, fn (Node $n) => $n instanceof Boundary && $n->open)) !== null) {
  29. /** @var Boundary $boundary */
  30. $boundary = self::at($root, $open);
  31. $close = self::find($root, fn (Node $n) => $n instanceof Boundary && ! $n->open && $n->pair === $boundary->pair);
  32. if ($close === null || $boundary->call === null || ! self::before($open, $close)) {
  33. self::remove($root, $open);
  34. continue;
  35. }
  36. self::wrap($root, $open, $close, $boundary, $scope);
  37. }
  38. // 剩下的都是配不上对的结尾
  39. while (($orphan = self::find($root, fn (Node $n) => $n instanceof Boundary)) !== null) {
  40. self::remove($root, $orphan);
  41. }
  42. return $root->children;
  43. }
  44. /**
  45. * @param list<int> $open
  46. * @param list<int> $close
  47. */
  48. private static function wrap(Element $root, array $open, array $close, Boundary $boundary, Scope $scope): void
  49. {
  50. // 最近的共同父层:两条路径在第 k 层分叉(或其中一条在第 k 层就是边界本身)
  51. $k = 0;
  52. while ($k < count($open) - 1 && $k < count($close) - 1 && $open[$k] === $close[$k]) {
  53. $k++;
  54. }
  55. $container = $root;
  56. for ($i = 0; $i < $k; $i++) {
  57. /** @var Element $container */
  58. $container = $container->children[$open[$i]];
  59. }
  60. $from = $open[$k];
  61. $to = $close[$k];
  62. $openIsDirect = count($open) === $k + 1;
  63. $closeIsDirect = count($close) === $k + 1;
  64. // 先删深处的边界(不影响第 k 层的下标),再处理第 k 层
  65. if (! $closeIsDirect) {
  66. self::remove($root, $close);
  67. }
  68. if (! $openIsDirect) {
  69. self::remove($root, $open);
  70. }
  71. $start = $openIsDirect ? $from + 1 : $from;
  72. $end = $closeIsDirect ? $to - 1 : $to;
  73. $inner = array_slice($container->children, $start, max(0, $end - $start + 1));
  74. $call = $boundary->call;
  75. $call->slots['content'] = new Document($inner, true, '', [], $scope, fragment: true);
  76. array_splice($container->children, $from, $to - $from + 1, [$call]);
  77. }
  78. /**
  79. * 深度优先找第一个满足条件的节点,返回下标路径。
  80. *
  81. * @param callable(Node): bool $match
  82. * @return list<int>|null
  83. */
  84. private static function find(Element $element, callable $match): ?array
  85. {
  86. foreach ($element->children as $i => $child) {
  87. if ($match($child)) {
  88. return [$i];
  89. }
  90. if ($child instanceof Element && ($path = self::find($child, $match)) !== null) {
  91. return [$i, ...$path];
  92. }
  93. }
  94. return null;
  95. }
  96. /**
  97. * @param list<int> $path
  98. */
  99. private static function at(Element $root, array $path): Node
  100. {
  101. $node = $root;
  102. foreach ($path as $i) {
  103. /** @var Element $node */
  104. $node = $node->children[$i];
  105. }
  106. return $node;
  107. }
  108. /**
  109. * @param list<int> $path
  110. */
  111. private static function remove(Element $root, array $path): void
  112. {
  113. $parent = $root;
  114. foreach (array_slice($path, 0, -1) as $i) {
  115. /** @var Element $parent */
  116. $parent = $parent->children[$i];
  117. }
  118. array_splice($parent->children, (int) end($path), 1);
  119. }
  120. /**
  121. * 文档顺序上 a 在 b 之前。
  122. *
  123. * @param list<int> $a
  124. * @param list<int> $b
  125. */
  126. private static function before(array $a, array $b): bool
  127. {
  128. // 逐层比下标(PHP 的数组 < 先比长度,不是字典序)
  129. foreach ($a as $depth => $index) {
  130. if (! isset($b[$depth])) {
  131. return false;
  132. }
  133. if ($index !== $b[$depth]) {
  134. return $index < $b[$depth];
  135. }
  136. }
  137. return count($a) < count($b);
  138. }
  139. }