TermTemplate.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\Ir\Text;
  7. use App\Services\Markup\Render\RenderContext;
  8. use App\Services\Markup\Resolve\Resolver;
  9. use App\Services\Markup\Resolve\TermLoader;
  10. /**
  11. * 术语:`{{term|word}}`、`[[word]]`。
  12. *
  13. * `word@tag` 按 tag 查;`word#显示文字` 用指定文字代替译名。查询口径见 {@see TermLoader}。
  14. */
  15. final class TermTemplate extends Template
  16. {
  17. protected string $component = 'term';
  18. public function params(): array
  19. {
  20. return ['word' => ParamSpec::at(1)];
  21. }
  22. /**
  23. * @return array{word: string, tag: string, display: ?string}
  24. */
  25. private function word(Call $call): array
  26. {
  27. $word = (string) $call->param('word');
  28. $display = null;
  29. if (str_contains($word, '#')) {
  30. [$word, $display] = explode('#', $word, 2);
  31. }
  32. $tag = '';
  33. if (str_contains($word, '@')) {
  34. [$word, $tag] = explode('@', $word, 2);
  35. }
  36. return ['word' => $word, 'tag' => $tag, 'display' => $display];
  37. }
  38. public function collect(Call $call, Resolver $resolver): void
  39. {
  40. ['word' => $word, 'tag' => $tag] = $this->word($call);
  41. $call->data['term'] = $resolver->loader(TermLoader::class)
  42. ->request($word, $tag, $call->scope->firstChannel(), $call->scope);
  43. }
  44. /**
  45. * @return array<string, mixed>
  46. */
  47. public function props(Call $call, RenderContext $ctx): array
  48. {
  49. ['word' => $word, 'display' => $display] = $this->word($call);
  50. $props = self::termProps($ctx, $call->data['term'] ?? null, $word);
  51. if ($display !== null) {
  52. $props['meaning'] = $display;
  53. }
  54. return $props;
  55. }
  56. /**
  57. * 术语查询结果 → props(ql、g 模板也用)。字段沿用 v2 的 getTermProps();note 是内容节点而不是 HTML。
  58. *
  59. * @return array<string, mixed>
  60. */
  61. public static function termProps(RenderContext $ctx, ?string $key, string $word): array
  62. {
  63. $result = $ctx->loader(TermLoader::class)->result((string) $key);
  64. $props = [
  65. 'word' => $word,
  66. 'parentChannelId' => $result['channel'],
  67. 'parentStudioId' => $result['studio'],
  68. ];
  69. $term = $result['term'];
  70. if ($term !== null) {
  71. $props['id'] = (string) $term->guid;
  72. $props['meaning'] = (string) $term->meaning;
  73. $props['channel'] = $term->channal;
  74. if (! empty($term->other_meaning)) {
  75. $props['meaning2'] = (string) $term->other_meaning;
  76. }
  77. if ($result['community']) {
  78. $props['isCommunity'] = true;
  79. }
  80. // tree 输出不带 note:前端的术语组件打开弹窗时自己取词条详情,note 全文随正文下发是浪费
  81. // (一个词条的 note 动辄几十 KB)。markdown 导出用 note 做脚注(见 markdown())
  82. if ($result['note'] !== null && $ctx->format() !== Format::Tree) {
  83. $props['note'] = $result['note'];
  84. }
  85. }
  86. return $props;
  87. }
  88. public function children(Call $call, RenderContext $ctx): Document
  89. {
  90. return new Document([new Text($this->label($call, $ctx))], true, '', [], $call->scope);
  91. }
  92. /**
  93. * 正文里显示的文字:找到术语时「译名(原词)」,否则原词。
  94. */
  95. private function label(Call $call, RenderContext $ctx): string
  96. {
  97. $props = $this->props($call, $ctx);
  98. return isset($props['meaning']) ? "{$props['meaning']}({$props['word']})" : $props['word'];
  99. }
  100. public function text(Call $call, RenderContext $ctx): string
  101. {
  102. $props = $this->props($call, $ctx);
  103. return $props['meaning'] ?? $props['word'];
  104. }
  105. public function html(Call $call, RenderContext $ctx): string
  106. {
  107. $props = $this->props($call, $ctx);
  108. $class = isset($props['id']) ? 'term-ref' : 'term-ref term_invalid';
  109. return '<span class="'.$class.'" data-id="'.e($props['id'] ?? '').'" data-term="'.e($props['word']).'">'
  110. .e($props['meaning'] ?? $props['word']).'</span>';
  111. }
  112. /**
  113. * 第一次出现写「译名(原词)」并把术语 note 作为脚注,之后只写译名(v2 口径)。
  114. */
  115. public function markdown(Call $call, RenderContext $ctx): string
  116. {
  117. $props = $this->props($call, $ctx);
  118. if (! isset($props['meaning'])) {
  119. return $props['word'];
  120. }
  121. $seen = $ctx->termSeen[$props['word']] ?? 0;
  122. $ctx->termSeen[$props['word']] = $seen + 1;
  123. if ($seen > 0) {
  124. return $props['meaning'];
  125. }
  126. $output = "{$props['meaning']}({$props['word']})";
  127. if (($props['note'] ?? null) instanceof Document) {
  128. $sn = $ctx->nextNoteNumber();
  129. $ctx->footnotes[] = ['sn' => $sn, 'trigger' => '', 'content' => $ctx->renderString($props['note'])];
  130. $output .= "[^{$sn}]";
  131. }
  132. return $output;
  133. }
  134. }