ArticleTemplate.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Services\Markup\Ir\Call;
  4. use App\Services\Markup\Render\RenderContext;
  5. use App\Services\Markup\Templates\Concerns\CitesParagraph;
  6. use App\Services\Markup\Templates\Concerns\EmbedsArticle;
  7. /**
  8. * 嵌入文章 / 文集 / 章节 / 段落:`{{article|type|id|title|channel|style|book|paragraphs|anthology}}`。
  9. *
  10. * 章节另有独立的 {@see ChapterTemplate}(`{{chapter}}`);这里的 `type=chapter` 是旧写法,
  11. * 没给 id 时由 book + paragraphs 拼出 id,拼法与它共用。
  12. */
  13. final class ArticleTemplate extends Template
  14. {
  15. use CitesParagraph;
  16. use EmbedsArticle;
  17. protected string $component = 'article';
  18. public function params(): array
  19. {
  20. return [
  21. 'type' => ParamSpec::at(1),
  22. 'id' => ParamSpec::at(2),
  23. 'title' => ParamSpec::at(3),
  24. 'channel' => ParamSpec::at(4),
  25. 'style' => ParamSpec::at(5),
  26. 'book' => ParamSpec::at(6),
  27. 'paragraphs' => ParamSpec::at(7),
  28. 'anthology' => ParamSpec::at(8),
  29. ];
  30. }
  31. /**
  32. * @return array<string, mixed>
  33. */
  34. public function props(Call $call, RenderContext $ctx): array
  35. {
  36. $type = (string) $call->param('type');
  37. $id = $type === 'chapter' ? $this->paragraphId($call) : (string) $call->param('id');
  38. return $this->embedProps($call, $type, $id, ['style', 'channel', 'title', 'anthology', 'book', 'paragraphs']);
  39. }
  40. /**
  41. * 章节 / 段落(type=chapter|para)引用了经文段落;文章、文集不算。
  42. */
  43. public function references(Call $call): array
  44. {
  45. return $this->citesParagraph($call) ? self::paragraphRef($this->paragraphId($call)) : [];
  46. }
  47. /**
  48. * 章节 / 段落:`style=reference` 且没给标题时显示缩写 + 段号,与 {{chapter}}、{{para}} 相同。
  49. */
  50. protected function title(Call $call): string
  51. {
  52. return $this->citesParagraph($call)
  53. ? $this->citedTitle($call, $this->paragraphId($call))
  54. : (string) $call->param('title');
  55. }
  56. private function citesParagraph(Call $call): bool
  57. {
  58. return in_array((string) $call->param('type'), ['chapter', 'para'], true);
  59. }
  60. private function paragraphId(Call $call): string
  61. {
  62. return ChapterTemplate::resolveId(
  63. (string) $call->param('id'),
  64. (string) $call->param('book'),
  65. (string) $call->param('paragraphs'),
  66. );
  67. }
  68. }