Browse Source

🔥 (api-v13): 删掉没有调用方的模板渲染实验代码

Services/Template、Services/Templates、Services/TemplateRender.php 是重写渲染的早期实验,
没有任何调用方(只有 test:md.render 命令开头一段引用了它并提前 return,后面原来的测试都跑不到)。
新实现见 App\Services\Markup。命令里那一段一并删掉。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 7 hours ago
parent
commit
a67517edd5
24 changed files with 0 additions and 1824 deletions
  1. 0 28
      api-v13/app/Console/Commands/TestMdRender.php
  2. 0 14
      api-v13/app/Services/Template/ContentNode.php
  3. 0 17
      api-v13/app/Services/Template/Contracts/ParserInterface.php
  4. 0 24
      api-v13/app/Services/Template/MarkdownNode.php
  5. 0 50
      api-v13/app/Services/Template/ParameterResolver.php
  6. 0 20
      api-v13/app/Services/Template/ParsedDocument.php
  7. 0 141
      api-v13/app/Services/Template/Renderers/HtmlRenderer.php
  8. 0 34
      api-v13/app/Services/Template/Renderers/JsonRenderer.php
  9. 0 67
      api-v13/app/Services/Template/Renderers/MarkdownRenderer.php
  10. 0 32
      api-v13/app/Services/Template/Renderers/RendererFactory.php
  11. 0 149
      api-v13/app/Services/Template/Renderers/TextRenderer.php
  12. 0 36
      api-v13/app/Services/Template/TemplateNode.php
  13. 0 160
      api-v13/app/Services/Template/TemplateParser.php
  14. 0 82
      api-v13/app/Services/Template/TemplateRegistry.php
  15. 0 219
      api-v13/app/Services/Template/TemplateService.php
  16. 0 119
      api-v13/app/Services/Template/TemplateTokenizer.php
  17. 0 24
      api-v13/app/Services/Template/TextNode.php
  18. 0 76
      api-v13/app/Services/TemplateRender.php
  19. 0 37
      api-v13/app/Services/Templates/AbstractTemplate.php
  20. 0 43
      api-v13/app/Services/Templates/ConfidenceTemplate.php
  21. 0 54
      api-v13/app/Services/Templates/NissayaTemplate.php
  22. 0 116
      api-v13/app/Services/Templates/NoteTemplate.php
  23. 0 12
      api-v13/app/Services/Templates/TemplateInterface.php
  24. 0 270
      api-v13/app/Services/Templates/TermTemplate.php

+ 0 - 28
api-v13/app/Console/Commands/TestMdRender.php

@@ -3,7 +3,6 @@
 namespace App\Console\Commands;
 
 use App\Http\Api\MdRender;
-use App\Services\Template\TemplateService;
 use App\Tools\Markdown;
 use App\Tools\Tools;
 use Illuminate\Console\Command;
@@ -48,33 +47,6 @@ class TestMdRender extends Command
             return 0;
         }
 
-        $content = <<<'md'
-        # 测试
-        ## 测试
-
-        {{note|text=这是一个普通信息提示|type=info}}
-
-        下面是一个警告信息:
-        {{warning|message=请注意这个重要提示|title=重要}}
-
-        你也可以使用位置参数:
-        {{note|
-        成功完成操作|
-        success}}
-
-        支持嵌套模板:
-        {{info|
-        content=外层信息 {{note|内嵌提示|warning}} 继续外层|
-        title=嵌套示例}}
-
-        **粗体文本** 和 *斜体文本* 也被支持。
-        md;
-        $parser = new TemplateService(false);
-        $render = $parser->parseAndRender($content, 'json');
-        var_dump($render);
-
-        return 0;
-
         Log::info('md render start item='.$this->argument('item'));
         $data = [];
         $data['bold'] = <<<'md'

+ 0 - 14
api-v13/app/Services/Template/ContentNode.php

@@ -1,14 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-abstract class ContentNode
-{
-    public string $type;
-
-    public string $content;
-
-    public array $position = [];
-
-    abstract public function toArray(): array;
-}

+ 0 - 17
api-v13/app/Services/Template/Contracts/ParserInterface.php

@@ -1,17 +0,0 @@
-<?php
-
-// ================== 契约接口 ==================
-
-namespace App\Services\Template\Contracts;
-
-use App\Services\Template\ParsedDocument;
-
-interface ParserInterface
-{
-    public function parse(string $content): ParsedDocument;
-}
-
-interface RendererInterface
-{
-    public function render(ParsedDocument $document): string;
-}

+ 0 - 24
api-v13/app/Services/Template/MarkdownNode.php

@@ -1,24 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-class MarkdownNode extends ContentNode
-{
-    public string $content;
-
-    public function __construct(string $content, array $position = [])
-    {
-        $this->type = 'markdown';
-        $this->content = $content;
-        $this->position = $position;
-    }
-
-    public function toArray(): array
-    {
-        return [
-            'type' => $this->type,
-            'content' => $this->content,
-            'position' => $this->position,
-        ];
-    }
-}

+ 0 - 50
api-v13/app/Services/Template/ParameterResolver.php

@@ -1,50 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-// ================== 参数解析器 ==================
-
-class ParameterResolver
-{
-    private TemplateRegistry $registry;
-
-    public function __construct(TemplateRegistry $registry)
-    {
-        $this->registry = $registry;
-    }
-
-    public function resolveParameters(string $templateName, array $rawParams): array
-    {
-        $template = $this->registry->getTemplate($templateName);
-        if (! $template) {
-            return $rawParams;
-        }
-
-        $resolved = [];
-        $paramMapping = $template['paramMapping'] ?? [];
-        $defaultValues = $template['defaultValues'] ?? [];
-
-        // 处理位置参数
-        $positionIndex = 0;
-        foreach ($rawParams as $key => $value) {
-            if (is_numeric($key)) {
-                // 位置参数
-                $paramName = $paramMapping[$positionIndex] ?? $positionIndex;
-                $resolved[$paramName] = $value;
-                $positionIndex++;
-            } else {
-                // 命名参数
-                $resolved[$key] = $value;
-            }
-        }
-
-        // 应用默认值
-        foreach ($defaultValues as $key => $value) {
-            if (! isset($resolved[$key])) {
-                $resolved[$key] = $value;
-            }
-        }
-
-        return $resolved;
-    }
-}

+ 0 - 20
api-v13/app/Services/Template/ParsedDocument.php

@@ -1,20 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-// ================== 数据结构 ==================
-
-class ParsedDocument
-{
-    public string $type = 'document';
-
-    public array $content = [];
-
-    public array $meta = [];
-
-    public function __construct(array $content = [], array $meta = [])
-    {
-        $this->content = $content;
-        $this->meta = $meta;
-    }
-}

+ 0 - 141
api-v13/app/Services/Template/Renderers/HtmlRenderer.php

