Просмотр исходного кода

✨ (markup): 章节模板 {{chapter}},与 {{article|type=chapter}} 等价

- ChapterTemplate:{{chapter|id|title|channel|style}},没给 id 时用 book= + para=;
  前端仍是 article 组件(type=chapter),v6 / library / mobile 不用改
- 共用部分抽成 Concerns\EmbedsArticle(props 拼法、纯文本只留标题、html 为空),
  ArticleTemplate 的 type=chapter 拼 id 改调 ChapterTemplate::resolveId()
- 模板生成器加 chapter 条目与三份文案
- 只在新渲染器(App\Services\Markup)里有,v2 TemplateRender 不认识

顺带:ArticleTemplate 里 props() 与 references() 各写一份、口径不一的拼 id 合成一处;
既没 id 也没 book 时 id 由 "0-0" 变为空串

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 9 часов назад
Родитель
Сommit
4d72e7773c

+ 17 - 39
api-v13/app/Services/Markup/Templates/ArticleTemplate.php

@@ -2,16 +2,20 @@
 
 namespace App\Services\Markup\Templates;
 
-use App\Services\Markup\Format;
 use App\Services\Markup\Ir\Call;
 use App\Services\Markup\Render\RenderContext;
+use App\Services\Markup\Templates\Concerns\EmbedsArticle;
 
 /**
- * 嵌入文章 / 章节:`{{article|type|id|title|channel|style|book|paragraphs|anthology}}`。
- * `type=chapter` 且没给 id 时由 book + paragraphs 拼出 id。
+ * 嵌入文章 / 文集 / 章节 / 段落:`{{article|type|id|title|channel|style|book|paragraphs|anthology}}`。
+ *
+ * 章节另有独立的 {@see ChapterTemplate}(`{{chapter}}`);这里的 `type=chapter` 是旧写法,
+ * 没给 id 时由 book + paragraphs 拼出 id,拼法与它共用。
  */
 final class ArticleTemplate extends Template
 {
+    use EmbedsArticle;
+
     protected string $component = 'article';
 
     public function params(): array
@@ -34,29 +38,9 @@ final class ArticleTemplate extends Template
     public function props(Call $call, RenderContext $ctx): array
     {
         $type = (string) $call->param('type');
-        $id = (string) $call->param('id');
-        $book = (string) $call->param('book');
-        $paragraphs = (string) $call->param('paragraphs');
-        if ($type === 'chapter' && $id === '') {
-            $id = ((int) $book).'-'.((int) $paragraphs);
-        }
-        $props = ['type' => $type, 'id' => $id];
-        // style 为空时不给:前端组件用它自己的默认值(modal),传空串反而什么都不渲染
-        foreach (['style', 'channel', 'title', 'anthology'] as $name) {
-            $value = (string) $call->param($name);
-            if ($value !== '') {
-                $props[$name] = $value;
-            }
-        }
-        if ($book !== '') {
-            $props['book'] = $book;
-        }
-        if ($paragraphs !== '') {
-            $props['paragraphs'] = $paragraphs;
-        }
-        $props['parentChannels'] = $call->scope->channels;
+        $id = $type === 'chapter' ? $this->paragraphId($call) : (string) $call->param('id');
 
-        return $props;
+        return $this->embedProps($call, $type, $id, ['style', 'channel', 'title', 'anthology', 'book', 'paragraphs']);
     }
 
     /**
@@ -64,25 +48,19 @@ final class ArticleTemplate extends Template
      */
     public function references(Call $call): array
     {
-        $type = (string) $call->param('type');
-        if (! in_array($type, ['chapter', 'para'], true)) {
+        if (! in_array((string) $call->param('type'), ['chapter', 'para'], true)) {
             return [];
         }
-        $id = (string) $call->param('id');
-        if ($id === '') {
-            $id = $call->param('book').'-'.$call->param('paragraphs');
-        }
 
-        return self::paragraphRef($id);
-    }
-
-    public function plain(Call $call, RenderContext $ctx, Format $format): string
-    {
-        return $format === Format::Prompt ? '' : (string) $call->param('title');
+        return self::paragraphRef($this->paragraphId($call));
     }
 
-    public function html(Call $call, RenderContext $ctx): string
+    private function paragraphId(Call $call): string
     {
-        return '';
+        return ChapterTemplate::resolveId(
+            (string) $call->param('id'),
+            (string) $call->param('book'),
+            (string) $call->param('paragraphs'),
+        );
     }
 }

+ 62 - 0
api-v13/app/Services/Markup/Templates/ChapterTemplate.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace App\Services\Markup\Templates;
+
+use App\Services\Markup\Ir\Call;
+use App\Services\Markup\Render\RenderContext;
+use App\Services\Markup\Templates\Concerns\EmbedsArticle;
+
+/**
+ * 嵌入经文章节:`{{chapter|id=book-para|title|channel|style}}`,没给 id 时用 `book=` + `para=`。
+ *
+ * 与 `{{article|type=chapter|…}}` 等价(前端是同一个 `article` 组件),
+ * 后者拼 id 也走 {@see self::resolveId()}。
+ */
+final class ChapterTemplate extends Template
+{
+    use EmbedsArticle;
+
+    protected string $component = 'article';
+
+    public function params(): array
+    {
+        return [
+            'id' => ParamSpec::at(1),
+            'title' => ParamSpec::at(2),
+            'channel' => ParamSpec::at(3),
+            'style' => ParamSpec::at(4),
+            'book' => new ParamSpec,
+            'para' => new ParamSpec,
+        ];
+    }
+
+    /**
+     * 章节 id(`book-para`):给了 id 就用 id,否则由书号 + 段号拼出;书号也没给时为空。
+     */
+    public static function resolveId(string $id, string $book, string $para): string
+    {
+        if ($id !== '' || $book === '') {
+            return $id;
+        }
+
+        return ((int) $book).'-'.((int) $para);
+    }
+
+    /**
+     * @return array<string, mixed>
+     */
+    public function props(Call $call, RenderContext $ctx): array
+    {
+        return $this->embedProps($call, 'chapter', $this->id($call), ['style', 'channel', 'title']);
+    }
+
+    public function references(Call $call): array
+    {
+        return self::paragraphRef($this->id($call));
+    }
+
+    private function id(Call $call): string
+    {
+        return self::resolveId((string) $call->param('id'), (string) $call->param('book'), (string) $call->param('para'));
+    }
+}

+ 43 - 0
api-v13/app/Services/Markup/Templates/Concerns/EmbedsArticle.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace App\Services\Markup\Templates\Concerns;
+
+use App\Services\Markup\Format;
+use App\Services\Markup\Ir\Call;
+use App\Services\Markup\Render\RenderContext;
+
+/**
+ * 交给前端 `article` 组件嵌入的模板({{article}}、{{chapter}})共用的输出:
+ * props 的拼法、纯文本只留标题、html 不输出(只有前端组件能渲染)。
+ */
+trait EmbedsArticle
+{
+    /**
+     * @param  list<string>  $optional  有值才写进 props 的参数,按顺序
+     * @return array<string, mixed>
+     */
+    protected function embedProps(Call $call, string $type, string $id, array $optional): array
+    {
+        $props = ['type' => $type, 'id' => $id];
+        // style 为空时不给:前端组件用它自己的默认值(modal),传空串反而什么都不渲染
+        foreach ($optional as $name) {
+            $value = (string) $call->param($name);
+            if ($value !== '') {
+                $props[$name] = $value;
+            }
+        }
+        $props['parentChannels'] = $call->scope->channels;
+
+        return $props;
+    }
+
+    public function plain(Call $call, RenderContext $ctx, Format $format): string
+    {
+        return $format === Format::Prompt ? '' : (string) $call->param('title');
+    }
+
+    public function html(Call $call, RenderContext $ctx): string
+    {
+        return '';
+    }
+}

+ 1 - 0
api-v13/app/Services/Markup/Templates/TemplateRegistry.php

@@ -19,6 +19,7 @@ final class TemplateRegistry
         'ql' => QuoteLinkTemplate::class,
         'exercise' => ExerciseTemplate::class,
         'article' => ArticleTemplate::class,
+        'chapter' => ChapterTemplate::class,
         'nissaya' => NissayaTemplate::class,
         'mermaid' => MermaidTemplate::class,
         'qa' => QaTemplate::class,

+ 16 - 0
api-v13/tests/Feature/MarkupSyntaxTest.php

@@ -466,6 +466,22 @@ describe('templates without data', function () {
         expect(markupRender('{{article|type=article|id=x|title=T}}', Format::Prompt))->toBe('');
     });
 
+    it('renders chapter like article type=chapter', function () {
+        $legacy = markupTree('{{article|type=chapter|book=168|paragraphs=915|title=T}}')[0];
+        $chapter = markupTree('{{chapter|book=168|para=915|title=T}}')[0];
+        expect($chapter['name'])->toBe('article');
+        expect($chapter['props'])->toBe(['type' => 'chapter', 'id' => '168-915', 'title' => 'T', 'parentChannels' => []]);
+        expect($chapter['props'])->toBe(array_diff_key($legacy['props'], ['book' => 0, 'paragraphs' => 0]));
+
+        expect(markupTree('{{chapter|168-915|T|c1|card}}')[0]['props'])
+            ->toMatchArray(['id' => '168-915', 'title' => 'T', 'channel' => 'c1', 'style' => 'card']);
+        // id 优先于 book + para
+        expect(markupTree('{{chapter|id=1-2|book=3|para=4}}')[0]['props']['id'])->toBe('1-2');
+        expect(markupTree('{{chapter}}')[0]['props']['id'])->toBe('');
+        expect(markupRender('{{chapter|1-2|T}}', Format::Text))->toBe('T');
+        expect(markupRender('{{chapter|1-2|T}}', Format::Prompt))->toBe('');
+    });
+
     it('renders nissaya templates and nissaya channel lines', function () {
         expect(markupTree('{{nissaya|evaṃ|ဤသို့=thus}}')[0]['props'])
             ->toMatchArray(['pali' => 'evaṃ', 'meaning' => ['ဤသို့', 'thus']]);

+ 6 - 0
api-v13/tests/Feature/MarkupTemplateDataTest.php

@@ -357,6 +357,12 @@ describe('ref', function () {
         expect($titles(markupDataTree($source)))->toBe(['second work', 'third work', 'first work']);
     });
 
+    it('collects references from chapter, by id or by book and para', function () use ($titles) {
+        expect($titles(markupDataTree('{{chapter|998-12}} {{chapter|book=997|para=3}}{{ref}}')))
+            ->toBe(['second work', 'third work']);
+        expect($titles(markupDataTree('{{chapter|title=t}}{{ref}}')))->toBe([]);
+    });
+
     it('picks the nearest title, counting a reference to the first paragraph of a work', function () use ($titles) {
         expect($titles(markupDataTree('{{para|id=998-10}}{{ref}}')))->toBe(['second work']);
         expect($titles(markupDataTree('{{para|id=998-9}}{{ref}}')))->toBe(['first work']);

+ 11 - 0
dashboard-v6/src/components/tpl-builder/templates.ts

@@ -86,6 +86,17 @@ export const TEMPLATES: TplSpec[] = [
       style(),
     ],
   },
+  {
+    name: "chapter",
+    fields: [
+      { name: "id", kind: "text", placeholder: "book-para" },
+      { name: "book", kind: "number" },
+      { name: "para", kind: "number" },
+      { name: "title", kind: "text" },
+      { name: "channel", kind: "channel" },
+      style(),
+    ],
+  },
   {
     name: "para",
     fields: [

+ 3 - 0
dashboard-v6/src/locales/en-US/tpl-builder.ts

@@ -9,6 +9,7 @@ const items = {
   "tpl-builder.select.attachment": "Choose from attachments",
 
   "tpl-builder.tpl.article": "Article / chapter",
+  "tpl-builder.tpl.chapter": "Chapter",
   "tpl-builder.tpl.para": "Paragraph",
   "tpl-builder.tpl.sent": "Sentence",
   "tpl-builder.tpl.quote": "Quote",
@@ -31,6 +32,8 @@ const items = {
 
   "tpl-builder.desc.article":
     "Embed an article, an anthology or a chapter / paragraph. A chapter can be given by book + paragraph instead of an id.",
+  "tpl-builder.desc.chapter":
+    "Embed a chapter; the id is book-paragraph, or give book + paragraph instead.",
   "tpl-builder.desc.para": "Embed a paragraph; the id is book-paragraph.",
   "tpl-builder.desc.sent":
     "Embed a sentence; the id is book-paragraph-start-end. Without a channel, the content's own channel is used.",

+ 3 - 0
dashboard-v6/src/locales/zh-Hans/tpl-builder.ts

@@ -9,6 +9,7 @@ const items = {
   "tpl-builder.select.attachment": "从网盘选择",
 
   "tpl-builder.tpl.article": "文章 / 章节",
+  "tpl-builder.tpl.chapter": "经文章节",
   "tpl-builder.tpl.para": "经文段落",
   "tpl-builder.tpl.sent": "句子",
   "tpl-builder.tpl.quote": "引用段落",
@@ -31,6 +32,8 @@ const items = {
 
   "tpl-builder.desc.article":
     "嵌入一篇文章、文集或一个章节 / 段落。章节可以只给书号 + 段号,不填 id。",
+  "tpl-builder.desc.chapter":
+    "嵌入一个章节,id 写成 书号-段号,也可以不写 id、改填书号与段号。",
   "tpl-builder.desc.para": "嵌入一段经文,id 写成 书号-段号。",
   "tpl-builder.desc.sent":
     "嵌入一个句子,id 写成 书号-段号-起始词-结束词;不填 channel 用内容所属的版本。",

+ 3 - 0
dashboard-v6/src/locales/zh-Hant/tpl-builder.ts

@@ -9,6 +9,7 @@ const items = {
   "tpl-builder.select.attachment": "從網盤選擇",
 
   "tpl-builder.tpl.article": "文章 / 章節",
+  "tpl-builder.tpl.chapter": "經文章節",
   "tpl-builder.tpl.para": "經文段落",
   "tpl-builder.tpl.sent": "句子",
   "tpl-builder.tpl.quote": "引用段落",
@@ -31,6 +32,8 @@ const items = {
 
   "tpl-builder.desc.article":
     "嵌入一篇文章、文集或一個章節 / 段落。章節可以只給書號 + 段號,不填 id。",
+  "tpl-builder.desc.chapter":
+    "嵌入一個章節,id 寫成 書號-段號,也可以不寫 id、改填書號與段號。",
   "tpl-builder.desc.para": "嵌入一段經文,id 寫成 書號-段號。",
   "tpl-builder.desc.sent":
     "嵌入一個句子,id 寫成 書號-段號-起始詞-結束詞;不填 channel 用內容所屬的版本。",