Skip to content

Instantly share code, notes, and snippets.

@etienned
Created May 25, 2012 18:28
Show Gist options
  • Save etienned/2789689 to your computer and use it in GitHub Desktop.
Save etienned/2789689 to your computer and use it in GitHub Desktop.
CLI for the PHP version of CSSTidy
#!/usr/bin/env php
<?php
/**
* CSSTidy - Command Line Interface (CLI)
*
* Command Line Interface that try to mimic as much as possible the C++ CLI
* version of csstidy. It should be a drop in replacment for the C++ CLI that is
* no longuer maintain. New settings like css_level are added.
*
* Copyright 2005, 2006, 2007 Florian Schmitz
*
* This file is part of CSSTidy.
*
* CSSTidy is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* CSSTidy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @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 Cedric Morin (cedric at yterium dot com) 2010-2012
* @author Christopher Finke (cfinke at gmail.com) 2012
* @author Etienne Desautels (etienne dot desautels at gmail dot com) 2012
*/
/**
* Contains the Parser class
*
* @version 1.4
*/
include('class.csstidy.php');
/**
* Return the usage message to sdterr and exit with return code 1
* @access private
* @version 1.0
*/
function _show_usage() {
file_put_contents('php://stderr', "Usage:
csstidy input_filename [
--compress_colors=[true|false] |
--compress_font-weight=[true|false] |
--discard_invalid_properties=[false|true] |
--discard_invalid_selectors=[false|true] |
--lowercase_s=[false|true] |
--preserve_css=[false|true] |
--remove_bslash=[true|false] |
--remove_last_;=[false|true] |
--sort_properties=[false|true] |
--sort_selectors=[false|true] |
--timestamp=[false|true] |
--merge_selectors=[2|1|0] |
--case_properties=[0|1|2] |
--optimise_shorthands=[1|2|0] |
--template=[default|filename|low|high|highest] |
--css_level=[CSS3.0|CSS2.1|CSS2.0|CSS1.0] |
output_filename ]*\n");
exit(1);
}
// Check if there's at least one argument
if ($argc < 2) {
_show_usage();
}
// Get the first argument, the input filename
$input = $argv[1];
// Set the input to stdin if filename is -
if ($input == "-") {
$input = 'php://stdin';
// Check that the filename is not a setting
} else if (strpos($input, '-') === 0) {
_show_usage();
}
// Get the data
$css_code = file_get_contents($input);
// Exit on error when reading the data
if ($css_code === false) {
file_put_contents('php://stderr', "The file \"" . $input . "\" does not exist.\n");
exit(1);
}
// Get the output file
if ($argc > 2) {
$output = $argv[$argc - 1];
if (strpos($output, '-') === 0) {
$output = 'php://stdout';
}
} else {
$output = 'php://stdout';
}
$css = new csstidy();
// Reset some default settings to match csstidy CLI (C++) defaults
$css->set_cfg('remove_last_;', false);
$css->set_cfg('case_properties', 0);
$css->set_cfg('merge_selectors', 2);
// Set the settings defined on the CLI
$settings = array( 'compress_colors' => 'bool',
'compress_font-weight' => 'bool',
'discard_invalid_properties' => 'bool',
'discard_invalid_selectors' => 'bool',
'lowercase_s' => 'bool',
'preserve_css' => 'bool',
'remove_bslash' => 'bool',
'remove_last_;' => 'bool',
'sort_properties' => 'bool',
'sort_selectors' => 'bool',
'timestamp' => 'bool',
'merge_selectors' => 'int',
'case_properties' => 'int',
'optimise_shorthands' => 'int',
'template' => 'str',
'css_level' => 'str',
);
// Can't use getopt here because too limited and broken
foreach ($argv as $arg) {
// Check if it's a settings
if (strpos($arg, '--') === 0 && strpos($arg, '=') !== false) {
// Remove the -- and split the setting name and the value
$arg = explode('=', substr($arg, 2), 2);
$setting = $arg[0];
$value = $arg[1];
if (array_key_exists($setting, $settings)) {
switch ($settings[$setting]) {
case 'bool':
if ($value === 'true') {
$value = true;
} else if ($value === 'false') {
$value = false;
} else {
_show_usage();
}
// Cast it to int for sort_selectors,
// there's a mismatch between the PHP and C++ versions
if ($setting === 'sort_selectors') {
$value = intval($value);
}
break;
case 'int':
if (in_array($value, array('0', '1', '2'))) {
$value = intval($value);
} else {
_show_usage();
}
break;
}
$css->set_cfg($setting, $value);
}
}
}
// Parse the CSS
$css->parse($css_code);
// Optimize and output the CSS file
if (file_put_contents($output, $css->print->plain()) === false) {
file_put_contents('php://stderr', "There's a problem writing to this file \"" . $output . "\".\n");
exit(1);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment