SentTemplate.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Services\Markup\Format;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Ir\Document;
  6. use App\Services\Markup\Render\RenderContext;
  7. use App\Services\Markup\Resolve\Resolver;
  8. use App\Services\Markup\Resolve\SentenceLoader;
  9. /**
  10. * 句子:`{{sent|id=65-464-2-27}}`、简写 `{{65-464-2-27}}`、指定 channel `{{65-464-2-27@channel}}`。
  11. *
  12. * 参数:id、channel(逗号分隔,缺省用内容所属 channel)、text(both / origin / translation)、mode(read / edit)。
  13. * channel 与 text 共用第 2 个位置(v2 如此)。
  14. */
  15. final class SentTemplate extends Template
  16. {
  17. public function params(): array
  18. {
  19. return [
  20. 'id' => ParamSpec::at(1),
  21. 'channel' => ParamSpec::at(2),
  22. 'text' => ParamSpec::at(2, 'both'),
  23. 'mode' => ParamSpec::at(3),
  24. ];
  25. }
  26. /**
  27. * @return array{0: string, 1: list<string>} [句子坐标, channel 列表]
  28. */
  29. private function target(Call $call): array
  30. {
  31. $id = (string) $call->param('id');
  32. $channel = (string) $call->param('channel');
  33. $channels = $channel !== '' && ! in_array($channel, ['both', 'origin', 'translation'], true)
  34. ? array_values(array_filter(array_map('trim', explode(',', $channel))))
  35. : $call->scope->channels;
  36. if (str_contains($id, '@')) {
  37. [$id, $only] = explode('@', $id, 2);
  38. $channels = [trim($only)];
  39. }
  40. return [trim($id), $channels];
  41. }
  42. private function mode(Call $call, string $default): string
  43. {
  44. $mode = (string) $call->param('mode');
  45. return $mode !== '' ? $mode : $default;
  46. }
  47. private function show(Call $call): string
  48. {
  49. $show = (string) $call->param('text', 'both');
  50. return in_array($show, ['origin', 'translation'], true) ? $show : 'both';
  51. }
  52. public function collect(Call $call, Resolver $resolver): void
  53. {
  54. [$id, $channels] = $this->target($call);
  55. $call->data['sentence'] = $resolver->loader(SentenceLoader::class)
  56. ->request($id, $channels, $this->mode($call, $resolver->options->mode), $call->scope);
  57. }
  58. /**
  59. * @return array<string, mixed>|null
  60. */
  61. private function sentence(Call $call, RenderContext $ctx): ?array
  62. {
  63. $sentence = $ctx->loader(SentenceLoader::class)->result((string) ($call->data['sentence'] ?? ''));
  64. if ($sentence !== null) {
  65. // 以调用为键:同一个调用在一次渲染里可能被取多次(props、纯文本…),只记一次
  66. $ctx->sentenceRefs[spl_object_id($call)] = [$sentence['book'], $sentence['para']];
  67. }
  68. return $sentence;
  69. }
  70. public function component(Call $call, RenderContext $ctx): string
  71. {
  72. return $this->mode($call, $ctx->options->mode) === 'read' ? 'sentread' : 'sentedit';
  73. }
  74. /**
  75. * `{id, show, origin, translation, commentaries?}`。id 是句子坐标 book-para-start-end;
  76. * 每条:`{id, channel{id,name,lang}, body}`,body 是渲染后的正文;编辑模式另给 content(源码)与 contentType。
  77. * 原文 / 译文 / 义注已经分组,所以不再给 channel.type;没有义注时不给 commentaries。
  78. *
  79. * @return array<string, mixed>
  80. */
  81. public function props(Call $call, RenderContext $ctx): array
  82. {
  83. [$id] = $this->target($call);
  84. $edit = $this->mode($call, $ctx->options->mode) !== 'read';
  85. $sentence = $this->sentence($call, $ctx);
  86. if ($sentence === null) {
  87. return ['id' => $id, 'show' => $this->show($call), 'error' => 'invalid sentence id'];
  88. }
  89. $props = ['id' => $id, 'show' => $this->show($call)];
  90. foreach (['origin', 'translation', 'commentaries'] as $area) {
  91. if ($area === 'commentaries' && $sentence[$area] === []) {
  92. continue;
  93. }
  94. $props[$area] = array_map(function (array $entry) use ($edit) {
  95. $item = [
  96. 'id' => $entry['id'],
  97. 'channel' => [
  98. 'id' => $entry['channel']['id'],
  99. 'name' => $entry['channel']['name'],
  100. 'lang' => $entry['channel']['lang'],
  101. ],
  102. 'body' => $entry['body'],
  103. ];
  104. if ($edit) {
  105. $item['content'] = $entry['content'];
  106. $item['contentType'] = $entry['contentType'];
  107. }
  108. return $item;
  109. }, $sentence[$area]);
  110. }
  111. return $props;
  112. }
  113. /**
  114. * 各条正文按当前格式渲染后拼接。
  115. *
  116. * @param list<array<string, mixed>> $entries
  117. */
  118. private function join(array $entries, RenderContext $ctx, bool $trim = false, string $wrapClass = ''): string
  119. {
  120. $out = '';
  121. foreach ($entries as $entry) {
  122. $body = $entry['body'] ?? null;
  123. $text = $body instanceof Document ? $ctx->renderString($body) : '';
  124. if ($trim) {
  125. $text = trim($text);
  126. }
  127. $out .= $wrapClass === '' ? $text : '<span class="'.$wrapClass.'">'.$text.'</span>';
  128. }
  129. return $out;
  130. }
  131. public function text(Call $call, RenderContext $ctx): string
  132. {
  133. return $this->plain($call, $ctx, Format::Text);
  134. }
  135. public function plain(Call $call, RenderContext $ctx, Format $format): string
  136. {
  137. $sentence = $this->sentence($call, $ctx);
  138. if ($sentence === null) {
  139. return '';
  140. }
  141. $show = $this->show($call);
  142. $trim = $format === Format::Simple;
  143. $out = '';
  144. if ($show !== 'translation') {
  145. $out .= $this->join($sentence['origin'], $ctx, $trim);
  146. }
  147. if ($show !== 'origin') {
  148. $out .= $this->join($sentence['translation'], $ctx, $trim);
  149. }
  150. return $out;
  151. }
  152. public function html(Call $call, RenderContext $ctx): string
  153. {
  154. $sentence = $this->sentence($call, $ctx);
  155. if ($sentence === null) {
  156. return '';
  157. }
  158. $show = $this->show($call);
  159. $html = '<span class="sentence">';
  160. if ($show !== 'translation') {
  161. $html .= $this->join($sentence['origin'], $ctx, false, 'origin');
  162. }
  163. if ($show !== 'origin') {
  164. $html .= $this->join($sentence['translation'], $ctx, false, 'translation');
  165. }
  166. return $html.'</span>';
  167. }
  168. /**
  169. * 导出:按 origin / translation 选项取原文、译文;只要译文却没有译文时用原文代替(v2 口径)。
  170. */
  171. public function markdown(Call $call, RenderContext $ctx): string
  172. {
  173. $sentence = $this->sentence($call, $ctx);
  174. if ($sentence === null) {
  175. return '';
  176. }
  177. $show = $this->show($call);
  178. $out = '';
  179. if ($show !== 'translation' && $ctx->options->origin) {
  180. $out .= $this->join($sentence['origin'], $ctx, true);
  181. }
  182. if ($show !== 'origin' && $ctx->options->translation) {
  183. if ($sentence['translation'] !== []) {
  184. $out .= $this->join($sentence['translation'], $ctx, true);
  185. } elseif ($show === 'translation') {
  186. $out .= $this->join($sentence['origin'], $ctx, true);
  187. }
  188. }
  189. return $out;
  190. }
  191. }