@@ -1,141 +0,0 @@
-<?php
-
-namespace App\Services\Template\Renderers;
-
-use App\Services\Template\ContentNode;
-use App\Services\Template\Contracts\RendererInterface;
-use App\Services\Template\MarkdownNode;
-use App\Services\Template\ParsedDocument;
-use App\Services\Template\TemplateNode;
-use App\Services\Template\TextNode;
-use Illuminate\Support\Str;
-
-// ================== HTML 渲染器 ==================
-
-class HtmlRenderer implements RendererInterface
-{
-    private array $templateMappings = [];
-
-    public function __construct()
-    {
-        $this->initializeTemplateMappings();
-    }
-
-    private function initializeTemplateMappings(): void
-    {
-        $this->templateMappings = [
-            'note' => function ($params) {
-                $type = $params['type'] ?? 'info';
-                $text = $params['text'] ?? '';
-                $title = $params['title'] ?? '';
-
-                $typeClass = match ($type) {
-                    'warning' => 'alert-warning',
-                    'error' => 'alert-danger',
-                    'success' => 'alert-success',
-                    default => 'alert-info'
-                };
-
-                $titleHtml = $title ? "<h6 class='alert-heading'>$title</h6>" : '';
-
-                return "<div class='alert $typeClass' role='alert'>$titleHtml$text</div>";
-            },
-
-            'info' => function ($params) {
-                $content = $params['content'] ?? '';
-                $title = $params['title'] ?? '';
-
-                $titleHtml = $title ? "<h6 class='alert-heading'>$title</h6>" : '';
-
-                return "<div class='alert alert-info' role='alert'>$titleHtml$content</div>";
-            },
-
-            'warning' => function ($params) {
-                $message = $params['message'] ?? '';
-                $title = $params['title'] ?? '';
-
-                $titleHtml = $title ? "<h6 class='alert-heading'>$title</h6>" : '';
-
-                return "<div class='alert alert-warning' role='alert'>$titleHtml$message</div>";
-            },
-        ];
-    }
-
-    public function render(ParsedDocument $document): string
-    {
-        $html = '';
-
-        foreach ($document->content as $node) {
-            $html .= $this->renderNode($node);
-        }
-
-        return Str::markdown($html);
-    }
-
-    private function renderNode(ContentNode $node): string
-    {
-        switch ($node->type) {
-            case 'text':
-                return $this->renderText($node);
-            case 'markdown':
-                return $this->renderMarkdown($node);
-            case 'template':
-                return $this->renderTemplate($node);
-            default:
-                return '';
-        }
-    }
-
-    public function renderText(TextNode $text): string
-    {
-        return htmlspecialchars($text->content, ENT_QUOTES, 'UTF-8');
-    }
-
-    private function renderMarkdown(MarkdownNode $markdown): string
-    {
-        return Str::markdown($markdown->content);
-        // 简单的 Markdown 渲染,实际项目中可以使用 CommonMark 等库
-        $html = $markdown->content;
-
-        // 处理粗体
-        $html = preg_replace('/\*\*(.*?)\*\*/', '<strong>$1</strong>', $html);
-        $html = preg_replace('/\*(.*?)\*/', '<em>$1</em>', $html);
-
-        // 处理链接
-        $html = preg_replace('/\[([^\]]+)\]\(([^)]+)\)/', '<a href="$2">$1</a>', $html);
-
-        return $html;
-    }
-
-    public function renderTemplate(TemplateNode $template): string
-    {
-        if (! isset($this->templateMappings[$template->name])) {
-            // 未知模板,返回原始内容
-            return htmlspecialchars($template->raw, ENT_QUOTES, 'UTF-8');
-        }
-
-        $renderer = $this->templateMappings[$template->name];
-
-        // 处理参数中的嵌套内容
-        $processedParams = [];
-        foreach ($template->parameters as $key => $value) {
-            if (is_array($value)) {
-                // 嵌套内容,递归渲染
-                $nestedHtml = '';
-                foreach ($value as $childNode) {
-                    $nestedHtml .= $this->renderNode($childNode);
-                }
-                $processedParams[$key] = $nestedHtml;
-            } else {
-                $processedParams[$key] = $value;
-            }
-        }
-
-        return $renderer($processedParams);
-    }
-
-    public function registerTemplate(string $name, callable $renderer): void
-    {
-        $this->templateMappings[$name] = $renderer;
-    }
-}

+ 0 - 34
api-v13/app/Services/Template/Renderers/JsonRenderer.php

@@ -1,34 +0,0 @@
-<?php
-
-namespace App\Services\Template\Renderers;
-
-use App\Services\Template\Contracts\RendererInterface;
-use App\Services\Template\ParsedDocument;
-use App\Services\Template\TemplateNode;
-use App\Services\Template\TextNode;
-
-// ================== JSON 渲染器 ==================
-
-class JsonRenderer implements RendererInterface
-{
-    public function render(ParsedDocument $document): string
-    {
-        $data = [
-            'type' => $document->type,
-            'content' => array_map(fn ($node) => $node->toArray(), $document->content),
-            'meta' => $document->meta,
-        ];
-
-        return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
-    }
-
-    public function renderTemplate(TemplateNode $template): string
-    {
-        return json_encode($template->toArray(), JSON_UNESCAPED_UNICODE);
-    }
-
-    public function renderText(TextNode $text): string
-    {
-        return json_encode($text->toArray(), JSON_UNESCAPED_UNICODE);
-    }
-}

+ 0 - 67
api-v13/app/Services/Template/Renderers/MarkdownRenderer.php

@@ -1,67 +0,0 @@
-<?php
-
-namespace App\Services\Template\Renderers;
-
-use App\Services\Template\ContentNode;
-use App\Services\Template\Contracts\RendererInterface;
-use App\Services\Template\ParsedDocument;
-use App\Services\Template\TemplateNode;
-use App\Services\Template\TextNode;
-
-// ================== Markdown 渲染器 ==================
-
-class MarkdownRenderer implements RendererInterface
-{
-    public function render(ParsedDocument $document): string
-    {
-        $markdown = '';
-
-        foreach ($document->content as $node) {
-            $markdown .= $this->renderNode($node);
-        }
-
-        return $markdown;
-    }
-
-    private function renderNode(ContentNode $node): string
-    {
-        switch ($node->type) {
-            case 'text':
-                return $this->renderText($node);
-            case 'markdown':
-                return $node->content;
-            case 'template':
-                return $this->renderTemplate($node);
-            default:
-                return '';
-        }
-    }
-
-    public function renderText(TextNode $text): string
-    {
-        return $text->content;
-    }
-
-    public function renderTemplate(TemplateNode $template): string
-    {
-        // 将模板转换回 Markdown 格式
-        $params = [];
-
-        foreach ($template->parameters as $key => $value) {
-            if (is_array($value)) {
-                // 嵌套内容,递归渲染
-                $nestedMarkdown = '';
-                foreach ($value as $childNode) {
-                    $nestedMarkdown .= $this->renderNode($childNode);
-                }
-                $params[] = "$key=$nestedMarkdown";
-            } else {
-                $params[] = is_numeric($key) ? $value : "$key=$value";
-            }
-        }
-
-        $paramString = implode('|', $params);
-
-        return '{{'.$template->name.($paramString ? "|$paramString" : '').'}}';
-    }
-}

