RenderContext.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Services\Markup\Render;
  3. use App\Services\Markup\Format;
  4. use App\Services\Markup\Ir\Document;
  5. use App\Services\Markup\Ir\Scope;
  6. use App\Services\Markup\RenderOptions;
  7. use App\Services\Markup\Resolve\Loader;
  8. use App\Services\Markup\Resolve\Resolver;
  9. /**
  10. * 一段顶层内容渲染期间的状态:选项、取数结果、脚注编号与列表、引用到的经文段落、已出现过的术语。
  11. *
  12. * 取代 v2 的 `$GLOBALS['note_sn']` / `$GLOBALS['note']` / `$GLOBALS['ref_sent']` / `$GLOBALS['term-…']`。
  13. */
  14. final class RenderContext
  15. {
  16. public int $noteNumber = 0;
  17. /** @var list<array{sn: int, trigger: string, content: string}> 脚注(html / markdown 输出末尾的列表) */
  18. public array $footnotes = [];
  19. /** @var array<int, array{0: int, 1: int}> 句子模板引用到的 [book, para](按调用去重),ref 模板据此列参考文献 */
  20. public array $sentenceRefs = [];
  21. /** @var array<string, int> 术语出现次数(markdown 输出只有第一次带巴利原词与脚注) */
  22. public array $termSeen = [];
  23. /**
  24. * @param string $idPrefix 这段内容生成的 HTML id 的前缀(边注的 checkbox / label):同一页上多段内容
  25. * 各自从 1 编号,没有前缀 id 会重复,点角标会切换到别处的边注
  26. */
  27. public function __construct(
  28. public readonly RenderOptions $options,
  29. public readonly Resolver $resolver,
  30. public readonly Renderer $renderer,
  31. public readonly string $idPrefix = '',
  32. ) {}
  33. /**
  34. * 本段内容里唯一、跨段也不重复的 HTML id。
  35. */
  36. public function htmlId(string $kind, int $number): string
  37. {
  38. return $this->idPrefix === '' ? "{$kind}-{$number}" : "{$kind}-{$this->idPrefix}-{$number}";
  39. }
  40. public function format(): Format
  41. {
  42. return $this->options->format;
  43. }
  44. /**
  45. * @template T of Loader
  46. *
  47. * @param class-string<T> $class
  48. * @return T
  49. */
  50. public function loader(string $class): Loader
  51. {
  52. return $this->resolver->loader($class);
  53. }
  54. /**
  55. * 按当前格式渲染一段内容(模板 props 里的 Document、slot、句子正文都走这里)。
  56. *
  57. * @return list<array<string, mixed>>|string
  58. */
  59. public function render(Document $document): array|string
  60. {
  61. return $this->renderer->document($document, $this);
  62. }
  63. /**
  64. * 按当前格式渲染,要求结果是字符串(html / 纯文本 / markdown 模板方法用)。
  65. */
  66. public function renderString(Document $document): string
  67. {
  68. $rendered = $this->render($document);
  69. return is_string($rendered) ? $rendered : '';
  70. }
  71. /**
  72. * 这段内容的语言:第一个 channel 的语言,没有 channel 用选项里的默认语言(v2 口径)。
  73. */
  74. public function lang(Scope $scope): string
  75. {
  76. $lang = $this->resolver->channels()->get($scope->firstChannel())?->lang;
  77. return $lang !== null && $lang !== '' ? (string) $lang : $this->options->lang;
  78. }
  79. public function nextNoteNumber(): int
  80. {
  81. return ++$this->noteNumber;
  82. }
  83. }