199 lines
5.8 KiB
PHP
199 lines
5.8 KiB
PHP
<?php
|
|
|
|
|
|
$root = dirname(__DIR__) . '/assets/css/';
|
|
$main = file_get_contents(dirname(__DIR__) . '/tools/main.source.css');
|
|
|
|
function parse_css(string $css, $media = null): array
|
|
{
|
|
$out = array();
|
|
$n = strlen($css);
|
|
$i = 0;
|
|
$ss = 0;
|
|
$in_str = '';
|
|
$in_com = false;
|
|
while ($i < $n) {
|
|
$c = $css[$i];
|
|
$next = ($i + 1 < $n) ? $css[$i + 1] : '';
|
|
if ($in_com) {
|
|
if ('*' === $c && '/' === $next) {
|
|
$in_com = false;
|
|
$i += 2;
|
|
continue;
|
|
}
|
|
$i++;
|
|
continue;
|
|
}
|
|
if ('' !== $in_str) {
|
|
if ('\\' === $c) {
|
|
$i += 2;
|
|
continue;
|
|
}
|
|
if ($c === $in_str) {
|
|
$in_str = '';
|
|
}
|
|
$i++;
|
|
continue;
|
|
}
|
|
if ('/' === $c && '*' === $next) {
|
|
$in_com = true;
|
|
$i += 2;
|
|
continue;
|
|
}
|
|
if ("'" === $c || '"' === $c) {
|
|
$in_str = $c;
|
|
$i++;
|
|
continue;
|
|
}
|
|
if ('{' === $c) {
|
|
$sel = trim(substr($css, $ss, $i - $ss));
|
|
$j = $i + 1;
|
|
$d = 1;
|
|
$s2 = '';
|
|
$cm = false;
|
|
while ($j < $n && $d > 0) {
|
|
$cc = $css[$j];
|
|
$nn = ($j + 1 < $n) ? $css[$j + 1] : '';
|
|
if ($cm) {
|
|
if ('*' === $cc && '/' === $nn) {
|
|
$cm = false;
|
|
$j += 2;
|
|
continue;
|
|
}
|
|
$j++;
|
|
continue;
|
|
}
|
|
if ('' !== $s2) {
|
|
if ('\\' === $cc) {
|
|
$j += 2;
|
|
continue;
|
|
}
|
|
if ($cc === $s2) {
|
|
$s2 = '';
|
|
}
|
|
$j++;
|
|
continue;
|
|
}
|
|
if ('/' === $cc && '*' === $nn) {
|
|
$cm = true;
|
|
$j += 2;
|
|
continue;
|
|
}
|
|
if ("'" === $cc || '"' === $cc) {
|
|
$s2 = $cc;
|
|
$j++;
|
|
continue;
|
|
}
|
|
if ('{' === $cc) {
|
|
$d++;
|
|
} elseif ('}' === $cc) {
|
|
$d--;
|
|
}
|
|
$j++;
|
|
}
|
|
$block = substr($css, $ss, $j - $ss);
|
|
if (0 === strpos($sel, '@media') || 0 === strpos($sel, '@supports')) {
|
|
$inner = substr($css, $i + 1, $j - $i - 2);
|
|
$out = array_merge($out, parse_css($inner, $sel));
|
|
} elseif (0 !== strpos($sel, '@')) {
|
|
$out[] = array( $media, $sel, $block );
|
|
}
|
|
$i = $j;
|
|
$ss = $i;
|
|
continue;
|
|
}
|
|
if ('}' === $c) {
|
|
$ss = $i + 1;
|
|
}
|
|
$i++;
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
|
|
$dead = array(
|
|
'.teacher_name', '.teacher_name-wrapper', '.teacher__rating', '.teacher__rating-img',
|
|
'.teacher_stars', '.teacher-about', '.teacher-about::before', '.teacher-description_text',
|
|
'.teacher-description_text::after', '.teacher-description_location', '.teacher-description_location::before',
|
|
'.info-block', '.info-block__item', '.info-block__item-wrapper', '.info__block-item_number',
|
|
'.advertising-block', '.advertising-block__text_text', '.advertising-label', '.advertising-label span',
|
|
'.preparation-block', '.preparation-block p', '.block-new', '.block-new_title',
|
|
'.page-rewiews', '.main-text-color', '.section-Mntitle-circle', '.banner-circle',
|
|
'.certificates_title', '.about-teacher', '.about-teacher-wrapper', '.about-col_item',
|
|
'.cat-wrapper_item', '.disciplines__item', '.cta-final .btn', '.map-club__item',
|
|
);
|
|
|
|
function render(array $rules): string
|
|
{
|
|
$out = array();
|
|
$open = null;
|
|
foreach ($rules as $r) {
|
|
list($media, $sel, $block) = $r;
|
|
if ($media !== $open) {
|
|
if ($open !== null) {
|
|
$out[] = '}';
|
|
}
|
|
if ($media !== null) {
|
|
$out[] = $media . ' {';
|
|
}
|
|
$open = $media;
|
|
}
|
|
$out[] = $block;
|
|
}
|
|
if ($open !== null) {
|
|
$out[] = '}';
|
|
}
|
|
return implode("\n", $out) . "\n";
|
|
}
|
|
|
|
$rules = parse_css($main);
|
|
$core = $teachers = $reviews = $removed = array();
|
|
|
|
foreach ($rules as $r) {
|
|
list($media, $sel, $block) = $r;
|
|
$sel_clean = preg_replace('#/\*.*?\*/#s', '', $sel);
|
|
$selectors = array_map(function ($s) {
|
|
return preg_replace('/\s+/', ' ', trim($s));
|
|
}, explode(',', $sel_clean));
|
|
|
|
$is_dead = false;
|
|
foreach ($selectors as $one) {
|
|
if (in_array($one, $dead, true)) {
|
|
$is_dead = true;
|
|
break;
|
|
}
|
|
}
|
|
if ($is_dead) {
|
|
$removed[] = $r;
|
|
continue;
|
|
}
|
|
|
|
|
|
$all_t = true;
|
|
$all_v = true;
|
|
foreach ($selectors as $one) {
|
|
if (! preg_match('/teacher|mentor/i', $one)) {
|
|
$all_t = false;
|
|
}
|
|
if (! preg_match('/reviews?/i', $one)) {
|
|
$all_v = false;
|
|
}
|
|
}
|
|
if ($all_t) {
|
|
$teachers[] = $r;
|
|
}
|
|
if ($all_v) {
|
|
$reviews[] = $r;
|
|
}
|
|
if (! $all_t && ! $all_v) {
|
|
$core[] = $r;
|
|
}
|
|
}
|
|
|
|
file_put_contents($root . 'main.css', render($core));
|
|
file_put_contents($root . 'main-teachers.css', "/* teachers */\n" . render($teachers));
|
|
file_put_contents($root . 'main-reviews.css', "/* reviews */\n" . render($reviews));
|
|
file_put_contents($root . '_unused-legacy.css', "/* removed dead legacy */\n" . render($removed));
|
|
|
|
echo 'core: ' . count($core) . ", teachers: " . count($teachers) . ", reviews: " . count($reviews) . ", removed: " . count($removed) . "\n";
|