+ 0 - 32
api-v13/app/Services/Template/Renderers/RendererFactory.php

@@ -1,32 +0,0 @@
-<?php
-
-namespace App\Services\Template\Renderers;
-
-use App\Services\Template\Contracts\RendererInterface;
-
-// ================== 渲染器工厂 ==================
-
-class RendererFactory
-{
-    private static array $renderers = [];
-
-    public static function create(string $format): RendererInterface
-    {
-        if (! isset(self::$renderers[$format])) {
-            self::$renderers[$format] = match ($format) {
-                'json' => new JsonRenderer,
-                'html' => new HtmlRenderer,
-                'markdown' => new MarkdownRenderer,
-                'text' => new TextRenderer,
-                default => throw new \InvalidArgumentException("Unsupported format: $format")
-            };
-        }
-
-        return self::$renderers[$format];
-    }
-
-    public static function getSupportedFormats(): array
-    {
-        return ['json', 'html', 'markdown', 'text'];
-    }
-}

+ 0 - 149
api-v13/app/Services/Template/Renderers/TextRenderer.php

@@ -1,149 +0,0 @@
-<?php
-
-namespace App\Services\Template\Renderers;
-
-use App\Services\Template\ContentNode;
-use App\Services\Template\Contracts\RendererInterface;
-use App\Services\Template\MarkdownNode;
-use App\Services\Template\ParsedDocument;
-use App\Services\Template\TemplateNode;
-use App\Services\Template\TextNode;
-
-// ================== 纯文本渲染器 ==================
-
-class TextRenderer implements RendererInterface
-{
-    private array $templateTexts = [];
-
-    public function __construct()
-    {
-        $this->initializeTemplateTexts();
-    }
-
-    private function initializeTemplateTexts(): void
-    {
-        $this->templateTexts = [
-            'note' => function ($params) {
-                $type = $params['type'] ?? 'info';
-                $text = $params['text'] ?? '';
-                $title = $params['title'] ?? '';
-
-                $prefix = match ($type) {
-                    'warning' => '[警告]',
-                    'error' => '[错误]',
-                    'success' => '[成功]',
-                    default => '[信息]'
-                };
-
-                return $prefix.($title ? " $title: " : ' ').$text;
-            },
-
-            'info' => function ($params) {
-                $content = $params['content'] ?? '';
-                $title = $params['title'] ?? '';
-
-                return '[信息]'.($title ? " $title: " : ' ').$content;
-            },
-
-            'warning' => function ($params) {
-                $message = $params['message'] ?? '';
-                $title = $params['title'] ?? '';
-
-                return '[警告]'.($title ? " $title: " : ' ').$message;
-            },
-        ];
-    }
-
-    public function render(ParsedDocument $document): string
-    {
-        $text = '';
-
-        foreach ($document->content as $node) {
-            $text .= $this->renderNode($node);
-        }
-
-        return $text;
-    }
-
-    private function renderNode(ContentNode $node): string
-    {
-        switch ($node->type) {
-            case 'text':
-                return $this->renderText($node);
-            case 'markdown':
-                return $this->renderMarkdownAsText($node);
-            case 'template':
-                return $this->renderTemplate($node);
-            default:
-                return '';
-        }
-    }
-
-    public function renderText(TextNode $text): string
-    {
-        return $text->content;
-    }
-
-    private function renderMarkdownAsText(MarkdownNode $markdown): string
-    {
-        // 移除 Markdown 标记,返回纯文本
-        $text = $markdown->content;
-
-        // 移除粗体和斜体标记
-        $text = preg_replace('/\*\*(.*?)\*\*/', '$1', $text);
-        $text = preg_replace('/\*(.*?)\*/', '$1', $text);
-
-        // 移除链接标记,保留链接文本
-        $text = preg_replace('/\[([^\]]+)\]\([^)]+\)/', '$1', $text);
-
-        return $text;
-    }
-
-    public function renderTemplate(TemplateNode $template): string
-    {
-        if (! isset($this->templateTexts[$template->name])) {
-            // 未知模板,返回简化的文本表示
-            $text = $template->name;
-            if (! empty($template->parameters)) {
-                $paramTexts = [];
-                foreach ($template->parameters as $key => $value) {
-                    if (is_array($value)) {
-                        $nestedText = '';
-                        foreach ($value as $childNode) {
-                            $nestedText .= $this->renderNode($childNode);
-                        }
-                        $paramTexts[] = is_numeric($key) ? $nestedText : "$key: $nestedText";
-                    } else {
-                        $paramTexts[] = is_numeric($key) ? $value : "$key: $value";
-                    }
-                }
-                $text .= '('.implode(', ', $paramTexts).')';
-            }
-
-            return $text;
-        }
-
-        $renderer = $this->templateTexts[$template->name];
-
-        // 处理参数中的嵌套内容
-        $processedParams = [];
-        foreach ($template->parameters as $key => $value) {
-            if (is_array($value)) {
-                $nestedText = '';
-                foreach ($value as $childNode) {
-                    $nestedText .= $this->renderNode($childNode);
-                }
-                $processedParams[$key] = $nestedText;
-            } else {
-                $processedParams[$key] = $value;
-            }
-        }
-
-        return $renderer($processedParams);
-    }
-
-    public function registerTemplate(string $name, callable $renderer): void
-    {
-        $this->templateTexts[$name] = $renderer;
-    }
-}

+ 0 - 36
api-v13/app/Services/Template/TemplateNode.php

@@ -1,36 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-class TemplateNode extends ContentNode
-{
-    public string $name;
-
-    public array $parameters = [];
-
-    public array $children = [];
-
-    public string $raw = '';
-
-    public function __construct(string $name, array $parameters = [], array $children = [], string $raw = '', array $position = [])
-    {
-        $this->type = 'template';
-        $this->name = $name;
-        $this->parameters = $parameters;
-        $this->children = $children;
-        $this->raw = $raw;
-        $this->position = $position;
-    }
-
-    public function toArray(): array
-    {
-        return [
-            'type' => $this->type,
-            'name' => $this->name,
-            'parameters' => $this->parameters,
-            'children' => array_map(fn ($child) => $child->toArray(), $this->children),
-            'raw' => $this->raw,
-            'position' => $this->position,
-        ];
-    }
-}

+ 0 - 160
api-v13/app/Services/Template/TemplateParser.php

