#!/usr/bin/env php . * * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License * @package csstidy * @author Florian Schmitz (floele at gmail dot com) 2005-2007 * @author Brett Zamir (brettz9 at yahoo dot com) 2007 * @author Nikolay Matsievsky (speed at webo dot name) 2009-2010 * @author Christopher Finke (cfinke at gmail.com) 2012 * @author Etienne Desautels (etienne dot desautels at gmail dot com) 2012 * @author Cedric Morin (cedric at yterium dot com) 2010-2019 */ error_reporting(E_ALL); if (version_compare(PHP_VERSION, '5.4')<0){ die('Requires PHP 5.4 or above'); } /** * Contains the Parser class * * @version 1.6.5 */ require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'class.csstidy.php'; $csstidy = new csstidy(); $dumpTree = false; $default_media = ''; /** * Return the usage message to sdterr and exit with return code 1 * @access private * @version 1.0 */ function _show_usage(){ $exe = $GLOBALS['argv'][0]; $version = "Version " . $GLOBALS['csstidy']->version; $HELP = << ['format' => 'int', 'expected' => [0, 1, 2]], 'compress_colors' => ['format' => 'bool'], 'compress_font-weight' => ['format' => 'bool'], 'css_level' => ['format' => 'str', 'expected' => ['CSS3.0', 'CSS2.1', 'CSS2.0', 'CSS1.0']], 'discard_invalid_properties' => ['format' => 'bool'], 'discard_invalid_selectors' => ['format' => 'bool'], 'merge_selectors' => ['format' => 'int', 'expected' => [0, 1, 2]], 'lowercase_s' => ['format' => 'bool'], 'optimise_shorthands' => ['format' => 'int', 'expected' => [0, 1, 2]], 'preserve_css' => ['format' => 'bool'], 'remove_bslash' => ['format' => 'bool'], 'remove_last_semicolon' => ['format' => 'bool', 'setting' => 'remove_last_;'], 'reverse_left_and_right' => ['format' => 'bool'], 'sort_properties' => ['format' => 'bool'], 'sort_selectors' => ['format' => 'bool'], 'template' => ['format' => 'str', 'short' => 't', 'expected' => ['default', 'filename', 'low', 'high', 'highest']], 'timestamp' => ['format' => 'bool'], ]; foreach ($settings as $option_name => $desc) { $options_list = [ "--$option_name" ]; if (isset($desc['short'])) { $options_list[] = "-" . $desc['short']; } $settings[$option_name]['options_list'] = $options_list; } for ($i = 1; $i<$argc; $i++){ if ($argv[$i]==='-h' || $argv[$i]==='--help'){ _show_usage(); } if ($argv[$i]==='-v' || $argv[$i]==='--version'){ exit($csstidy->version . "\n"); } $value = parseArgument($i, ['--default_media']); if (isset($value)) { $value = castArgument($value, 'str'); $default_media = trim($value); if (strpos($default_media, '@') === false) { $default_media = '@media ' . $default_media; } } foreach ($settings as $option_name => $desc) { $value = parseArgument($i, $desc['options_list']); if (isset($value)){ if (isset($desc['format'])) { $value = castArgument($value, $desc['format'], isset($desc['expected']) ? $desc['expected'] : null); } $setting_name = $option_name; if (isset($desc['setting'])) { $setting_name = $desc['setting']; } $csstidy->set_cfg($setting_name, $value); continue 2; } } if ($argv[$i]==='-T'){ $dumpTree = true; continue; } if (file_exists($argv[$i])){ $inputFile = $argv[$i]; continue; } } if (!$inputFile) { $inputFile = 'php://stdin'; } // Get the data $css_code = file_get_contents($inputFile); // Exit on error when reading the data if ($css_code===false){ file_put_contents('php://stderr', "The file \"" . $inputFile . "\" does not exist.\n"); exit(1); } // Parse the CSS $csstidy->parse($css_code); if ($dumpTree){ var_dump($csstidy->css); } else { // Optimize and output the CSS file echo $csstidy->print->plain($default_media); echo "\n"; }