Browse Source

✨ (markup): para / chapter 的 style=reference 不写标题时显示「wikipali 缩写 + 段号」

- PaliSeriesesService::citation():abbr_wp + 段号(如 dī.ni.1.168),与逐词检索的 WP 缩写同源
- Concerns\CitesParagraph:标题规则只写一份,{{para}}、{{chapter}}、{{article|type=chapter|para}} 共用;
  给了标题用标题、非 reference 不补、查不到缩写用 id
- EmbedsArticle 的标题改走可覆盖的 title(),props 与纯文本共用
- v6 ArticleCtl 补 reference 分支(复用 DisplayWrapper),原来 chapter 选 reference 什么都不显示;
  模板生成器的 chapter / para 显示方式加 reference

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 5 hours ago
parent
commit
32323b1f5f

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

@@ -4,6 +4,7 @@ namespace App\Services\Markup\Templates;
 
 use App\Services\Markup\Ir\Call;
 use App\Services\Markup\Render\RenderContext;
+use App\Services\Markup\Templates\Concerns\CitesParagraph;
 use App\Services\Markup\Templates\Concerns\EmbedsArticle;
 
 /**
@@ -14,6 +15,7 @@ use App\Services\Markup\Templates\Concerns\EmbedsArticle;
  */
 final class ArticleTemplate extends Template
 {
+    use CitesParagraph;
     use EmbedsArticle;
 
     protected string $component = 'article';
@@ -48,11 +50,22 @@ final class ArticleTemplate extends Template
      */
     public function references(Call $call): array
     {
-        if (! in_array((string) $call->param('type'), ['chapter', 'para'], true)) {
-            return [];
-        }
+        return $this->citesParagraph($call) ? self::paragraphRef($this->paragraphId($call)) : [];
+    }
 
-        return self::paragraphRef($this->paragraphId($call));
+    /**
+     * 章节 / 段落:`style=reference` 且没给标题时显示缩写 + 段号,与 {{chapter}}、{{para}} 相同。
+     */
+    protected function title(Call $call): string
+    {
+        return $this->citesParagraph($call)
+            ? $this->citedTitle($call, $this->paragraphId($call))
+            : (string) $call->param('title');
+    }
+
+    private function citesParagraph(Call $call): bool
+    {
+        return in_array((string) $call->param('type'), ['chapter', 'para'], true);
     }
 
     private function paragraphId(Call $call): string

+ 8 - 1
api-v13/app/Services/Markup/Templates/ChapterTemplate.php

@@ -4,16 +4,18 @@ namespace App\Services\Markup\Templates;
 
 use App\Services\Markup\Ir\Call;
 use App\Services\Markup\Render\RenderContext;
+use App\Services\Markup\Templates\Concerns\CitesParagraph;
 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()}。
+ * 后者拼 id 也走 {@see self::resolveId()}。`style=reference` 且没给标题时显示缩写 + 段号({@see CitesParagraph})。
  */
 final class ChapterTemplate extends Template
 {
+    use CitesParagraph;
     use EmbedsArticle;
 
     protected string $component = 'article';
@@ -55,6 +57,11 @@ final class ChapterTemplate extends Template
         return self::paragraphRef($this->id($call));
     }
 
