new ParamSpec]; } private function title(Call $call, RenderContext $ctx): string { return $this->blockTitle($call, $ctx, 'labels.references'); } private const COPYRIGHT = 'CSCD V4 VRI 2008'; /** * @return list */ private function bibliography(RenderContext $ctx): array { $refs = $ctx->document === null ? [] : $this->gather($ctx->document->nodes); if ($refs === []) { return []; } $books = array_values(array_unique(array_map(fn (array $ref) => $ref[0], $refs))); $titles = BookTitle::whereIn('book', $books)->orderByDesc('paragraph')->get(['book', 'paragraph', 'title']); $seen = []; $references = []; foreach ($refs as [$book, $para]) { $title = $titles->first(fn (BookTitle $row) => (int) $row->book === $book && (int) $row->paragraph <= $para); if ($title === null || isset($seen[$title->title])) { continue; } $seen[$title->title] = true; $references[] = ['sn' => count($references) + 1, 'title' => (string) $title->title, 'copyright' => self::COPYRIGHT]; } return $references; } /** * 按文档顺序收集引用的 [book, para](含模板参数里的内容,如脚注正文)。 * * @param list $nodes * @return list */ private function gather(array $nodes): array { $refs = []; foreach ($nodes as $node) { if ($node instanceof Element) { array_push($refs, ...$this->gather($node->children)); } elseif ($node instanceof Call) { array_push($refs, ...$node->template->references($node)); foreach ($node->slots as $slot) { array_push($refs, ...$this->gather($slot instanceof Document ? $slot->nodes : [])); } } } return $refs; } /** * @return array */ public function props(Call $call, RenderContext $ctx): array { $references = $this->bibliography($ctx); $title = $references === [] ? '' : $this->title($call, $ctx); return ($title === '' ? [] : ['title' => $title]) + ['pali' => $references]; } public function text(Call $call, RenderContext $ctx): string { $references = $this->bibliography($ctx); if ($references === []) { return ''; } $title = $this->title($call, $ctx); $text = $title === '' ? '' : $title."\n"; foreach ($references as $reference) { $text .= '['.$reference['sn'].'] '.ucfirst($reference['title']).' '.$reference['copyright']."\n"; } return $text; } /** * 一条一行(与 v6 的 ReferenceCtl 一致)。模板落在 `

` 里,不能再套块级元素,用 `
` 分行; * 没有这个方法时走基类的「转义后的纯文本」,条目间的换行会被浏览器合并成空格。 */ public function html(Call $call, RenderContext $ctx): string { $references = $this->bibliography($ctx); if ($references === []) { return ''; } $title = $this->title($call, $ctx); $items = $title === '' ? [] : [''.e($title).'']; foreach ($references as $reference) { $items[] = '' .e('['.$reference['sn'].'] '.ucfirst($reference['title']).' '.$reference['copyright']) .''; } return implode('
', $items); } public function markdown(Call $call, RenderContext $ctx): string { $references = $this->bibliography($ctx); if ($references === []) { return ''; } $title = $this->title($call, $ctx); $markdown = $title === '' ? '' : $title."\n\n"; foreach ($references as $reference) { $markdown .= '['.$reference['sn'].'] **'.ucfirst($reference['title']).'** '.$reference['copyright']."\n\n"; } return $markdown; } }