@@ -1,160 +0,0 @@
-<?php
-
-// ================== 主解析器 ==================
-
-namespace App\Services\Template;
-
-use App\Services\Template\Contracts\ParserInterface;
-
-class TemplateParser implements ParserInterface
-{
-    private TemplateRegistry $registry;
-
-    private ParameterResolver $parameterResolver;
-
-    public function __construct()
-    {
-        $this->registry = new TemplateRegistry;
-        $this->parameterResolver = new ParameterResolver($this->registry);
-    }
-
-    public function parse(string $content): ParsedDocument
-    {
-        $tokenizer = new TemplateTokenizer($content);
-        $tokens = $tokenizer->tokenize();
-
-        $nodes = [];
-        $templatesUsed = [];
-
-        foreach ($tokens as $token) {
-            if ($token['type'] === 'text') {
-                $nodes[] = new TextNode($token['content'], $token['position']);
-            } elseif ($token['type'] === 'template') {
-                $templateNode = $this->parseTemplateContent($token);
-                if ($templateNode) {
-                    $nodes[] = $templateNode;
-                    $templatesUsed[] = $templateNode->name;
-                } else {
-                    // 解析失败,当作文本处理
-                    $nodes[] = new TextNode($token['raw'], $token['position']);
-                }
-            }
-        }
-
-        return new ParsedDocument($nodes, [
-            'templates_used' => array_unique($templatesUsed),
-            'total_templates' => count($templatesUsed),
-        ]);
-    }
-
-    private function parseTemplateContent(array $token): ?TemplateNode
-    {
-        $content = $token['content'];
-        $parts = $this->splitTemplateParts($content);
-
-        if (empty($parts)) {
-            return null;
-        }
-
-        $templateName = array_shift($parts);
-        $rawParams = $this->parseParameters($parts);
-
-        // 递归解析参数中的嵌套模板
-        $processedParams = [];
-        foreach ($rawParams as $key => $value) {
-            if (strpos($value, '{{') !== false) {
-                // 参数值包含模板,递归解析
-                $subDocument = $this->parse($value);
-                $processedParams[$key] = $subDocument->content;
-            } else {
-                $processedParams[$key] = $value;
-            }
-        }
-
-        $resolvedParams = $this->parameterResolver->resolveParameters($templateName, $processedParams);
-
-        return new TemplateNode(
-            $templateName,
-            $resolvedParams,
-            [],
-            $token['raw'],
-            $token['position']
-        );
-    }
-
-    private function splitTemplateParts(string $content): array
-    {
-        $parts = [];
-        $current = '';
-        $braceLevel = 0;
-        $inQuotes = false;
-        $quoteChar = '';
-
-        for ($i = 0; $i < strlen($content); $i++) {
-            $char = $content[$i];
-            $nextChar = $i + 1 < strlen($content) ? $content[$i + 1] : '';
-
-            if (! $inQuotes && ($char === '"' || $char === "'")) {
-                $inQuotes = true;
-                $quoteChar = $char;
-                $current .= $char;
-            } elseif ($inQuotes && $char === $quoteChar) {
-                $inQuotes = false;
-                $quoteChar = '';
-                $current .= $char;
-            } elseif (! $inQuotes && $char === '{' && $nextChar === '{') {
-                $braceLevel++;
-                $current .= '{{';
-                $i++; // 跳过下一个字符
-            } elseif (! $inQuotes && $char === '}' && $nextChar === '}') {
-                $braceLevel--;
-                $current .= '}}';
-                $i++; // 跳过下一个字符
-            } elseif (! $inQuotes && $char === '|' && $braceLevel === 0) {
-                $parts[] = trim($current);
-                $current = '';
-            } else {
-                $current .= $char;
-            }
-        }
-
-        if ($current !== '') {
-            $parts[] = trim($current);
-        }
-
-        return $parts;
-    }
-
-    private function parseParameters(array $parts): array
-    {
-        $params = [];
-        $positionalIndex = 0;
-
-        foreach ($parts as $part) {
-            if (strpos($part, '=') !== false && ! $this->isInNestedTemplate($part)) {
-                // 命名参数
-                [$key, $value] = explode('=', $part, 2);
-                $params[trim($key)] = trim($value);
-            } else {
-                // 位置参数
-                $params[$positionalIndex] = trim($part);
-                $positionalIndex++;
-            }
-        }
-
-        return $params;
-    }
-
-    private function isInNestedTemplate(string $content): bool
-    {
-        $openBraces = substr_count($content, '{{');
-        $closeBraces = substr_count($content, '}}');
-
-        return $openBraces > 0 && $openBraces >= $closeBraces;
-    }
-
-    public function getRegistry(): TemplateRegistry
-    {
-        return $this->registry;
-    }
-}

+ 0 - 82
api-v13/app/Services/Template/TemplateRegistry.php

@@ -1,82 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-// ================== 模板注册表 ==================
-
-class TemplateRegistry
-{
-    private array $templates = [];
-
-    public function __construct()
-    {
-        $this->loadDefaultTemplates();
-    }
-
-    private function loadDefaultTemplates(): void
-    {
-        $this->templates = [
-            'note' => [
-                'defaultParams' => ['text', 'type'],
-                'paramMapping' => [
-                    '0' => 'text',
-                    '1' => 'type',
-                ],
-                'defaultValues' => [
-                    'type' => 'info',
-                ],
-                'validation' => [
-                    'required' => ['text'],
-                    'optional' => ['type', 'title'],
-                ],
-            ],
-            'info' => [
-                'defaultParams' => ['content'],
-                'paramMapping' => [
-                    '0' => 'content',
-                ],
-                'defaultValues' => [],
-                'validation' => [
-                    'required' => ['content'],
-                    'optional' => ['title'],
-                ],
-            ],
-            'warning' => [
-                'defaultParams' => ['message'],
-                'paramMapping' => [
-                    '0' => 'message',
-                ],
-                'defaultValues' => [],
-                'validation' => [
-                    'required' => ['message'],
-                    'optional' => ['title'],
-                ],
-            ],
-        ];
-    }
-
-    public function registerTemplate(string $name, array $config): void
-    {
-        $this->templates[$name] = $config;
-    }
-
-    public function getTemplate(string $name): ?array
-    {
-        return $this->templates[$name] ?? null;
-    }
-
-    public function hasTemplate(string $name): bool
-    {
-        return isset($this->templates[$name]);
-    }
-
-    public function getParamMapping(string $templateName): array
-    {
-        return $this->templates[$templateName]['paramMapping'] ?? [];
-    }
-
-    public function getDefaultValues(string $templateName): array
-    {
-        return $this->templates[$templateName]['defaultValues'] ?? [];
-    }
-}

+ 0 - 219
api-v13/app/Services/Template/TemplateService.php

