MarkupService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Services\Markup;
  3. use App\Services\Markup\Annotation\AnnotationSet;
  4. use App\Services\Markup\Ir\Document;
  5. use App\Services\Markup\Ir\Scope;
  6. use App\Services\Markup\Parsing\MarkupParser;
  7. use App\Services\Markup\Render\HtmlRenderer;
  8. use App\Services\Markup\Render\MarkdownRenderer;
  9. use App\Services\Markup\Render\RenderContext;
  10. use App\Services\Markup\Render\Renderer;
  11. use App\Services\Markup\Render\TextRenderer;
  12. use App\Services\Markup\Render\TreeRenderer;
  13. use App\Services\Markup\Resolve\Resolver;
  14. /**
  15. * wikipali 正文(markdown + 类维基模板)的解析与渲染。取代 v2 的 `App\Http\Api\MdRender` + `TemplateRender`
  16. * (v2 继续用旧代码,不改)。
  17. *
  18. * 三段式:解析成 IR({@see MarkupParser},纯函数)→ 批量取数({@see Resolver})→ 按格式输出({@see Renderer})。
  19. * 一次渲染多段内容用 {@see renderMany()},所有段落里的术语、句子合在一起查。
  20. *
  21. * ```php
  22. * $tree = $markup->render($sentence->content, new RenderOptions(
  23. * format: Format::Tree,
  24. * channels: [$sentence->channel_uid],
  25. * contentType: $sentence->content_type,
  26. * ));
  27. * ```
  28. */
  29. final class MarkupService
  30. {
  31. public function __construct(private readonly MarkupParser $parser) {}
  32. /**
  33. * 只解析不取数(测试、静态分析用)。
  34. */
  35. public function parse(string $source, RenderOptions $options): Document
  36. {
  37. return $this->parser->parse($source, $this->scope($options), $options->paragraph);
  38. }
  39. /**
  40. * @return list<array<string, mixed>>|string tree 格式返回节点数组,其余返回字符串
  41. */
  42. public function render(?string $source, RenderOptions $options): array|string
  43. {
  44. return $this->renderMany(['content' => $source], $options)['content'];
  45. }
  46. /**
  47. * 批量渲染。每项可以是源码字符串,或带自己 channel / content_type 的数组(如一章里各句来自不同 channel)。
  48. *
  49. * @template K of array-key
  50. *
  51. * @param array<K, string|null|array{source: ?string, channels?: list<string>, contentType?: string, channelType?: string, annotations?: AnnotationSet, idPrefix?: string}> $items
  52. * @return array<K, list<array<string, mixed>>|string>
  53. */
  54. public function renderMany(array $items, RenderOptions $options): array
  55. {
  56. return $this->renderManyAs($items, $options, [$options->format])[$options->format->value];
  57. }
  58. /**
  59. * 同一批内容按多种格式输出(如 discussions 同时要节点树与纯文本摘要):解析与取数只做一次。
  60. *
  61. * @template K of array-key
  62. *
  63. * @param array<K, string|null|array{source: ?string, channels?: list<string>, contentType?: string, channelType?: string, annotations?: AnnotationSet, idPrefix?: string}> $items
  64. * @param list<Format> $formats
  65. * @return array<string, array<K, list<array<string, mixed>>|string>> 格式名 => 各项结果
  66. */
  67. public function renderManyAs(array $items, RenderOptions $options, array $formats): array
  68. {
  69. $resolver = new Resolver($options, $this->parser);
  70. $documents = [];
  71. $idPrefixes = [];
  72. foreach ($items as $key => $item) {
  73. $itemOptions = is_array($item) ? $options->with(array_intersect_key($item, array_flip(['channels', 'contentType', 'channelType']))) : $options;
  74. $source = is_array($item) ? $item['source'] : $item;
  75. $annotations = is_array($item) ? ($item['annotations'] ?? null) : null;
  76. $idPrefixes[$key] = is_array($item) && isset($item['idPrefix']) ? (string) $item['idPrefix'] : null;
  77. $document = $this->parser->parse((string) $source, $this->scope($itemOptions), $options->paragraph, 0, $annotations);
  78. $resolver->collect($document);
  79. $documents[$key] = $document;
  80. }
  81. $resolver->run();
  82. $output = [];
  83. foreach ($formats as $format) {
  84. $output[$format->value] = $this->output($documents, $options->with(['format' => $format]), $resolver, $idPrefixes);
  85. }
  86. return $output;
  87. }
  88. /**
  89. * @template K of array-key
  90. *
  91. * @param array<K, Document> $documents
  92. * @param array<K, string|null> $idPrefixes 各项 HTML id 的前缀;没给的取内容的短哈希
  93. * @return array<K, list<array<string, mixed>>|string>
  94. */
  95. private function output(array $documents, RenderOptions $options, Resolver $resolver, array $idPrefixes): array
  96. {
  97. $renderer = $this->renderer($options->format);
  98. $output = [];
  99. foreach ($documents as $key => $document) {
  100. $idPrefix = $idPrefixes[$key] ?? substr(md5($key.'|'.$document->source), 0, 8);
  101. $ctx = new RenderContext($options, $resolver, $renderer, $idPrefix);
  102. $rendered = $renderer->document($document, $ctx);
  103. if (is_string($rendered) && $options->footnote) {
  104. $rendered .= match (true) {
  105. $renderer instanceof HtmlRenderer => $renderer->footnotes($ctx),
  106. $renderer instanceof MarkdownRenderer => $renderer->footnotes($ctx),
  107. default => '',
  108. };
  109. }
  110. $output[$key] = $rendered;
  111. }
  112. return $output;
  113. }
  114. private function scope(RenderOptions $options): Scope
  115. {
  116. return new Scope(
  117. channels: array_values($options->channels),
  118. dialect: $options->dialect(),
  119. channelType: $options->channelType,
  120. contentType: $options->contentType,
  121. );
  122. }
  123. private function renderer(Format $format): Renderer
  124. {
  125. return match ($format) {
  126. Format::Tree => new TreeRenderer,
  127. Format::Html => new HtmlRenderer,
  128. Format::Markdown => new MarkdownRenderer,
  129. Format::Text, Format::Simple, Format::Prompt => new TextRenderer($format),
  130. };
  131. }
  132. }