2
0

AnnotationSet.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Services\Markup\Annotation;
  3. use App\Services\AnnotationLayout;
  4. use App\Services\AnnotationMarkup;
  5. /**
  6. * 结构化的批注注入:作为 {@see AnnotationLayout} 的输出策略,在插入点写占位符而不是 HTML,
  7. * 同时记下每个占位符的结构化内容({@see Marker})。解析时占位符变成模板节点
  8. * (anno-issue、anno-issue-mark、anno-assessment、note),能渲染成任意格式。
  9. *
  10. * ```php
  11. * $set = new AnnotationSet;
  12. * $layout = AnnotationLayout::apply($content, $glossaries, $issues, $notes, true, $assessment, $set);
  13. * $markup->renderMany([['source' => $layout['content'], 'annotations' => $set, …]], $options);
  14. * ```
  15. *
  16. * 一个实例只对应一段内容(下标在这段内容里唯一)。
  17. */
  18. final class AnnotationSet implements AnnotationMarkup
  19. {
  20. /** @var list<Marker> */
  21. private array $markers = [];
  22. /** @var array<string, int> issue 区间 → pair 编号 */
  23. private array $pairs = [];
  24. public function issueOpen(array $span): string
  25. {
  26. $pair = count($this->pairs) + 1;
  27. $this->pairs[self::spanKey($span)] = $pair;
  28. return $this->add(new Marker(Marker::OPEN, $pair, 'anno-issue', [
  29. 'ids' => implode(',', $span['ids']),
  30. 'severity' => (string) ($span['severity'] ?? ''),
  31. ]));
  32. }
  33. public function issueClose(array $span): string
  34. {
  35. return $this->add(new Marker(Marker::CLOSE, $this->pairs[self::spanKey($span)] ?? 0));
  36. }
  37. public function issueMark(array $ids, ?string $severity): string
  38. {
  39. return $this->add(new Marker(Marker::POINT, 0, 'anno-issue-mark', [
  40. 'ids' => implode(',', $ids),
  41. 'severity' => (string) ($severity ?? ''),
  42. ]));
  43. }
  44. public function assessmentMark(string $id, int $score): string
  45. {
  46. return $this->add(new Marker(Marker::POINT, 0, 'anno-assessment', ['id' => $id, 'score' => (string) $score]));
  47. }
  48. /**
  49. * 没有评估的句子的灰色对勾(同段有别的句子评估过时补上)。
  50. */
  51. public function unassessedMark(): string
  52. {
  53. return $this->add(new Marker(Marker::POINT, 0, 'anno-assessment'));
  54. }
  55. /**
  56. * 边注 / 义注对应:一个 note 模板,正文是 markdown(解析成节点,格式与模板都保留)。
  57. *
  58. * @param array<string, string> $params trigger / cite / citelink
  59. * @param string $contentType 正文的方言(取自批注那一行)
  60. */
  61. public function note(string $body, array $params = [], string $contentType = 'markdown'): string
  62. {
  63. return $this->add(new Marker(Marker::POINT, 0, 'note', $params, $body, $contentType));
  64. }
  65. public function get(int $index): ?Marker
  66. {
  67. return $this->markers[$index] ?? null;
  68. }
  69. private function add(Marker $marker): string
  70. {
  71. $this->markers[] = $marker;
  72. return AnnotationToken::make(count($this->markers) - 1);
  73. }
  74. /**
  75. * @param array{start: int, end: int} $span
  76. */
  77. private static function spanKey(array $span): string
  78. {
  79. return $span['start'].'-'.$span['end'];
  80. }
  81. }