@@ -1,219 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-use App\Facades\RedisCache;
-use App\Services\Template\Renderers\RendererFactory;
-use Illuminate\Support\Facades\Log;
-
-class TemplateService
-{
-    private const CACHE_VERSION_KEY = 'template/version';
-
-    private TemplateParser $parser;
-
-    private bool $cacheEnabled;
-
-    private int $cacheTtl;
-
-    public function __construct(bool $cacheEnabled = true, int $cacheTtl = 3600)
-    {
-        $this->parser = new TemplateParser;
-        $this->cacheEnabled = $cacheEnabled;
-        $this->cacheTtl = $cacheTtl;
-    }
-
-    /**
-     * 解析并渲染内容
-     */
-    public function parseAndRender(string $content, string $format = 'json'): array
-    {
-        try {
-            // 生成缓存键
-            $cacheKey = $this->generateCacheKey($content, $format);
-
-            if ($this->cacheEnabled && RedisCache::has($cacheKey)) {
-                return RedisCache::get($cacheKey);
-            }
-
-            // 解析内容
-            $document = $this->parser->parse($content);
-
-            // 渲染内容
-            $renderer = RendererFactory::create($format);
-            $renderedContent = $renderer->render($document);
-
-            $result = [
-                'data' => $format === 'json' ? json_decode($renderedContent, true) : $renderedContent,
-                'meta' => $document->meta,
-            ];
-
-            // 缓存结果
-            if ($this->cacheEnabled) {
-                RedisCache::put($cacheKey, $result, $this->cacheTtl);
-            }
-
-            return $result;
-        } catch (\Exception $e) {
-            Log::error('Template parsing failed', [
-                'content' => substr($content, 0, 200),
-                'format' => $format,
-                'error' => $e->getMessage(),
-            ]);
-
-            throw $e;
-        }
-    }
-
-    /**
-     * 仅解析,不渲染
-     */
-    public function parse(string $content): ParsedDocument
-    {
-        return $this->parser->parse($content);
-    }
-
-    /**
-     * 仅渲染已解析的文档
-     */
-    public function render(ParsedDocument $document, string $format): string
-    {
-        $renderer = RendererFactory::create($format);
-
-        return $renderer->render($document);
-    }
-
-    /**
-     * 注册新模板
-     */
-    public function registerTemplate(string $name, array $config): void
-    {
-        $registry = $this->parser->getRegistry();
-        $registry->registerTemplate($name, $config);
-
-        // 清除相关缓存
-        if ($this->cacheEnabled) {
-            $this->clearTemplateCache($name);
-        }
-    }
-
-    /**
-     * 获取可用模板列表
-     */
-    public function getAvailableTemplates(): array
-    {
-        $registry = $this->parser->getRegistry();
-        $templates = [];
-
-        // 这里需要添加一个方法来获取所有模板
-        // 为了示例,我们返回一些基本信息
-        $basicTemplates = ['note', 'info', 'warning'];
-
-        foreach ($basicTemplates as $templateName) {
-            $template = $registry->getTemplate($templateName);
-            if ($template) {
-                $templates[$templateName] = [
-                    'name' => $templateName,
-                    'config' => $template,
-                    'example' => $this->generateTemplateExample($templateName, $template),
-                ];
-            }
-        }
-
-        return $templates;
-    }
-
-    /**
-     * 生成模板使用示例
-     */
-    private function generateTemplateExample(string $name, array $config): string
-    {
-        $params = [];
-        $defaultParams = $config['defaultParams'] ?? [];
-
-        foreach ($defaultParams as $index => $paramName) {
-            $params[] = $paramName.'=示例值'.($index + 1);
-        }
-
-        return '{{'.$name.'|'.implode('|', $params).'}}';
-    }
-
-    /**
-     * 生成缓存键。
-     *
-     * 键里带版本号:模板定义变了就把版本号加一,旧版本的缓存不再被读到、到期自然消失。
-     */
-    private function generateCacheKey(string $content, string $format): string
-    {
-        $version = RedisCache::get(self::CACHE_VERSION_KEY, 0);
-
-        return "template/v{$version}/{$format}/".md5($content);
-    }
-
-    /**
-     * 让所有模板渲染缓存失效。
-     *
-     * 缓存键是内容的 md5,按模板名查不出受影响的键,所以整体换版本号。
-     * 原来用的是 Cache::flush(),在 Redis Cluster 上会清空所有 master,
-     * 连队列和其他业务缓存一起清掉。
-     */
-    private function clearTemplateCache(string $templateName): void
-    {
-        RedisCache::increment(self::CACHE_VERSION_KEY);
-    }
-
-    /**
-     * 批量处理内容
-     */
-    public function batchProcess(array $contents, string $format = 'json'): array
-    {
-        $results = [];
-
-        foreach ($contents as $index => $content) {
-            try {
-                $results[$index] = $this->parseAndRender($content, $format);
-            } catch (\Exception $e) {
-                $results[$index] = [
-                    'success' => false,
-                    'error' => $e->getMessage(),
-                ];
-            }
-        }
-
-        return $results;
-    }
-
-    /**
-     * 验证模板语法
-     */
-    public function validateSyntax(string $content): array
-    {
-        $errors = [];
-
-        try {
-            $document = $this->parser->parse($content);
-
-            // 检查是否有未知模板
-            foreach ($document->content as $node) {
-                if ($node instanceof TemplateNode) {
-                    $registry = $this->parser->getRegistry();
-                    if (! $registry->hasTemplate($node->name)) {
-                        $errors[] = [
-                            'type' => 'unknown_template',
-                            'template' => $node->name,
-                            'position' => $node->position,
-                            'message' => "Unknown template: {$node->name}",
-                        ];
-                    }
-                }
-            }
-        } catch (\Exception $e) {
-            $errors[] = [
-                'type' => 'parse_error',
-                'message' => $e->getMessage(),
-            ];
-        }
-
-        return $errors;
-    }
-}

+ 0 - 119
api-v13/app/Services/Template/TemplateTokenizer.php

