From 9cf5e08378fadc982787e04de44226b94c1c0c4c Mon Sep 17 00:00:00 2001 From: Michal Sniatala Date: Tue, 24 Oct 2023 04:24:17 +0200 Subject: [PATCH] fix generate-page (#414) --- bin/generate-page | 57 +++++++++++++++++++++++++++++++------------ page/Views/detail.php | 6 ++++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/bin/generate-page b/bin/generate-page index 120f0748..42fd1b8d 100755 --- a/bin/generate-page +++ b/bin/generate-page @@ -33,6 +33,25 @@ if (! is_dir($enDir)) { exit(1); } +function flatterArray(array $inputArray): array +{ + $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($inputArray), RecursiveIteratorIterator::SELF_FIRST); + $newArray = []; + $path = []; + + foreach ($iterator as $key => $value) { + $path[$iterator->getDepth()] = $key; + + if (! is_array($value)) { + $newArray[ + implode('.', array_slice($path, 0, $iterator->getDepth() + 1)) + ] = $value; + } + } + + return $newArray; +} + // get default "en" language for reference $enFiles = get_filenames( $enDir, @@ -44,18 +63,11 @@ $enFiles = get_filenames( $enItems = []; foreach ($enFiles as $enFile) { - $enFileLines = file($enFile); + $enFileLines = include $enFile; + $enFileLines = flatterArray($enFileLines); $enName = basename($enFile); - $pattern = '/(.*)\'([a-zA-Z0-9_]+?)\'(\s*=>\s*)([\'"].+[\'"]),/u'; - - $enItems[$enName] = ['lines' => [], 'count' => 0]; - foreach ($enFileLines as $line) { - if (preg_match($pattern, $line, $matches)) { - $enItems[$enName]['lines'][$matches[2]] = $matches[4]; - $enItems[$enName]['count']++; - } - } + $enItems[$enName] = ['lines' => $enFileLines, 'count' => count($enFileLines)]; } // create build directory @@ -118,17 +130,17 @@ foreach ($locales as $localeKey => $locale) { ]; foreach ($files as $file) { - $fileLines = file($file); + $fileLines = include $file; + $fileLines = flatterArray($fileLines); $name = basename($file); - $pattern = '/(.*)\'([a-zA-Z0-9_]+?)\'(\s*=>\s*)([\'"].+[\'"]),/u'; $validLines = array_keys($enItems[$name]['lines']); $items[$locale]['files'][$name] = ['lines' => [], 'count' => 0, 'percent' => 0, 'missing' => [], 'background' => '']; - foreach ($fileLines as $line) { - if (preg_match($pattern, $line, $matches) && in_array($matches[2], $validLines, true)) { - $items[$locale]['files'][$name]['lines'][$matches[2]] = $matches[4]; + foreach ($fileLines as $key => $val) { + if (in_array($key, $validLines, true) && $val !== '') { + $items[$locale]['files'][$name]['lines'][$key] = $val; $items[$locale]['files'][$name]['count']++; } } @@ -138,6 +150,21 @@ foreach ($locales as $localeKey => $locale) { $items[$locale]['files'][$name]['background'] = $backgroundChoice($items[$locale]['files'][$name]['percent']); } + // detect missing files + if ($missingFiles = array_diff(array_keys($enItems), array_keys($items[$locale]['files']))) { + foreach ($missingFiles as $missingName) { + $items[$locale]['files'][$missingName] = [ + 'lines' => [], + 'count' => 0, + 'missing' => array_keys($enItems[$missingName]['lines']), + 'percent' => 'Missing file', + 'background' => $backgroundChoice(0), + ]; + } + // sort by file names + ksort($items[$locale]['files']); + } + $items[$locale]['percent'] = round(array_sum(array_column($items[$locale]['files'], 'percent')) / count($items[$locale]['files']), 1); $items[$locale]['background'] = $backgroundChoice($items[$locale]['percent']); diff --git a/page/Views/detail.php b/page/Views/detail.php index 9dfb4a08..8bc59b74 100644 --- a/page/Views/detail.php +++ b/page/Views/detail.php @@ -35,7 +35,11 @@ /
-
%
+ +
%
+ +
+