ReferenceTemplate.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Models\BookTitle;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Ir\Document;
  6. use App\Services\Markup\Ir\Element;
  7. use App\Services\Markup\Ir\Node;
  8. use App\Services\Markup\Render\RenderContext;
  9. use App\Services\Markup\Templates\Concerns\OwnsBlockTitle;
  10. /**
  11. * 参考文献:`{{ref}}`,列出本文引用到的巴利文献(按书名去重,按首次出现的顺序编号)。
  12. *
  13. * 引用来自整篇内容里各模板的 {@see Template::references()}:句子、para、quote、
  14. * 带 book + para 的 ql、章节 / 段落 article。遍历整篇内容而不是边渲染边记,所以 `{{ref}}` 写在哪里都能列全
  15. * (v2 只统计句子模板,且必须放在文末)。
  16. *
  17. * 标题:自带一行标题,规则见 {@see OwnsBlockTitle}(labels.references;`title=` 改文字或隐藏;
  18. * 紧跟在手写的「## 参考文献」后面时不显示)。没有任何引用时整块都不输出。
  19. *
  20. * 一段引用对应的书名:`book_titles` 里同一本书、起始段号 ≤ 该段的最近一条(一个 book 里可能收了几部作品;
  21. * v2 把排在前面的作品全列了进来,且用 < 漏掉了恰好引用作品首段的情况)。
  22. */
  23. final class ReferenceTemplate extends Template
  24. {
  25. use OwnsBlockTitle;
  26. protected string $component = 'reference';
  27. public function params(): array
  28. {
  29. return ['title' => new ParamSpec];
  30. }
  31. private function title(Call $call, RenderContext $ctx): string
  32. {
  33. return $this->blockTitle($call, $ctx, 'labels.references');
  34. }
  35. private const COPYRIGHT = 'CSCD V4 VRI 2008';
  36. /**
  37. * @return list<array{sn: int, title: string, copyright: string}>
  38. */
  39. private function bibliography(RenderContext $ctx): array
  40. {
  41. $refs = $ctx->document === null ? [] : $this->gather($ctx->document->nodes);
  42. if ($refs === []) {
  43. return [];
  44. }
  45. $books = array_values(array_unique(array_map(fn (array $ref) => $ref[0], $refs)));
  46. $titles = BookTitle::whereIn('book', $books)->orderByDesc('paragraph')->get(['book', 'paragraph', 'title']);
  47. $seen = [];
  48. $references = [];
  49. foreach ($refs as [$book, $para]) {
  50. $title = $titles->first(fn (BookTitle $row) => (int) $row->book === $book && (int) $row->paragraph <= $para);
  51. if ($title === null || isset($seen[$title->title])) {
  52. continue;
  53. }
  54. $seen[$title->title] = true;
  55. $references[] = ['sn' => count($references) + 1, 'title' => (string) $title->title, 'copyright' => self::COPYRIGHT];
  56. }
  57. return $references;
  58. }
  59. /**
  60. * 按文档顺序收集引用的 [book, para](含模板参数里的内容,如脚注正文)。
  61. *
  62. * @param list<Node> $nodes
  63. * @return list<array{0: int, 1: int}>
  64. */
  65. private function gather(array $nodes): array
  66. {
  67. $refs = [];
  68. foreach ($nodes as $node) {
  69. if ($node instanceof Element) {
  70. array_push($refs, ...$this->gather($node->children));
  71. } elseif ($node instanceof Call) {
  72. array_push($refs, ...$node->template->references($node));
  73. foreach ($node->slots as $slot) {
  74. array_push($refs, ...$this->gather($slot instanceof Document ? $slot->nodes : []));
  75. }
  76. }
  77. }
  78. return $refs;
  79. }
  80. /**
  81. * @return array<string, mixed>
  82. */
  83. public function props(Call $call, RenderContext $ctx): array
  84. {
  85. $references = $this->bibliography($ctx);
  86. $title = $references === [] ? '' : $this->title($call, $ctx);
  87. return ($title === '' ? [] : ['title' => $title]) + ['pali' => $references];
  88. }
  89. public function text(Call $call, RenderContext $ctx): string
  90. {
  91. $references = $this->bibliography($ctx);
  92. if ($references === []) {
  93. return '';
  94. }
  95. $title = $this->title($call, $ctx);
  96. $text = $title === '' ? '' : $title."\n";
  97. foreach ($references as $reference) {
  98. $text .= '['.$reference['sn'].'] '.ucfirst($reference['title']).' '.$reference['copyright']."\n";
  99. }
  100. return $text;
  101. }
  102. /**
  103. * 一条一行(与 v6 的 ReferenceCtl 一致)。模板落在 `<p>` 里,不能再套块级元素,用 `<br />` 分行;
  104. * 没有这个方法时走基类的「转义后的纯文本」,条目间的换行会被浏览器合并成空格。
  105. */
  106. public function html(Call $call, RenderContext $ctx): string
  107. {
  108. $references = $this->bibliography($ctx);
  109. if ($references === []) {
  110. return '';
  111. }
  112. $title = $this->title($call, $ctx);
  113. $items = $title === '' ? [] : ['<span class="tpl-reference-title">'.e($title).'</span>'];
  114. foreach ($references as $reference) {
  115. $items[] = '<span class="tpl-reference-item">'
  116. .e('['.$reference['sn'].'] '.ucfirst($reference['title']).' '.$reference['copyright'])
  117. .'</span>';
  118. }
  119. return implode('<br />', $items);
  120. }
  121. public function markdown(Call $call, RenderContext $ctx): string
  122. {
  123. $references = $this->bibliography($ctx);
  124. if ($references === []) {
  125. return '';
  126. }
  127. $title = $this->title($call, $ctx);
  128. $markdown = $title === '' ? '' : $title."\n\n";
  129. foreach ($references as $reference) {
  130. $markdown .= '['.$reference['sn'].'] **'.ucfirst($reference['title']).'** '.$reference['copyright']."\n\n";
  131. }
  132. return $markdown;
  133. }
  134. }