-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen.php
More file actions
233 lines (220 loc) · 7.9 KB
/
gen.php
File metadata and controls
233 lines (220 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Generator script for microhtml.php
*
* This script generates the src/microhtml.php file by combining:
* 1. Core classes from src/classes.php
* 2. Generated HTML element functions based on the $elements definition
*/
enum ElementType
{
case Normal;
case Void;
}
// Define all HTML elements organized by category
$elements = [
'https://developer.mozilla.org/en-US/docs/Web/HTML/Element' => [
'Main root' => [
'HTML' => ElementType::Normal,
],
'Document metadata' => [
'BASE' => ElementType::Void,
'HEAD' => ElementType::Normal,
'LINK' => ElementType::Void,
'META' => ElementType::Void,
'STYLE' => ElementType::Normal,
'TITLE' => ElementType::Normal,
],
'Sectioning root' => [
'BODY' => ElementType::Normal,
],
'Content sectioning' => [
'ADDRESS' => ElementType::Normal,
'ARTICLE' => ElementType::Normal,
'ASIDE' => ElementType::Normal,
'FOOTER' => ElementType::Normal,
'HEADER' => ElementType::Normal,
'H1' => ElementType::Normal,
'H2' => ElementType::Normal,
'H3' => ElementType::Normal,
'H4' => ElementType::Normal,
'H5' => ElementType::Normal,
'H6' => ElementType::Normal,
'HGROUP' => ElementType::Normal,
'MAIN' => ElementType::Normal,
'NAV' => ElementType::Normal,
'SECTION' => ElementType::Normal,
],
'Text content' => [
'BLOCKQUOTE' => ElementType::Normal,
'DD' => ElementType::Normal,
'DIR' => ElementType::Normal,
'DIV' => ElementType::Normal,
'DL' => ElementType::Normal,
'DT' => ElementType::Normal,
'FIGCAPTION' => ElementType::Normal,
'FIGURE' => ElementType::Normal,
'HR' => ElementType::Void,
'LI' => ElementType::Normal,
'OL' => ElementType::Normal,
'P' => ElementType::Normal,
'PRE' => ElementType::Normal,
'UL' => ElementType::Normal,
],
'Inline text semantics' => [
'A' => ElementType::Normal,
'ABBR' => ElementType::Normal,
'B' => ElementType::Normal,
'BDI' => ElementType::Normal,
'BDO' => ElementType::Normal,
'BR' => ElementType::Void,
'CITE' => ElementType::Normal,
'CODE' => ElementType::Normal,
'DATA' => ElementType::Normal,
'DFN' => ElementType::Normal,
'EM' => ElementType::Normal,
'I' => ElementType::Normal,
'KBD' => ElementType::Normal,
'MARK' => ElementType::Normal,
'Q' => ElementType::Normal,
'RB' => ElementType::Normal,
'RP' => ElementType::Normal,
'RT' => ElementType::Normal,
'RTC' => ElementType::Normal,
'RUBY' => ElementType::Normal,
'S' => ElementType::Normal,
'SAMP' => ElementType::Normal,
'SMALL' => ElementType::Normal,
'SPAN' => ElementType::Normal,
'STRONG' => ElementType::Normal,
'SUB' => ElementType::Normal,
'SUP' => ElementType::Normal,
'TIME' => ElementType::Normal,
'TT' => ElementType::Normal,
'U' => ElementType::Normal,
'VAR_' => ElementType::Normal, // VAR_ because var is a reserved word
'WBR' => ElementType::Void,
],
'Image and multimedia' => [
'AREA' => ElementType::Void,
'AUDIO' => ElementType::Normal,
'IMG' => ElementType::Void,
'MAP' => ElementType::Normal,
'TRACK' => ElementType::Void,
'VIDEO' => ElementType::Normal,
],
'Embedded content' => [
'APPLET' => ElementType::Normal,
'EMBED' => ElementType::Void,
'IFRAME' => ElementType::Normal,
'NOEMBED' => ElementType::Normal,
'OBJECT' => ElementType::Normal,
'PARAM' => ElementType::Void,
'PICTURE' => ElementType::Normal,
'SOURCE' => ElementType::Void,
],
'Scripting' => [
'CANVAS' => ElementType::Normal,
'NOSCRIPT' => ElementType::Normal,
'SCRIPT' => ElementType::Normal,
],
'Demarcating edits' => [
'DEL' => ElementType::Normal,
'INS' => ElementType::Normal,
],
'Table content' => [
'CAPTION' => ElementType::Normal,
'COL' => ElementType::Void,
'COLGROUP' => ElementType::Normal,
'TABLE' => ElementType::Normal,
'TBODY' => ElementType::Normal,
'TD' => ElementType::Normal,
'TFOOT' => ElementType::Normal,
'TH' => ElementType::Normal,
'THEAD' => ElementType::Normal,
'TR' => ElementType::Normal,
],
'Forms' => [
'BUTTON' => ElementType::Normal,
'DATALIST' => ElementType::Normal,
'FIELDSET' => ElementType::Normal,
'FORM' => ElementType::Normal,
'INPUT' => ElementType::Void,
'LABEL' => ElementType::Normal,
'LEGEND' => ElementType::Normal,
'METER' => ElementType::Normal,
'OPTGROUP' => ElementType::Normal,
'OPTION' => ElementType::Normal,
'OUTPUT' => ElementType::Normal,
'PROGRESS' => ElementType::Normal,
'SELECT' => ElementType::Normal,
'TEXTAREA' => ElementType::Normal,
],
'Interactive elements' => [
'DETAILS' => ElementType::Normal,
'DIALOG' => ElementType::Normal,
'SUMMARY' => ElementType::Normal,
],
],
];
$attrsType = 'array<string,string|\Stringable|null|bool|int|float>';
$phpAttrsType = preg_replace('/<.*>/', '', $attrsType);
$childType = '\MicroHTML\HTMLElement|string|\Stringable|null|bool|int|float';
function generateNormalElement(string $funcName, string $tag): string
{
global $attrsType, $phpAttrsType, $childType;
return <<<EOD
/**
* @param $attrsType|$childType \$arg0 HTML attributes or first child
* @param $childType \$args any further children
*/
function $funcName(
$phpAttrsType|$childType \$arg0 = [],
$childType ...\$args,
): HTMLElement {
return new HTMLElement("$tag", [\$arg0, ...\$args]);
}
EOD;
}
function generateSelfClosingElement(string $funcName, string $tag): string
{
global $attrsType, $phpAttrsType;
return <<<EOD
/** @param $attrsType \$attrs */
function $funcName(
$phpAttrsType \$attrs = [],
): SelfClosingHTMLElement {
return new SelfClosingHTMLElement("$tag", \$attrs);
}
EOD;
}
function generateFile(array $elements): string
{
$output = "<?php\n\n";
$output .= "declare(strict_types=1);\n\n";
$output .= "/**\n";
$output .= " * This file is auto-generated. Do not edit directly.\n";
$output .= " * Generated by gen.php\n";
$output .= " */\n\n";
$output .= "namespace MicroHTML;\n\n";
$output .= "require_once __DIR__ . '/classes.php';\n\n";
foreach ($elements as $url => $categories) {
$output .= "# $url";
foreach ($categories as $category => $elems) {
$output .= "\n# $category\n";
foreach ($elems as $funcName => $type) {
$tag = rtrim($funcName, '_');
$tag = strtolower($tag);
if ($type === ElementType::Void) {
$output .= generateSelfClosingElement($funcName, $tag) . "\n";
} else {
$output .= generateNormalElement($funcName, $tag) . "\n";
}
}
}
}
return $output;
}
$content = generateFile($elements);
file_put_contents(__DIR__ . '/src/microhtml.php', $content);
echo "Generated src/microhtml.php successfully!\n";