+    protected function title(Call $call): string
+    {
+        return $this->citedTitle($call, $this->id($call));
+    }
+
     private function id(Call $call): string
     {
         return self::resolveId((string) $call->param('id'), (string) $call->param('book'), (string) $call->param('para'));

+ 31 - 0
api-v13/app/Services/Markup/Templates/Concerns/CitesParagraph.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace App\Services\Markup\Templates\Concerns;
+
+use App\Services\Markup\Ir\Call;
+use App\Services\PaliSeriesesService;
+
+/**
+ * 嵌入经文段落 / 章节的模板({{para}}、{{chapter}}、{{article|type=chapter|para}})的标题规则:
+ * `style=reference`(行内引用)且没给标题时,标题用 wikipali 缩写 + 段号(如 `dī.ni.1.168`),
+ * 查不到缩写时用 id 本身。
+ */
+trait CitesParagraph
+{
+    /**
+     * @param  string  $id  段落 / 章节 id(`book-para`)
+     */
+    protected function citedTitle(Call $call, string $id): string
+    {
+        $title = (string) $call->param('title');
+        if ($title !== '' || (string) $call->param('style') !== 'reference') {
+            return $title;
+        }
+        $ref = self::paragraphRef($id)[0] ?? null;
+        if ($ref === null) {
+            return $id;
+        }
+
+        return app(PaliSeriesesService::class)->citation($ref[0], $ref[1]) ?? $id;
+    }
+}

+ 11 - 2
api-v13/app/Services/Markup/Templates/Concerns/EmbedsArticle.php

@@ -9,6 +9,7 @@ use App\Services\Markup\Render\RenderContext;
 /**
  * 交给前端 `article` 组件嵌入的模板({{article}}、{{chapter}})共用的输出:
  * props 的拼法、纯文本只留标题、html 不输出(只有前端组件能渲染)。
+ * 标题走 {@see self::title()},模板可覆盖。
  */
 trait EmbedsArticle
 {
@@ -21,7 +22,7 @@ trait EmbedsArticle
         $props = ['type' => $type, 'id' => $id];
         // style 为空时不给:前端组件用它自己的默认值(modal),传空串反而什么都不渲染
         foreach ($optional as $name) {
-            $value = (string) $call->param($name);
+            $value = $name === 'title' ? $this->title($call) : (string) $call->param($name);
             if ($value !== '') {
                 $props[$name] = $value;
             }
@@ -31,9 +32,17 @@ trait EmbedsArticle
         return $props;
     }
 
+    /**
+     * 显示的标题(props 与纯文本共用);嵌入经文的模板按 {@see CitesParagraph} 补缺省标题。
+     */
+    protected function title(Call $call): string
+    {
+        return (string) $call->param('title');
+    }
+
     public function plain(Call $call, RenderContext $ctx, Format $format): string
     {
-        return $format === Format::Prompt ? '' : (string) $call->param('title');
+        return $format === Format::Prompt ? '' : $this->title($call);
     }
 
     public function html(Call $call, RenderContext $ctx): string

+ 6 - 2
api-v13/app/Services/Markup/Templates/ParaTemplate.php

@@ -4,12 +4,16 @@ namespace App\Services\Markup\Templates;
 
 use App\Services\Markup\Ir\Call;
 use App\Services\Markup\Render\RenderContext;
+use App\Services\Markup\Templates\Concerns\CitesParagraph;
 
 /**
  * 嵌入经文段落:`{{para|id=book-para|title|style}}`。
+ * `style=reference` 且没给标题时显示缩写 + 段号({@see CitesParagraph})。
  */
 final class ParaTemplate extends Template
 {
+    use CitesParagraph;
+
     protected string $component = 'para';
 
     public function params(): array
@@ -24,7 +28,7 @@ final class ParaTemplate extends Template
     {
         $props = [
             'id' => (string) $call->param('id'),
-            'title' => (string) $call->param('title'),
+            'title' => $this->citedTitle($call, (string) $call->param('id')),
             'style' => (string) $call->param('style'),
         ];
         if ($call->scope->channels !== []) {
@@ -41,6 +45,6 @@ final class ParaTemplate extends Template
 
     public function text(Call $call, RenderContext $ctx): string
     {
-        return (string) $call->param('title');
+        return $this->citedTitle($call, (string) $call->param('id'));
     }
 }

+ 10 - 0
api-v13/app/Services/PaliSeriesesService.php

@@ -43,4 +43,14 @@ class PaliSeriesesService
 
         return $result;
     }
+
+    /**
+     * wikipali 缩写 + 段号的引用,如 `dī.ni.1.168`(缩写本身以点结尾)。查不到缩写时为 null。
+     */
+    public function citation(int $book, int $paragraph): ?string
+    {
+        $abbr = $this->find($book, $paragraph)['abbr_wp'] ?? null;
+
+        return $abbr === null ? null : $abbr.$paragraph;
+    }
 }

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

@@ -482,6 +482,24 @@ describe('templates without data', function () {
         expect(markupRender('{{chapter|1-2|T}}', Format::Prompt))->toBe('');
     });
 
+    it('titles para and chapter references with the wikipali abbreviation and paragraph', function () {
+        // pali_serieses:book 1 自 para 2 起是 namakkāra.
+        expect(markupTree('{{para|id=1-2|style=reference}}')[0]['props']['title'])->toBe('namakkāra.2');
+        expect(markupTree('{{chapter|1-5|style=reference}}')[0]['props']['title'])->toBe('namakkāra.5');
+        expect(markupTree('{{chapter|book=1|para=5|style=reference}}')[0]['props']['title'])->toBe('namakkāra.5');
+        expect(markupTree('{{article|type=chapter|book=1|paragraphs=5|style=reference}}')[0]['props']['title'])->toBe('namakkāra.5');
+        expect(markupTree('{{article|type=para|id=1-5|style=reference}}')[0]['props']['title'])->toBe('namakkāra.5');
+        expect(markupRender('{{chapter|1-5|style=reference}}', Format::Text))->toBe('namakkāra.5');
+        expect(markupRender('{{para|id=1-2|style=reference}}', Format::Text))->toBe('namakkāra.2');
+
+        // 给了标题就用标题;不是 reference 不补;查不到缩写用 id;文章不补
+        expect(markupTree('{{chapter|1-5|T|style=reference}}')[0]['props']['title'])->toBe('T');
+        expect(markupTree('{{chapter|1-5|style=card}}')[0]['props'])->not->toHaveKey('title');
+        expect(markupTree('{{para|id=1-2|style=card}}')[0]['props']['title'])->toBe('');
+        expect(markupTree('{{chapter|99999-1|style=reference}}')[0]['props']['title'])->toBe('99999-1');
+        expect(markupTree('{{article|type=article|id=x|style=reference}}')[0]['props'])->not->toHaveKey('title');
+    });
+
     it('renders nissaya templates and nissaya channel lines', function () {
         expect(markupTree('{{nissaya|evaṃ|ဤသို့=thus}}')[0]['props'])
             ->toMatchArray(['pali' => 'evaṃ', 'meaning' => ['ဤသို့', 'thus']]);

+ 12 - 8
dashboard-v6/src/components/template/Article.tsx

@@ -10,16 +10,12 @@ import TypeAnthology from "../article/TypeAnthology";
 import TypePali from "../article/TypePali";
 import TypePage from "../article/TypePage";
 import { articlePath, fullUrl } from "../../utils";
+import { DisplayWrapper } from "../general/DisplayWrapper";
 
 const { Text } = Typography;
 
 export type TDisplayStyle =
-  | "modal"
-  | "card"
-  | "toggle"
-  | "link"
-  | "window"
-  | "popover";
+  "modal" | "card" | "toggle" | "link" | "window" | "popover" | "reference";
 
 interface IWidgetChapterCtl {
   type?: ArticleType;
@@ -140,7 +136,7 @@ export const ArticleCtl = ({
 
   const channelsToken = channel?.split(",").map((item) => item.split("@"));
   channelsToken?.forEach((value) =>
-    sessionStorage.setItem(value[0], value[1] ?? "")
+    sessionStorage.setItem(value[0], value[1] ?? ""),
   );
   const orgChannels = channel
     ? channel.split(",").map((item) => item.split("@")[0])
@@ -184,7 +180,7 @@ export const ArticleCtl = ({
         {
           id: "buttons.open.in.new.tab",
         },
-        { item: "" }
+        { item: "" },
       )}
     </Link>
   );
@@ -253,6 +249,14 @@ export const ArticleCtl = ({
     case "window":
       output = <div style={{ width: "100%" }}>{article}</div>;
       break;
+    case "reference":
+      // 行内引用:标题(缺省是后端给的「缩写 + 段号」)虚线下划线,悬停看正文
+      output = (
+        <DisplayWrapper style="reference" title={aTitle}>
+          {article}
+        </DisplayWrapper>
+      );
+      break;
     default:
       break;
   }

+ 10 - 4
dashboard-v6/src/components/tpl-builder/templates.ts

@@ -46,10 +46,16 @@ export type TplValues = Record<string, string | undefined>;
 /** 前端 DisplayWrapper 支持的显示方式(types/template.ts 的 TDisplayStyle) */
 const DISPLAY_STYLES = ["modal", "card", "toggle", "link", "window", "popover"];
 
-const style = (defaultValue?: string): TplField => ({
+/** 嵌入经文的模板(chapter、para)另有行内引用 reference:不写标题时显示「缩写 + 段号」 */
+const PARAGRAPH_STYLES = [...DISPLAY_STYLES, "reference"];
+
+const style = (
+  defaultValue?: string,
+  options: string[] = DISPLAY_STYLES,
+): TplField => ({
   name: "style",
   kind: "select",
-  options: DISPLAY_STYLES,
+  options,
   defaultValue,
 });
 
@@ -94,7 +100,7 @@ export const TEMPLATES: TplSpec[] = [
       { name: "para", kind: "number" },
       { name: "title", kind: "text" },
       { name: "channel", kind: "channel" },
-      style(),
+      style(undefined, PARAGRAPH_STYLES),
     ],
   },
   {
@@ -102,7 +108,7 @@ export const TEMPLATES: TplSpec[] = [
     fields: [
       { name: "id", kind: "text", placeholder: "book-para", required: true },
       { name: "title", kind: "text" },
-      style(),
+      style(undefined, PARAGRAPH_STYLES),
     ],
   },
   {