HtmlRenderer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Services\Template\Renderers;
  3. use Illuminate\Support\Str;
  4. use App\Services\Template\Contracts\RendererInterface;
  5. use App\Services\Template\ParsedDocument;
  6. use App\Services\Template\ContentNode;
  7. use App\Services\Template\TextNode;
  8. use App\Services\Template\MarkdownNode;
  9. use App\Services\Template\TemplateNode;
  10. // ================== HTML 渲染器 ==================
  11. class HtmlRenderer implements RendererInterface
  12. {
  13. private array $templateMappings = [];
  14. public function __construct()
  15. {
  16. $this->initializeTemplateMappings();
  17. }
  18. private function initializeTemplateMappings(): void
  19. {
  20. $this->templateMappings = [
  21. 'note' => function ($params) {
  22. $type = $params['type'] ?? 'info';
  23. $text = $params['text'] ?? '';
  24. $title = $params['title'] ?? '';
  25. $typeClass = match ($type) {
  26. 'warning' => 'alert-warning',
  27. 'error' => 'alert-danger',
  28. 'success' => 'alert-success',
  29. default => 'alert-info'
  30. };
  31. $titleHtml = $title ? "<h6 class='alert-heading'>$title</h6>" : '';
  32. return "<div class='alert $typeClass' role='alert'>$titleHtml$text</div>";
  33. },
  34. 'info' => function ($params) {
  35. $content = $params['content'] ?? '';
  36. $title = $params['title'] ?? '';
  37. $titleHtml = $title ? "<h6 class='alert-heading'>$title</h6>" : '';
  38. return "<div class='alert alert-info' role='alert'>$titleHtml$content</div>";
  39. },
  40. 'warning' => function ($params) {
  41. $message = $params['message'] ?? '';
  42. $title = $params['title'] ?? '';
  43. $titleHtml = $title ? "<h6 class='alert-heading'>$title</h6>" : '';
  44. return "<div class='alert alert-warning' role='alert'>$titleHtml$message</div>";
  45. }
  46. ];
  47. }
  48. public function render(ParsedDocument $document): string
  49. {
  50. $html = '';
  51. foreach ($document->content as $node) {
  52. $html .= $this->renderNode($node);
  53. }
  54. return Str::markdown($html);
  55. }
  56. private function renderNode(ContentNode $node): string
  57. {
  58. switch ($node->type) {
  59. case 'text':
  60. return $this->renderText($node);
  61. case 'markdown':
  62. return $this->renderMarkdown($node);
  63. case 'template':
  64. return $this->renderTemplate($node);
  65. default:
  66. return '';
  67. }
  68. }
  69. public function renderText(TextNode $text): string
  70. {
  71. return htmlspecialchars($text->content, ENT_QUOTES, 'UTF-8');
  72. }
  73. private function renderMarkdown(MarkdownNode $markdown): string
  74. {
  75. return Str::markdown($markdown->content);
  76. // 简单的 Markdown 渲染,实际项目中可以使用 CommonMark 等库
  77. $html = $markdown->content;
  78. // 处理粗体
  79. $html = preg_replace('/\*\*(.*?)\*\*/', '<strong>$1</strong>', $html);
  80. $html = preg_replace('/\*(.*?)\*/', '<em>$1</em>', $html);
  81. // 处理链接
  82. $html = preg_replace('/\[([^\]]+)\]\(([^)]+)\)/', '<a href="$2">$1</a>', $html);
  83. return $html;
  84. }
  85. public function renderTemplate(TemplateNode $template): string
  86. {
  87. if (!isset($this->templateMappings[$template->name])) {
  88. // 未知模板,返回原始内容
  89. return htmlspecialchars($template->raw, ENT_QUOTES, 'UTF-8');
  90. }
  91. $renderer = $this->templateMappings[$template->name];
  92. // 处理参数中的嵌套内容
  93. $processedParams = [];
  94. foreach ($template->parameters as $key => $value) {
  95. if (is_array($value)) {
  96. // 嵌套内容,递归渲染
  97. $nestedHtml = '';
  98. foreach ($value as $childNode) {
  99. $nestedHtml .= $this->renderNode($childNode);
  100. }
  101. $processedParams[$key] = $nestedHtml;
  102. } else {
  103. $processedParams[$key] = $value;
  104. }
  105. }
  106. return $renderer($processedParams);
  107. }
  108. public function registerTemplate(string $name, callable $renderer): void
  109. {
  110. $this->templateMappings[$name] = $renderer;
  111. }
  112. }