Skip to content

Instantly share code, notes, and snippets.

@mitio
Created June 8, 2010 14:22
Show Gist options
  • Save mitio/430087 to your computer and use it in GitHub Desktop.
Save mitio/430087 to your computer and use it in GitHub Desktop.
Simple command-line tool to prettify a bit CSS files.
#!/usr/local/bin/php
<?php
$replacements = array(
"/:([^ ])/" => ": \$1",
"/(\\w)\\{/" => "\$1 {",
"/\\}\n([^\n])/" => "}\n\n\$1",
);
if ($argc < 2) {
error_log("Usage:\n\t" . __FILE__ . " path/to/stylesheet.css [path/to/another_file.css ...]");
exit(1);
}
for ($i = 1; $i < $argc; $i++) {
$path = $argv[$i];
if (!is_file($path) || !is_readable($path) || !is_writeable($path)) {
error_log("Error: Bad file (does not exist, not readable & writeable or not a file): $path");
$errors = true;
continue;
}
$data = file_get_contents($path);
foreach ($replacements as $pattern => $substitute) {
$data = preg_replace($pattern, $substitute, $data);
}
file_put_contents($path, $data);
echo "OK: $path\n";
}
exit(!empty($errors) ? 2 : 0);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment