-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_templates.php
More file actions
159 lines (131 loc) · 3.97 KB
/
Copy pathupdate_templates.php
File metadata and controls
159 lines (131 loc) · 3.97 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
<?php
// translated from following pythong script:
// import os
//import re
//from glob import glob
//from mdpo.md2po import markdown_to_pofile
//
//def filter_anchors(self, msgid, *args):
// if re.match(r'<a name="(.+?)"></a>', msgid) is not None:
// self.disable_next_block = True
//
//print('Remove old pot files...')
//for file in glob('templates/*', recursive=True):
// if not os.path.isdir(file):
// os.remove(file)
//
//mdFiles = set([
// re.match(r'docs/[^/]+?/(.+\.md)', file).group(1)
// for file in glob('docs/**/*.md')
//])
//
//for file in mdFiles:
// if file == 'readme.md' or file == 'license.md':
// continue
//
// print(f'Processing {file}')
//
// pot_path = f'templates/{file[:-3]}.pot'
//
// os.makedirs(os.path.dirname(pot_path), exist_ok=True)
//
// markdown_to_pofile(
// f'docs/**/{file}',
// po_filepath=pot_path,
// wrapwidth=0,
// save=True,
// include_codeblocks=True,
// events={ 'msgid': filter_anchors }
// )
// The markdown_to_pofile should be replaced with a call to `babela-fisho` binary invoke
use Gettext\Generator\PoGenerator;
use Gettext\Loader\PoLoader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
use function Termwind\render;
require __DIR__ . '/vendor/autoload.php';
$files= new Finder()->files()->in(__DIR__ . '/docs/');
if (! $files->hasResults()) {
render(<<<'HTML'
<div class="py-1">
<span class="text-red-500 px-1 mr-1">ERROR</span>
<span>There are no markdown files to process.</span>
</div>
HTML);
}
$markdownFiles = [];
foreach ($files as $file) {
$markdownFilename = $file->getFilename();
if (in_array($markdownFilename, ['readme.md', 'license.md'])) {
continue;
}
if (! array_key_exists($markdownFilename, $markdownFiles)) {
$markdownFiles[$markdownFilename] = [];
}
$markdownFiles[$markdownFilename][] = $file->getRelativePathname();
}
// sort by versions:
// 8.x => 9.x => 10.x => 11.x => 12.x => 13.x => 14.x...
// master should be the last one
foreach ($markdownFiles as $filename => $files) {
usort($files, function ($a, $b) {
$a = explode('/', $a, 2)[0];
$b = explode('/', $b, 2)[0];
if ($a === 'master') {
return 1;
}
if ($b === 'master') {
return -1;
}
return version_compare($a, $b);
});
$markdownFiles[$filename] = $files;
}
ksort($markdownFiles);
$poLoader = new PoLoader();
$poGenerator = new PoGenerator();
foreach ($markdownFiles as $filename => $files) {
$potPath = __DIR__ . '/templates/' . pathinfo($filename, PATHINFO_FILENAME) . '.pot';
if (! is_dir(dirname($potPath))) {
mkdir(dirname($potPath), 0755, true);
}
$process = new Process([
__DIR__ . '/bin/babela-fisho',
'to-po',
...array_map(
fn ($file) => './docs/' . $file,
$files
),
]);
$process->run();
if (! $process->isSuccessful()) {
render(<<<'HTML'
<div class="py-1">
<span class="text-red-500 px-1 mr-1">ERROR</span>
<span>Failed to process the markdown files.</span>
</div>
HTML);
}
$potFileContent = $process->getOutput();
// filter out anchors
/** @var \Gettext\Translations|\Gettext\Translation[] $translations */
$translations = $poLoader->loadString($potFileContent);
foreach ($translations as $translation) {
if (preg_match('/<a name="(.+?)"><\/a>/', $translation->getOriginal())) {
$translation->disable(true);
}
}
$poGenerator->generateFile($translations, $potPath);
render(<<<HTML
<div>
<span class="px-1 text-green">✓</span>
<span>Processed <strong>{$filename}</strong></span>
</div>
HTML);
}
render(<<<'HTML'
<div class="py-1">
<span class="px-1 text-green">✓</span>
<span>All markdown files have been processed.</span>
</div>
HTML);