AnchorParser.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Services\RelatedParagraph;
  3. use InvalidArgumentException;
  4. /**
  5. * wbw_templates 里 `.ctl.` 锚点(CS 版 html 的 `<a name="...">`)的解析。
  6. *
  7. * 写法全表见 documents/related-paragraphs-rebuild.md §3.1(2026-11-25 全库扫描):
  8. * 单号、完整区间、缩写区间、链式、倒序笔误、截断,可带 `_dn1` 型后缀;另有章标记
  9. * `dn1_3` / `an5_1_1` / `kn14b_17`、书名标记 `kn11`、页码标记 `P1.0023`。
  10. * 表外的写法抛异常——旧命令丢段就是静默跳过造成的。
  11. */
  12. class AnchorParser
  13. {
  14. public const KIND_PARA = 'para';
  15. public const KIND_CHAPTER = 'chapter';
  16. public const KIND_BOOK = 'book';
  17. public const KIND_PAGE = 'page';
  18. /**
  19. * @return array{kind: string, begin?: int, end?: int, suffix?: string|null, name?: string, preface?: bool, warning?: string|null}
  20. *
  21. * @throws InvalidArgumentException 表外写法
  22. */
  23. public static function parse(string $word): array
  24. {
  25. if (preg_match('/^[A-Z][0-9]/', $word)) {
  26. return ['kind' => self::KIND_PAGE];
  27. }
  28. if (preg_match('/^para([0-9][0-9-]*)(?:_([a-z]+[0-9]+))?$/', $word, $m)) {
  29. return ['kind' => self::KIND_PARA, 'suffix' => $m[2] ?? null] + self::parseNumbers($m[1], $word);
  30. }
  31. // 章标记:dn1_3、an5_1_1、kn14b_17(kn14b 是 kn14 的第二册,名字仍是 kn14)。
  32. // 只有一级且为 0 的(dn1_0)表示义注序言 → dn1_A。
  33. if (preg_match('/^([a-z]+[0-9]+)[a-z]?((?:_[0-9]+)+)$/', $word, $m)) {
  34. $numbers = explode('_', ltrim($m[2], '_'));
  35. return [
  36. 'kind' => self::KIND_CHAPTER,
  37. 'name' => $m[1],
  38. 'preface' => count($numbers) === 1 && (int) $numbers[0] === 0,
  39. ];
  40. }
  41. if (preg_match('/^([a-z]+[0-9]+)[a-z]?$/', $word, $m)) {
  42. return ['kind' => self::KIND_BOOK, 'name' => $m[1]];
  43. }
  44. throw new InvalidArgumentException("无法解析的锚点:{$word}");
  45. }
  46. /**
  47. * @return array{begin: int, end: int, warning: string|null}
  48. */
  49. private static function parseNumbers(string $body, string $word): array
  50. {
  51. // para179-:右端缺失,全库仅 book 11 para 1577 一处
  52. if (str_ends_with($body, '-')) {
  53. $begin = (int) rtrim($body, '-');
  54. if (str_contains(rtrim($body, '-'), '-')) {
  55. throw new InvalidArgumentException("无法解析的锚点:{$word}");
  56. }
  57. return ['begin' => $begin, 'end' => $begin, 'warning' => "截断区间 {$word},按单号 {$begin} 处理"];
  58. }
  59. $parts = explode('-', $body);
  60. $begin = (int) $parts[0];
  61. $warning = str_starts_with($parts[0], '0') && strlen($parts[0]) > 1 ? "前导 0:{$word}" : null;
  62. $end = $begin;
  63. foreach (array_slice($parts, 1) as $digits) {
  64. $end = self::completePrefix($end, $digits);
  65. }
  66. if ($end < $begin) {
  67. // 倒序(同位数)是高位笔误:para504-407 → 504-507
  68. $end = self::repairReversed($begin, end($parts));
  69. $warning = "倒序区间 {$word},按 {$begin}-{$end} 处理";
  70. }
  71. return ['begin' => $begin, 'end' => $end, 'warning' => $warning];
  72. }
  73. /**
  74. * 缩写区间:右端位数少于左端时,用左端的高位补齐(292-3 → 293)。
  75. * 补齐后不大于左端则进一位(298-3 → 303;全库目前没有这种情况)。
  76. * 右端位数不少于左端时就是完整写法,原样返回。
  77. */
  78. public static function completePrefix(int $left, string $digits): int
  79. {
  80. $leftDigits = (string) $left;
  81. if (strlen($digits) >= strlen($leftDigits)) {
  82. return (int) $digits;
  83. }
  84. $completed = (int) (substr($leftDigits, 0, strlen($leftDigits) - strlen($digits)).$digits);
  85. return $completed > $left ? $completed : $completed + 10 ** strlen($digits);
  86. }
  87. /**
  88. * 倒序区间:逐位丢掉右端高位再补齐,取第一个比左端大、宽度不超过 20 的。
  89. */
  90. private static function repairReversed(int $begin, string $digits): int
  91. {
  92. for ($keep = strlen($digits) - 1; $keep >= 1; $keep--) {
  93. $candidate = self::completePrefix($begin, substr($digits, -$keep));
  94. if ($candidate > $begin && $candidate - $begin <= 20) {
  95. return $candidate;
  96. }
  97. }
  98. throw new InvalidArgumentException("倒序区间无法还原:para{$begin}-{$digits}");
  99. }
  100. }