@@ -1,119 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-// ================== 词法分析器 ==================
-
-class TemplateTokenizer
-{
-    private string $content;
-
-    private int $position = 0;
-
-    private int $length;
-
-    public function __construct(string $content)
-    {
-        $this->content = $content;
-        $this->length = strlen($content);
-    }
-
-    public function tokenize(): array
-    {
-        $tokens = [];
-        $this->position = 0;
-
-        while ($this->position < $this->length) {
-            $token = $this->nextToken();
-            if ($token) {
-                $tokens[] = $token;
-            }
-        }
-
-        return $tokens;
-    }
-
-    private function nextToken(): ?array
-    {
-        // 寻找模板开始标记
-        $templateStart = strpos($this->content, '{{', $this->position);
-
-        if ($templateStart === false) {
-            // 没有更多模板,返回剩余文本
-            if ($this->position < $this->length) {
-                $text = substr($this->content, $this->position);
-                $this->position = $this->length;
-
-                return [
-                    'type' => 'text',
-                    'content' => $text,
-                    'position' => ['start' => $this->position - strlen($text), 'end' => $this->position],
-                ];
-            }
-
-            return null;
-        }
-
-        // 如果模板前有文本
-        if ($templateStart > $this->position) {
-            $text = substr($this->content, $this->position, $templateStart - $this->position);
-            $this->position = $templateStart;
-
-            return [
-                'type' => 'text',
-                'content' => $text,
-                'position' => ['start' => $this->position - strlen($text), 'end' => $this->position],
-            ];
-        }
-
-        // 解析模板
-        return $this->parseTemplate();
-    }
-
-    private function parseTemplate(): ?array
-    {
-        $start = $this->position;
-        $this->position += 2; // 跳过 '{{'
-
-        $braceCount = 1;
-        $templateContent = '';
-
-        while ($this->position < $this->length && $braceCount > 0) {
-            $char = $this->content[$this->position];
-            $nextChar = $this->position + 1 < $this->length ? $this->content[$this->position + 1] : '';
-
-            if ($char === '{' && $nextChar === '{') {
-                $braceCount++;
-                $templateContent .= '{{';
-                $this->position += 2;
-            } elseif ($char === '}' && $nextChar === '}') {
-                $braceCount--;
-                if ($braceCount > 0) {
-                    $templateContent .= '}}';
-                }
-                $this->position += 2;
-            } else {
-                $templateContent .= $char;
-                $this->position++;
-            }
-        }
-
-        if ($braceCount > 0) {
-            // 未闭合的模板,当作普通文本处理
-            $this->position = $start + 2;
-
-            return [
-                'type' => 'text',
-                'content' => '{{',
-                'position' => ['start' => $start, 'end' => $start + 2],
-            ];
-        }
-
-        return [
-            'type' => 'template',
-            'content' => trim($templateContent),
-            'raw' => '{{'.$templateContent.'}}',
-            'position' => ['start' => $start, 'end' => $this->position],
-        ];
-    }
-}

+ 0 - 24
api-v13/app/Services/Template/TextNode.php

@@ -1,24 +0,0 @@
-<?php
-
-namespace App\Services\Template;
-
-class TextNode extends ContentNode
-{
-    public string $content;
-
-    public function __construct(string $content, array $position = [])
-    {
-        $this->type = 'text';
-        $this->content = $content;
-        $this->position = $position;
-    }
-
-    public function toArray(): array
-    {
-        return [
-            'type' => $this->type,
-            'content' => $this->content,
-            'position' => $this->position,
-        ];
-    }
-}

+ 0 - 76
api-v13/app/Services/TemplateRender.php

@@ -1,76 +0,0 @@
-<?php
-
-namespace App\Services;
-
-use App\Services\Templates\ConfidenceTemplate;
-use App\Services\Templates\NissayaTemplate;
-use App\Services\Templates\NoteTemplate;
-use App\Services\Templates\TemplateInterface;
-use App\Services\Templates\TermTemplate;
-use InvalidArgumentException;
-
-class TemplateRender
-{
-    protected string $templateName;
-
-    protected array $params = [];
-
-    protected TemplateInterface $template;
-
-    // 定义默认公共参数
-    protected array $options = [
-        'format' => 'react', // 默认格式为 react
-    ];
-
-    public static function name(string $templateName): self
-    {
-        $instance = new self;
-        $instance->templateName = $templateName;
-
-        return $instance;
-    }
-
-    public function param(array $params): self
-    {
-        $this->params = $params;
-
-        return $this;
-    }
-
-    public function options(array $options): self
-    {
-        $this->options = $options;
-
-        return $this;
-    }
-
-    public function render(): array
-    {
-        // 解析模板类
-        $this->resolveTemplate();
-
-        // 设置参数并渲染
-        return $this->template
-            ->setParams($this->params)
-            ->setOptions($this->options)
-            ->render();
-    }
-
-    protected function resolveTemplate(): void
-    {
-        // 模板名称到类的映射(可以用配置文件替代)
-        $templateMap = [
-            'cf' => ConfidenceTemplate::class,
-            'nissaya' => NissayaTemplate::class,
-            'term' => TermTemplate::class,
-            'note' => NoteTemplate::class,
-        ];
-
-        if (! isset($templateMap[$this->templateName])) {
-            throw new InvalidArgumentException("Template {$this->templateName} not found.");
-        }
-
-        // 通过服务容器解析模板类
-        $this->template = app($templateMap[$this->templateName]);
-    }
-}

+ 0 - 37
api-v13/app/Services/Templates/AbstractTemplate.php

@@ -1,37 +0,0 @@
-<?php
-
-namespace App\Services\Templates;
-
-abstract class AbstractTemplate implements TemplateInterface
-{
-    protected array $params = [];
-
-    protected array $options = [];
-
-    public function setParams(array $params): self
-    {
-        $this->params = $params;
-
-        return $this;
-    }
-
-    public function setOptions(array $options): self
-    {
-        $this->options = $options;
-
-        return $this;
-    }
-
-    protected function getParam(string $name, int $id, string $default = ''): string
-    {
-        if (isset($this->params[$name])) {
-            return trim($this->params[$name]);
-        } elseif (isset($this->params["{$id}"])) {
-            return trim($this->params["{$id}"]);
-        } else {
-            return $default;
-        }
-    }
-
-    abstract public function render(): array;
-}

+ 0 - 43
api-v13/app/Services/Templates/ConfidenceTemplate.php

@@ -1,43 +0,0 @@
-<?php
-
-namespace App\Services\Templates;
-
-class ConfidenceTemplate extends AbstractTemplate
-{
-    public function render(): array
-    {
-        // 示例:处理 email 模板的渲染逻辑
-        $value = $this->getParam('value', 1, '0');
-
-        // 根据 format 调整渲染逻辑
-        $props = ['value' => $value];
-        $output = [];
-        switch ($this->options['format']) {
-            case 'react':
-                $output = [
-                    'props' => base64_encode(\json_encode($props)),
-                    'html' => '',
-                    'tag' => 'span',
-                    'tpl' => 'cf',
-                ];
-                break;
-            case 'unity':
-                $output = [
-                    'props' => base64_encode(\json_encode($props)),
-                    'tpl' => 'cf',
-                ];
-                break;
-            case 'markdown':
-                $output = ["`$value`"];
-                break;
-            case 'html':
-                $output = ["<span>{$value}</span>"];
-                break;
-            default:
-                $output = [$value];
-                break;
-        }
-
-        return $output;
-    }
-}

+ 0 - 54
api-v13/app/Services/Templates/NissayaTemplate.php

