HtmlRenderer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Services\Markup\Render;
  3. use App\Services\Markup\Html\HtmlPolicy;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Ir\Document;
  6. use App\Services\Markup\Ir\Element;
  7. use App\Services\Markup\Ir\Node;
  8. use App\Services\Markup\Ir\Text;
  9. /**
  10. * IR → 静态 HTML(library 阅读器、导出)。
  11. *
  12. * 单段落内容包成 `<span>`(v2 的 replaceSinglePWithSpan);站内绝对路径的图片补上站点地址。
  13. * 模板的 HTML 由各模板的 html() 给出。
  14. */
  15. final class HtmlRenderer implements Renderer
  16. {
  17. public function document(Document $document, RenderContext $ctx): string
  18. {
  19. $html = $this->nodes($document->nodes, $ctx);
  20. return $document->inline && ! $document->fragment && $html !== '' ? "<span>{$html}</span>" : $html;
  21. }
  22. /** 行内安全渲染时要换成 span 的块级标签 */
  23. private const BLOCK_TAGS = [
  24. 'p', 'div', 'blockquote', 'pre', 'ul', 'ol', 'li', 'table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th',
  25. 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'dl', 'dt', 'dd', 'figure', 'figcaption', 'details', 'summary',
  26. ];
  27. private bool $inlineOnly = false;
  28. /**
  29. * 渲染成只含行内元素的 HTML:块级标签换成 `<span class="md-{标签}">`,顶层块之间用 `<br />` 分隔。
  30. *
  31. * 用于嵌在句子中间的内容(脚注 sidenote):块级元素落进 `<p>` / `<span>` 里,浏览器会提前闭合外层,
  32. * 把 sidenote 的结构切断、吞掉后面的正文。
  33. */
  34. public function inlineSafe(Document $document, RenderContext $ctx): string
  35. {
  36. $previous = $this->inlineOnly;
  37. $this->inlineOnly = true;
  38. try {
  39. if ($document->inline) {
  40. return $this->document($document, $ctx);
  41. }
  42. $blocks = array_map(fn (Node $node) => $this->nodes([$node], $ctx), $document->nodes);
  43. $html = implode('<br />', array_filter($blocks, fn (string $html) => $html !== ''));
  44. return $html === '' ? '' : "<span>{$html}</span>";
  45. } finally {
  46. $this->inlineOnly = $previous;
  47. }
  48. }
  49. /**
  50. * @param list<Node> $nodes
  51. */
  52. public function nodes(array $nodes, RenderContext $ctx): string
  53. {
  54. $html = '';
  55. foreach ($nodes as $node) {
  56. $html .= match (true) {
  57. $node instanceof Text => e($node->value, false),
  58. $node instanceof Element => $this->element($node, $ctx),
  59. $node instanceof Call => $node->template->html($node, $ctx),
  60. default => '',
  61. };
  62. }
  63. return $html;
  64. }
  65. private function element(Element $element, RenderContext $ctx): string
  66. {
  67. $attrs = '';
  68. foreach ($element->attrs as $name => $value) {
  69. if ($name === 'src' && str_starts_with($value, '/') && ! str_starts_with($value, '//')) {
  70. $value = rtrim((string) config('app.url'), '/').$value;
  71. }
  72. $attrs .= ' '.$name.'="'.e($value, false).'"';
  73. }
  74. if ($this->inlineOnly && in_array($element->tag, self::BLOCK_TAGS, true)) {
  75. if ($element->tag === 'hr') {
  76. return '<br />';
  77. }
  78. $class = 'md-'.$element->tag.(isset($element->attrs['class']) ? ' '.e($element->attrs['class'], false) : '');
  79. return '<span class="'.$class.'">'.$this->nodes($element->children, $ctx).'</span>';
  80. }
  81. if (in_array($element->tag, [...HtmlPolicy::VOID, 'input'], true)) {
  82. return "<{$element->tag}{$attrs} />";
  83. }
  84. return "<{$element->tag}{$attrs}>".$this->nodes($element->children, $ctx)."</{$element->tag}>";
  85. }
  86. /**
  87. * 文末的脚注列表(footnote 选项打开时)。
  88. */
  89. public function footnotes(RenderContext $ctx): string
  90. {
  91. if ($ctx->footnotes === []) {
  92. return '';
  93. }
  94. $html = '<div><h1>endnote</h1>';
  95. foreach ($ctx->footnotes as $note) {
  96. $html .= '<p><a name="footnote-'.$note['sn'].'">['.$note['sn'].']</a> '.$note['content'].'</p>';
  97. }
  98. return $html.'</div>';
  99. }
  100. }