1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DH\Adf\Exporter\Html; |
6
|
|
|
|
7
|
|
|
use DH\Adf\Exporter\ExporterInterface; |
8
|
|
|
use DH\Adf\Exporter\Html\Block\BlockquoteExporter; |
9
|
|
|
use DH\Adf\Exporter\Html\Block\BulletListExporter; |
10
|
|
|
use DH\Adf\Exporter\Html\Block\CodeBlockExporter; |
11
|
|
|
use DH\Adf\Exporter\Html\Block\DocumentExporter; |
12
|
|
|
use DH\Adf\Exporter\Html\Block\ExpandExporter; |
13
|
|
|
use DH\Adf\Exporter\Html\Block\HeadingExporter; |
14
|
|
|
use DH\Adf\Exporter\Html\Block\MediaGroupExporter; |
15
|
|
|
use DH\Adf\Exporter\Html\Block\MediaSingleExporter; |
16
|
|
|
use DH\Adf\Exporter\Html\Block\OrderedListExporter; |
17
|
|
|
use DH\Adf\Exporter\Html\Block\PanelExporter; |
18
|
|
|
use DH\Adf\Exporter\Html\Block\ParagraphExporter; |
19
|
|
|
use DH\Adf\Exporter\Html\Block\RuleExporter; |
20
|
|
|
use DH\Adf\Exporter\Html\Block\TableExporter; |
21
|
|
|
use DH\Adf\Exporter\Html\Child\ListItemExporter; |
22
|
|
|
use DH\Adf\Exporter\Html\Child\MediaExporter; |
23
|
|
|
use DH\Adf\Exporter\Html\Child\TableCellExporter; |
24
|
|
|
use DH\Adf\Exporter\Html\Child\TableHeaderExporter; |
25
|
|
|
use DH\Adf\Exporter\Html\Child\TableRowExporter; |
26
|
|
|
use DH\Adf\Exporter\Html\Inline\DateExporter; |
27
|
|
|
use DH\Adf\Exporter\Html\Inline\EmojiExporter; |
28
|
|
|
use DH\Adf\Exporter\Html\Inline\HardbreakExporter; |
29
|
|
|
use DH\Adf\Exporter\Html\Inline\InlineCardExporter; |
30
|
|
|
use DH\Adf\Exporter\Html\Inline\MentionExporter; |
31
|
|
|
use DH\Adf\Exporter\Html\Inline\StatusExporter; |
32
|
|
|
use DH\Adf\Exporter\Html\Inline\TextExporter; |
33
|
|
|
use DH\Adf\Exporter\Html\Mark\CodeExporter; |
34
|
|
|
use DH\Adf\Exporter\Html\Mark\EmExporter; |
35
|
|
|
use DH\Adf\Exporter\Html\Mark\LinkExporter; |
36
|
|
|
use DH\Adf\Exporter\Html\Mark\StrikeExporter; |
37
|
|
|
use DH\Adf\Exporter\Html\Mark\StrongExporter; |
38
|
|
|
use DH\Adf\Exporter\Html\Mark\SubsupExporter; |
39
|
|
|
use DH\Adf\Exporter\Html\Mark\TextColorExporter; |
40
|
|
|
use DH\Adf\Exporter\Html\Mark\UnderlineExporter; |
41
|
|
|
use DH\Adf\Node\Block\Blockquote; |
42
|
|
|
use DH\Adf\Node\Block\BulletList; |
43
|
|
|
use DH\Adf\Node\Block\CodeBlock; |
44
|
|
|
use DH\Adf\Node\Block\Document; |
45
|
|
|
use DH\Adf\Node\Block\Expand; |
46
|
|
|
use DH\Adf\Node\Block\Heading; |
47
|
|
|
use DH\Adf\Node\Block\MediaGroup; |
48
|
|
|
use DH\Adf\Node\Block\MediaSingle; |
49
|
|
|
use DH\Adf\Node\Block\OrderedList; |
50
|
|
|
use DH\Adf\Node\Block\Panel; |
51
|
|
|
use DH\Adf\Node\Block\Paragraph; |
52
|
|
|
use DH\Adf\Node\Block\Rule; |
53
|
|
|
use DH\Adf\Node\Block\Table; |
54
|
|
|
use DH\Adf\Node\BlockNode; |
55
|
|
|
use DH\Adf\Node\Child\ListItem; |
56
|
|
|
use DH\Adf\Node\Child\Media; |
57
|
|
|
use DH\Adf\Node\Child\TableCell; |
58
|
|
|
use DH\Adf\Node\Child\TableHeader; |
59
|
|
|
use DH\Adf\Node\Child\TableRow; |
60
|
|
|
use DH\Adf\Node\Inline\Date; |
61
|
|
|
use DH\Adf\Node\Inline\Emoji; |
62
|
|
|
use DH\Adf\Node\Inline\Hardbreak; |
63
|
|
|
use DH\Adf\Node\Inline\InlineCard; |
64
|
|
|
use DH\Adf\Node\Inline\Mention; |
65
|
|
|
use DH\Adf\Node\Inline\Status; |
66
|
|
|
use DH\Adf\Node\Inline\Text; |
67
|
|
|
use DH\Adf\Node\InlineNode; |
68
|
|
|
use DH\Adf\Node\Mark\Code; |
69
|
|
|
use DH\Adf\Node\Mark\Em; |
70
|
|
|
use DH\Adf\Node\Mark\Link; |
71
|
|
|
use DH\Adf\Node\Mark\Strike; |
72
|
|
|
use DH\Adf\Node\Mark\Strong; |
73
|
|
|
use DH\Adf\Node\Mark\Subsup; |
74
|
|
|
use DH\Adf\Node\Mark\TextColor; |
75
|
|
|
use DH\Adf\Node\Mark\Underline; |
76
|
|
|
use DH\Adf\Node\Node; |
77
|
|
|
|
78
|
|
|
abstract class HtmlExporter implements ExporterInterface |
79
|
|
|
{ |
80
|
|
|
public const NODE_MAPPING = [ |
81
|
|
|
// block nodes |
82
|
|
|
Document::class => DocumentExporter::class, |
83
|
|
|
Blockquote::class => BlockquoteExporter::class, |
84
|
|
|
BulletList::class => BulletListExporter::class, |
85
|
|
|
CodeBlock::class => CodeBlockExporter::class, |
86
|
|
|
Heading::class => HeadingExporter::class, |
87
|
|
|
MediaGroup::class => MediaGroupExporter::class, |
88
|
|
|
MediaSingle::class => MediaSingleExporter::class, |
89
|
|
|
OrderedList::class => OrderedListExporter::class, |
90
|
|
|
Panel::class => PanelExporter::class, |
91
|
|
|
Paragraph::class => ParagraphExporter::class, |
92
|
|
|
Rule::class => RuleExporter::class, |
93
|
|
|
Table::class => TableExporter::class, |
94
|
|
|
Expand::class => ExpandExporter::class, |
95
|
|
|
|
96
|
|
|
// child nodes |
97
|
|
|
ListItem::class => ListItemExporter::class, |
98
|
|
|
TableCell::class => TableCellExporter::class, |
99
|
|
|
TableHeader::class => TableHeaderExporter::class, |
100
|
|
|
TableRow::class => TableRowExporter::class, |
101
|
|
|
Media::class => MediaExporter::class, |
102
|
|
|
InlineCard::class => InlineCardExporter::class, |
103
|
|
|
|
104
|
|
|
// inline nodes |
105
|
|
|
Emoji::class => EmojiExporter::class, |
106
|
|
|
Hardbreak::class => HardbreakExporter::class, |
107
|
|
|
Mention::class => MentionExporter::class, |
108
|
|
|
Text::class => TextExporter::class, |
109
|
|
|
Status::class => StatusExporter::class, |
110
|
|
|
Date::class => DateExporter::class, |
111
|
|
|
|
112
|
|
|
// mark nodes |
113
|
|
|
Em::class => EmExporter::class, |
114
|
|
|
Strong::class => StrongExporter::class, |
115
|
|
|
Code::class => CodeExporter::class, |
116
|
|
|
Strike::class => StrikeExporter::class, |
117
|
|
|
Subsup::class => SubsupExporter::class, |
118
|
|
|
Underline::class => UnderlineExporter::class, |
119
|
|
|
Link::class => LinkExporter::class, |
120
|
|
|
TextColor::class => TextColorExporter::class, |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
protected Node $node; |
124
|
|
|
protected array $tags = []; |
125
|
|
|
|
126
|
|
|
public function __construct(Node $node) |
127
|
|
|
{ |
128
|
|
|
$this->node = $node; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function export(): string |
132
|
|
|
{ |
133
|
|
|
$outputs = []; |
134
|
|
|
|
135
|
|
|
if ($this->node instanceof BlockNode) { |
136
|
|
|
// $node has children (content) => iterate over them |
137
|
|
|
foreach ($this->node->getContent() as $child) { |
|
|
|
|
138
|
|
|
$class = self::NODE_MAPPING[$child::class]; |
139
|
|
|
$exporter = new $class($child); |
140
|
|
|
$outputs[] = $exporter->export(); |
141
|
|
|
} |
142
|
|
|
} elseif ($this->node instanceof InlineNode) { |
143
|
|
|
// $node doesn't have children but can have marks |
144
|
|
|
$class = self::NODE_MAPPING[$this->node::class]; |
145
|
|
|
$exporter = new $class($this->node); |
146
|
|
|
$outputs[] = $exporter->export(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$output = implode('', $outputs); |
150
|
|
|
|
151
|
|
|
if (0 === \count($this->tags)) { |
152
|
|
|
// no wrapping tags |
153
|
|
|
return $output; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (1 === \count($this->tags)) { |
157
|
|
|
// no closing tag |
158
|
|
|
return $this->tags[0].$output; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// opening and closing tags |
162
|
|
|
return $this->tags[0].$output.$this->tags[1]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|