-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-glossary
188 lines (136 loc) · 5.56 KB
/
generate-glossary
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
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use Symfony\Component\Finder\SplFileInfo;
$generator = new class
{
public const BASE_DIR = __DIR__;
public const NUMBER_TERMS_PER_SUMMARY_LINE = 5;
protected Filesystem $filesystem;
protected array $files = [];
protected string $summary = '';
protected string $filesContent = '';
public function __construct()
{
$this->filesystem = new Filesystem;
$this->files = $this->filesystem->allFiles(static::BASE_DIR.'/source');
}
public function generateFile()
{
foreach ($this->files as $file) {
$this->processFile($file);
}
$fileContent = "# Glossary\n\n\n";
$fileContent .= $this->summary();
$fileContent .= "\n\n";
$fileContent .= $this->content();
$this->filesystem->put(static::BASE_DIR.'/GLOSSARY.md', $fileContent);
}
private function processFile(SplFileInfo $file)
{
$fileBasename = Str::of($file->getFilename())->basename('.php');
$fileTittle = $fileBasename->title();
$this->summary(sprintf('[**%s**](#%s)', $fileTittle, $fileBasename->slug()), 2);
$this->content("## {$fileTittle}", 2);
$this->processFileTerms($fileBasename, require $file->getPathname());
}
private function processFileTerms(Stringable $fileBasename, array $terms)
{
$termGroups = array_chunk($terms, static::NUMBER_TERMS_PER_SUMMARY_LINE, true);
$this->summary(str_repeat('| ', static::NUMBER_TERMS_PER_SUMMARY_LINE).'|');
$this->summary(str_repeat('| :-----: ', static::NUMBER_TERMS_PER_SUMMARY_LINE).'|');
$summaryTerms = [];
foreach ($termGroups as $terms) {
foreach ($terms as $key => $value) {
$keyString = Str::of($key);
$termTitle = $keyString->title()->replace('_', ' ')->toString();
$summaryTerms[] = sprintf('[%s](#%s)', $termTitle, $keyString->slug());
$this->processTerm($fileBasename, $termTitle, $key, $value);
}
$this->addToSummary($summaryTerms);
}
$this->summary('', 2);
}
private function addToSummary(array &$summaryTerms,)
{
for ($i=0; $i < static::NUMBER_TERMS_PER_SUMMARY_LINE; $i++) {
if (empty($summaryTerms[$i])) {
$summaryTerms[] = ' ';
}
}
if (count($summaryTerms) == static::NUMBER_TERMS_PER_SUMMARY_LINE) {
$this->summary('|'.implode('|', $summaryTerms).'|');
$summaryTerms = [];
}
}
private function processTerm(Stringable $fileBasename, string $termTitle, string $key, string $value)
{
$this->content("### {$termTitle}", 2);
$this->content('<details>');
$this->content("<summary>Click for details and usage of {$termTitle}</summary>", 2);
$this->content("- Key: `'{$key}'`");
$this->content("- Value: `'{$value}'`");
$this->content("- Usage: `'{$fileBasename->toString()}.{$key}'`", 2);
$this->content($this->usage($fileBasename->toString(), $key, $value), 0);
$this->content('</details>', 2);
$this->backTop();
}
private function usage(string $basename, string $key, string $value): string
{
$returnString = '';
preg_match_all('/(:[a-zA-Z_\-]+)/mu', $value, $paramaters);
$paramaters = $paramaters[0];
$returnString = "```php\n";
if (! empty($paramaters)) {
// usage example with string
$paramatersString = ", [\n";
foreach ($paramaters as $paramater) {
$paramaterString = Str::of($paramater);
$paramatersString .= " '{$paramaterString->replaceFirst(':', '')}' => '{$paramaterString->replaceFirst(':', '')}',\n";
}
$paramatersString .= "]";
$returnString .= "trans('{$basename}.{$key}'{$paramatersString});\n\n";
// usage example with variable
$paramatersString = ", [\n";
foreach ($paramaters as $paramater) {
$paramaterString = Str::of($paramater);
$paramatersString .= " '{$paramaterString->replaceFirst(':', '')}' => e({$paramaterString->replaceFirst(':', '$')}),\n";
}
$paramatersString .= "]";
$returnString .= "trans('{$basename}.{$key}'{$paramatersString});\n";
} else {
$returnString .= "trans('{$basename}.{$key}');\n";
}
$returnString .= "```\n\n";
return $returnString;
}
private function backTop()
{
$this->content('[Back to top ^](#glossary)', 2);
}
private function summary(?string $string = null, int $numLineBreack = 1)
{
return $this->concat($string, $numLineBreack, 'summary');
}
private function content(?string $string = null, int $numLineBreack = 1)
{
return $this->concat($string, $numLineBreack, 'filesContent');
}
private function concat(?string $string = null, int $numLineBreack = 1, string $stack = '')
{
if (! isset($this->$stack)) {
throw new InvalidArgumentException('You should append string to either "content" or "summary" stack');
}
if ($string === null) {
return $this->$stack;
}
$this->$stack .= $string;
for ($i=0; $i < $numLineBreack; $i++) {
$this->$stack .= "\n";
}
}
};
$generator->generateFile();