1
|
|
|
#! /usr/bin/env php |
2
|
|
|
<?php |
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
require_once \dirname(__DIR__) . '/vendor/autoload.php'; |
6
|
|
|
|
7
|
|
|
// Twig bootstrapping |
8
|
|
|
$loader = new Twig_Loader_Filesystem(__DIR__ . '/templates'); |
|
|
|
|
9
|
|
|
$twig = new Twig_Environment($loader, [ |
|
|
|
|
10
|
|
|
'debug' => true, |
11
|
|
|
'charset' => 'utf-8', |
12
|
|
|
'cache' => '/tmp', |
13
|
|
|
'auto_reload' => true, |
14
|
|
|
'strict_variables' => true, |
15
|
|
|
'optimizations' => -1, |
16
|
|
|
]); |
17
|
|
|
$twig->addExtension(new Twig_Extension_Debug()); |
|
|
|
|
18
|
|
|
|
19
|
|
|
$twig->addFunction(new Twig_Function('timestamp', static function () { |
|
|
|
|
20
|
|
|
return \str_replace('+00:00', 'Z', \gmdate(\DATE_ATOM)); |
21
|
|
|
})); |
22
|
|
|
|
23
|
|
|
//------------------------------------------------------------------------------- |
24
|
|
|
|
25
|
|
|
$entities = \json_decode(\file_get_contents(\dirname(__DIR__) . '/resources/entities.json')); |
26
|
|
|
$enumerables = []; |
27
|
|
|
|
28
|
|
|
// Figure out what they key => value should be |
29
|
|
|
foreach ($entities as $entity => $codepoints) { |
30
|
|
|
$enumerables[] = (object) [ |
31
|
|
|
'amp' => \str_replace(['&', ';'], '', $entity), |
32
|
|
|
'codepoint' => (static function () use ($codepoints) { |
33
|
|
|
return \implode('', \array_map(static function ($p) { |
34
|
|
|
return \sprintf( |
35
|
|
|
'&#x%s;', |
36
|
|
|
\mb_strtoupper(\dechex($p)) |
37
|
|
|
); |
38
|
|
|
}, $codepoints->codepoints)); |
39
|
|
|
})(), |
40
|
|
|
'codepoint_x' => (static function () use ($codepoints) { |
41
|
|
|
return \implode('', \array_map(static function ($p) { |
42
|
|
|
return \sprintf( |
43
|
|
|
'\x%s', |
44
|
|
|
\mb_strtoupper(\dechex($p)) |
45
|
|
|
); |
46
|
|
|
}, $codepoints->codepoints)); |
47
|
|
|
})(), |
48
|
|
|
'codepoint_u' => (static function () use ($codepoints) { |
49
|
|
|
return \implode('', \array_map(static function ($p) { |
50
|
|
|
return \sprintf('\u%s', $p); |
51
|
|
|
}, $codepoints->codepoints)); |
52
|
|
|
})(), |
53
|
|
|
'char' => (static function () use ($codepoints) { |
54
|
|
|
return \implode('', \array_map(static function ($p) { |
55
|
|
|
return \addcslashes(IntlChar::chr($p), "\n`\\\""); |
56
|
|
|
}, $codepoints->codepoints)); |
57
|
|
|
})(), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Sort alphabetically, naturally |
62
|
|
|
\usort($enumerables, static function (stdClass $a, stdClass $b) { |
63
|
|
|
return \strcasecmp($a->amp, $b->amp); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
// Make sure that the `=>` signs align in the generated output |
67
|
|
|
$longestKey = \mb_strlen( |
68
|
|
|
\array_reduce($enumerables, static function ($a, stdClass $b) { |
69
|
|
|
return (\mb_strlen($a ?? '') > \mb_strlen($b->amp)) |
70
|
|
|
? $a |
71
|
|
|
: $b->amp; |
72
|
|
|
}) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
foreach ($enumerables as &$enum) { |
76
|
|
|
$enum->padded_amp = \str_pad("'" . $enum->amp . "'", $longestKey + 2, ' ', \STR_PAD_RIGHT); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
//------------------------------------------------------------------------------- |
80
|
|
|
// entities.dtd |
81
|
|
|
|
82
|
|
|
$template = $twig->load('entities.dtd.twig'); |
83
|
|
|
$output = $template->render([ |
84
|
|
|
'entities' => $enumerables, |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$writePath = \sprintf( |
88
|
|
|
'%s/entities.dtd', |
89
|
|
|
\dirname(__DIR__) . '/resources' |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
\file_put_contents($writePath, $output); |
93
|
|
|
|
94
|
|
|
//------------------------------------------------------------------------------- |
95
|
|
|
// Entity.php |
96
|
|
|
|
97
|
|
|
$template = $twig->load('Entity.php.twig'); |
98
|
|
|
$output = $template->render([ |
99
|
|
|
'entities' => $enumerables, |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
$writePath = \sprintf( |
103
|
|
|
'%s/Entity.php', |
104
|
|
|
\dirname(__DIR__) . '/src/Dictionary' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
\file_put_contents($writePath, $output); |
108
|
|
|
|