@@ -1,54 +0,0 @@
-<?php
-
-namespace App\Services\Templates;
-
-use App\Tools\Tools;
-
-class NissayaTemplate extends AbstractTemplate
-{
-    public function render(): array
-    {
-
-        $pali = $this->getParam('pali', 1);
-        $meaning = $this->getParam('meaning', 2);
-        $innerString = '';
-        $props = [
-            'pali' => $pali,
-            'meaning' => $meaning,
-            'lang' => $this->options['lang'],
-        ];
-        switch ($this->options['format']) {
-            case 'react':
-                $output = [
-                    'props' => base64_encode(\json_encode($props)),
-                    'html' => $innerString,
-                    'tag' => 'span',
-                    'tpl' => 'nissaya',
-                ];
-                break;
-            case 'unity':
-                $output = [
-                    'props' => base64_encode(\json_encode($props)),
-                    'tpl' => 'nissaya',
-                ];
-                break;
-            case 'text':
-                $output = $pali.'၊'.$meaning;
-                break;
-            case 'tex':
-                $output = $pali.'၊'.$meaning;
-                break;
-            case 'simple':
-                $output = $pali.'၊'.$meaning;
-                break;
-            case 'prompt':
-                $output = Tools::MyToRm($pali).':'.$meaning;
-                break;
-            default:
-                $output = $pali.'၊'.$meaning;
-                break;
-        }
-
-        return $output;
-    }
-}

+ 0 - 116
api-v13/app/Services/Templates/NoteTemplate.php

@@ -1,116 +0,0 @@
-<?php
-
-namespace App\Services\Templates;
-
-use App\Http\Api\MdRender;
-
-class NoteTemplate extends AbstractTemplate
-{
-    public function __construct() {}
-
-    public function render(): array
-    {
-        $note = $this->getParam('text', 1);
-        $trigger = $this->getParam('trigger', 2);
-        $props = ['note' => $note];
-        $innerString = '';
-        if (! empty($trigger)) {
-            $props['trigger'] = $trigger;
-            $innerString = $props['trigger'];
-        }
-        if ($this->options['format'] === 'unity') {
-            $props['note'] = MdRender::render(
-                $props['note'],
-                $this->options['channel_id'],
-                null,
-                'read',
-                'translation',
-                'markdown',
-                'unity'
-            );
-        }
-        $output = $note;
-        switch ($this->options['format']) {
-            case 'react':
-                $output = [
-                    'props' => base64_encode(\json_encode($props)),
-                    'html' => $innerString,
-                    'tag' => 'span',
-                    'tpl' => 'note',
-                ];
-                break;
-            case 'unity':
-                $output = [
-                    'props' => base64_encode(\json_encode($props)),
-                    'tpl' => 'note',
-                ];
-                break;
-            case 'html':
-                if (isset($GLOBALS['note_sn'])) {
-                    $GLOBALS['note_sn']++;
-                } else {
-                    $GLOBALS['note_sn'] = 1;
-                    $GLOBALS['note'] = [];
-                }
-                $GLOBALS['note'][] = [
-                    'sn' => $GLOBALS['note_sn'],
-                    'trigger' => $trigger,
-                    'content' => MdRender::render(
-                        $props['note'],
-                        $this->options['channel_id'],
-                        null,
-                        'read',
-                        'translation',
-                        'markdown',
-                        'html'
-                    ),
-                ];
-
-                $link = "<a href='#footnote-".$GLOBALS['note_sn']."' name='note-".$GLOBALS['note_sn']."'>";
-                if (empty($trigger)) {
-                    $output = $link.'<sup>['.$GLOBALS['note_sn'].']</sup></a>';
-                } else {
-                    $output = $link.$trigger.'</a>';
-                }
-                break;
-            case 'text':
-                $output = $trigger;
-                break;
-            case 'tex':
-                $output = $trigger;
-                break;
-            case 'simple':
-                $output = '';
-                break;
-            case 'markdown':
-                if (isset($GLOBALS['note_sn'])) {
-                    $GLOBALS['note_sn']++;
-                } else {
-                    $GLOBALS['note_sn'] = 1;
-                    $GLOBALS['note'] = [];
-                }
-                $content = MdRender::render(
-                    $props['note'],
-                    $this->options['channel_id'],
-                    null,
-                    'read',
-                    'translation',
-                    'markdown',
-                    'markdown'
-                );
-                $output = '[^'.$GLOBALS['note_sn'].']';
-                $GLOBALS['note'][] = [
-                    'sn' => $GLOBALS['note_sn'],
-                    'trigger' => $trigger,
-                    'content' => $content,
-                ];
-                // $output = '<footnote id="'.$GLOBALS['note_sn'].'">'.$content.'</footnote>';
-                break;
-            default:
-                $output = '';
-                break;
-        }
-
-        return $output;
-    }
-}

+ 0 - 12
api-v13/app/Services/Templates/TemplateInterface.php

@@ -1,12 +0,0 @@
-<?php
-
-namespace App\Services\Templates;
-
-interface TemplateInterface
-{
-    public function setParams(array $params): self;
-
-    public function setOptions(array $options): self;
-
-    public function render(): array;
-}

+ 0 - 270
api-v13/app/Services/Templates/TermTemplate.php

