-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.php
More file actions
58 lines (50 loc) · 2.04 KB
/
convert.php
File metadata and controls
58 lines (50 loc) · 2.04 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
<?php
// Конфигурация
$directory = './src'; // путь к твоим компонентам
$rootFontSize = 16; // базовый размер шрифта для rem
$mode = 'toRem'; // 'toRem' или 'toPx'
// Рекурсивная функция для обхода файлов
function scanDirRecursively($dir) {
$files = [];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file) {
if ($file->isFile() && preg_match('/\.(js|jsx|ts|tsx|html|css)$/', $file->getFilename())) {
$files[] = $file->getPathname();
}
}
return $files;
}
// Конвертация px → rem
function pxToRem($content, $rootFontSize) {
// Находит все числа с px, включая отрицательные и дробные
return preg_replace_callback('/(-?\d+(\.\d+)?)px/', function($matches) use ($rootFontSize) {
$pxValue = floatval($matches[1]);
$remValue = round($pxValue / $rootFontSize, 4);
return $remValue . 'rem';
}, $content);
}
// Конвертация rem → px
function remToPx($content, $rootFontSize) {
// Находит все числа с rem, включая отрицательные и дробные
return preg_replace_callback('/(-?\d+(\.\d+)?)rem/', function($matches) use ($rootFontSize) {
$remValue = floatval($matches[1]);
$pxValue = round($remValue * $rootFontSize, 2);
return $pxValue . 'px';
}, $content);
}
// Получаем все файлы
$files = scanDirRecursively($directory);
// Обрабатываем каждый файл
foreach ($files as $file) {
$content = file_get_contents($file);
if ($mode === 'toRem') {
$newContent = pxToRem($content, $rootFontSize);
} else {
$newContent = remToPx($content, $rootFontSize);
}
if ($newContent !== $content) {
file_put_contents($file, $newContent);
echo "Обработан файл: $file\n";
}
}
echo "Готово! Режим: $mode\n";