ReferenceTemplate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  10. * 参考文献:`{{ref}}`,列出本文引用到的巴利文献(按书名去重,按首次出现的顺序编号)。
  11. *
  12. * 引用来自整篇内容里各模板的 {@see Template::references()}:句子、para、quote、
  13. * 带 book + para 的 ql、章节 / 段落 article。遍历整篇内容而不是边渲染边记,所以 `{{ref}}` 写在哪里都能列全
  14. * (v2 只统计句子模板,且必须放在文末)。
  15. *
  16. * 一段引用对应的书名:`book_titles` 里同一本书、起始段号 ≤ 该段的最近一条(一个 book 里可能收了几部作品;
  17. * v2 把排在前面的作品全列了进来,且用 < 漏掉了恰好引用作品首段的情况)。
  18. */
  19. final class ReferenceTemplate extends Template
  20. {
  21. protected string $component = 'reference';
  22. private const COPYRIGHT = 'CSCD V4 VRI 2008';
  23. /**
  24. * @return list<array{sn: int, title: string, copyright: string}>
  25. */
  26. private function bibliography(RenderContext $ctx): array
  27. {
  28. $refs = $ctx->document === null ? [] : $this->gather($ctx->document->nodes);
  29. if ($refs === []) {
  30. return [];
  31. }
  32. $books = array_values(array_unique(array_map(fn (array $ref) => $ref[0], $refs)));
  33. $titles = BookTitle::whereIn('book', $books)->orderByDesc('paragraph')->get(['book', 'paragraph', 'title']);
  34. $seen = [];
  35. $references = [];
  36. foreach ($refs as [$book, $para]) {
  37. $title = $titles->first(fn (BookTitle $row) => (int) $row->book === $book && (int) $row->paragraph <= $para);
  38. if ($title === null || isset($seen[$title->title])) {
  39. continue;
  40. }
  41. $seen[$title->title] = true;
  42. $references[] = ['sn' => count($references) + 1, 'title' => (string) $title->title, 'copyright' => self::COPYRIGHT];
  43. }
  44. return $references;
  45. }
  46. /**
  47. * 按文档顺序收集引用的 [book, para](含模板参数里的内容,如脚注正文)。
  48. *
  49. * @param list<Node> $nodes
  50. * @return list<array{0: int, 1: int}>
  51. */
  52. private function gather(array $nodes): array
  53. {
  54. $refs = [];
  55. foreach ($nodes as $node) {
  56. if ($node instanceof Element) {
  57. array_push($refs, ...$this->gather($node->children));
  58. } elseif ($node instanceof Call) {
  59. array_push($refs, ...$node->template->references($node));
  60. foreach ($node->slots as $slot) {
  61. array_push($refs, ...$this->gather($slot instanceof Document ? $slot->nodes : []));
  62. }
  63. }
  64. }
  65. return $refs;
  66. }
  67. /**
  68. * @return array<string, mixed>
  69. */
  70. public function props(Call $call, RenderContext $ctx): array
  71. {
  72. return ['pali' => $this->bibliography($ctx)];
  73. }
  74. public function text(Call $call, RenderContext $ctx): string
  75. {
  76. $text = '';
  77. foreach ($this->bibliography($ctx) as $reference) {
  78. $text .= '['.$reference['sn'].'] '.ucfirst($reference['title']).' '.$reference['copyright']."\n";
  79. }
  80. return $text;
  81. }
  82. public function markdown(Call $call, RenderContext $ctx): string
  83. {
  84. $markdown = '';
  85. foreach ($this->bibliography($ctx) as $reference) {
  86. $markdown .= '['.$reference['sn'].'] **'.ucfirst($reference['title']).'** '.$reference['copyright']."\n\n";
  87. }
  88. return $markdown;
  89. }
  90. }