@@ -1,270 +0,0 @@
-<?php
-
-namespace App\Services\Templates;
-
-use App\Http\Api\ChannelApi;
-use App\Http\Api\MdRender;
-use App\Models\Channel;
-use App\Models\DhammaTerm;
-use Illuminate\Support\Facades\Log;
-use Illuminate\Support\Str;
-
-class TermTemplate extends AbstractTemplate
-{
-    protected $glossaryKey = 'glossary';
-
-    public function render(): array
-    {
-        $word = $this->getParam('word', 1);
-
-        $props = $this->getTermProps($word, '');
-
-        $output = $props['word'];
-        switch ($this->options['format']) {
-            case 'react':
-                $output = [
-                    'props' => base64_encode(\json_encode($props)),
-                    'html' => $props['innerHtml'],
-                    'tag' => 'span',
-                    'tpl' => 'term',
-                ];
-                break;
-            case 'unity':
-                $output = [
-                    'props' => base64_encode(\json_encode($props)),
-                    'tpl' => 'term',
-                ];
-                break;
-            case 'html':
-                if (isset($props['meaning'])) {
-                    $GLOBALS[$this->glossaryKey][$props['word']] = $props['meaning'];
-
-                    $key = 'term-'.$props['word'];
-                    $termHead = "<a href='#'>".$props['meaning'].'</a>';
-
-                    if (isset($GLOBALS[$key])) {
-                        $output = $termHead;
-                    } else {
-                        $GLOBALS[$key] = 1;
-                        $output = $termHead.'(<em>'.$props['word'].'</em>)';
-                    }
-                } else {
-                    $output = $props['word'];
-                }
-                break;
-            case 'text':
-                if (isset($props['meaning'])) {
-                    $key = 'term-'.$props['word'];
-                    if (isset($GLOBALS[$key])) {
-                        $output = $props['meaning'];
-                    } else {
-                        $GLOBALS[$key] = 1;
-                        $output = $props['meaning'].'('.$props['word'].')';
-                    }
-                } else {
-                    $output = $props['word'];
-                }
-                break;
-            case 'tex':
-                if (isset($props['meaning'])) {
-                    $key = 'term-'.$props['word'];
-                    if (isset($GLOBALS[$key])) {
-                        $output = $props['meaning'];
-                    } else {
-                        $GLOBALS[$key] = 1;
-                        $output = $props['meaning'].'('.$props['word'].')';
-                    }
-                } else {
-                    $output = $props['word'];
-                }
-                break;
-            case 'simple':
-                if (isset($props['meaning'])) {
-                    $output = $props['meaning'];
-                } else {
-                    $output = $props['word'];
-                }
-                break;
-            case 'markdown':
-                if (isset($props['meaning'])) {
-                    $key = 'term-'.$props['word'];
-                    if (isset($GLOBALS[$key]) && $GLOBALS[$key] === 1) {
-                        $GLOBALS[$key]++;
-                        $output = $props['meaning'];
-                    } else {
-                        $GLOBALS[$key] = 1;
-                        $output = $props['meaning'].'('.$props['word'].')';
-                    }
-                } else {
-                    $output = $props['word'];
-                }
-                // 如果有内容且第一次出现,显示为脚注
-                if (! empty($props['note']) && $GLOBALS[$key] === 1) {
-                    if (isset($GLOBALS['note_sn'])) {
-                        $GLOBALS['note_sn']++;
-                    } else {
-                        $GLOBALS['note_sn'] = 1;
-                        $GLOBALS['note'] = [];
-                    }
-                    $content = $props['note'];
-                    $output .= '[^'.$GLOBALS['note_sn'].']';
-                    $GLOBALS['note'][] = [
-                        'sn' => $GLOBALS['note_sn'],
-                        'trigger' => '',
-                        'content' => $content,
-                    ];
-                }
-                break;
-            default:
-                if (isset($props['meaning'])) {
-                    $output = $props['meaning'];
-                } else {
-                    $output = $props['word'];
-                }
-                break;
-        }
-
-        return $output;
-    }
-
-    public function getTermProps($word, $tag = null, $channel = null)
-    {
-        if ($channel && ! empty($channel)) {
-            $channelId = $channel;
-        } else {
-            if (count($this->options['channel_id']) > 0) {
-                $channelId = $this->options['channel_id'][0];
-            } else {
-                $channelId = null;
-            }
-        }
-
-        if (count($this->options['channelInfo']) === 0) {
-            if (! empty($channel)) {
-                $channelInfo = Channel::where('uid', $channel)->first();
-                if (! $channelInfo) {
-                    unset($channelInfo);
-                }
-            }
-            if (! isset($channelInfo)) {
-                Log::warning('channel is null');
-                $output = [
-                    'word' => $word,
-                    'innerHtml' => '',
-                ];
-
-                return $output;
-            }
-        } else {
-            $channelInfo = $this->options['channelInfo'][0];
-        }
-
-        if (Str::isUuid($channelId)) {
-            $lang = Channel::where('uid', $channelId)->value('lang');
-            if (! empty($lang)) {
-                $langFamily = explode('-', $lang)[0];
-            } else {
-                $langFamily = 'zh';
-            }
-            Log::info("term:{$word} 先查属于这个channel 的", 'term');
-            Log::info('channel id'.$channelId, 'term');
-            $table = DhammaTerm::where('word', $word)
-                ->where('channal', $channelId);
-            if ($tag && ! empty($tag)) {
-                $table = $table->where('tag', $tag);
-            }
-            $tplParam = $table->orderBy('updated_at', 'desc')
-                ->first();
-            $studioId = $channelInfo->owner_uid;
-        } else {
-            $tplParam = false;
-            $lang = '';
-            $langFamily = '';
-            $studioId = $this->options['studioId'];
-        }
-
-        if (! $tplParam) {
-            if (Str::isUuid($studioId)) {
-                /**
-                 * 没有,再查这个studio的
-                 * 按照语言过滤
-                 * 完全匹配的优先
-                 * 语族匹配也行
-                 */
-                Log::debug('没有-再查这个studio的', 'term');
-                $table = DhammaTerm::where('word', $word);
-                if (! empty($tag)) {
-                    $table = $table->where('tag', $tag);
-                }
-                $termsInStudio = $table->where('owner', $channelInfo->owner_uid)
-                    ->orderBy('updated_at', 'desc')
-                    ->get();
-                if (count($termsInStudio) > 0) {
-                    $list = [];
-                    foreach ($termsInStudio as $key => $term) {
-                        if (empty($term->channal)) {
-                            if ($term->language === $lang) {
-                                $list[$term->guid] = 2;
-                            } elseif (strpos($term->language, $langFamily) !== false) {
-                                $list[$term->guid] = 1;
-                            }
-                        }
-                    }
-                    if (count($list) > 0) {
-                        arsort($list);
-                        foreach ($list as $key => $one) {
-                            foreach ($termsInStudio as $term) {
-                                if ($term->guid === $key) {
-                                    $tplParam = $term;
-                                    break;
-                                }
-                            }
-                            break;
-                        }
-                    }
-                }
-            }
-        }
-
-        if (! $tplParam) {
-            Log::debug('没有,再查社区', 'term');
-            $community_channel = ChannelApi::getSysChannel('_community_term_zh-hans_');
-            $table = DhammaTerm::where('word', $word);
-            if (! empty($tag)) {
-                $table = $table->where('tag', $tag);
-            }
-            $tplParam = $table->where('channal', $community_channel)
-                ->first();
-            if ($tplParam) {
-                $isCommunity = true;
-            } else {
-                Log::debug('查社区没有', 'term');
-            }
-        }
-        $output = [
-            'word' => $word,
-            'parentChannelId' => $channelId,
-            'parentStudioId' => $channelInfo->owner_uid,
-        ];
-        $innerString = $output['word'];
-        if ($tplParam) {
-            $output['id'] = $tplParam->guid;
-            $output['meaning'] = $tplParam->meaning;
-            $output['channel'] = $tplParam->channal;
-            if (! empty($tplParam->note)) {
-                $mdRender = new MdRender(['format' => $this->options['format']]);
-                $output['note'] = $mdRender->convert($tplParam->note, $this->options['channel_id']);
-            }
-            if (isset($isCommunity)) {
-                $output['isCommunity'] = true;
-            }
-            $innerString = "{$output['meaning']}({$output['word']})";
-            if (! empty($tplParam->other_meaning)) {
-                $output['meaning2'] = $tplParam->other_meaning;
-            }
-        }
-        $output['innerHtml'] = $innerString;
-
-        return $output;
-    }
-}