[TxMt] Bundle to alphabetize CSS rules
pete otaqui
pete at otaqui.com
Mon Mar 10 06:47:10 UTC 2008
Hello 'Mates,
I'm working on a bundle (my first!) to alphabetize css attributes, as
described by Garrett Dimon here:
http://www.digital-web.com/articles/architecting_css/
I whipped up some 'working' (as in, it works on a 2000+ line CSS file
I threw it at) code here, which has some obvious problems. Here are
the key parts, all in PHP:
[code]
function find_rule()
{
global $input, $nOffset;
$nStart = strpos($input,'{',$nOffset);
if ( $nStart === false ) return false;
$nEnd = strpos($input,'}',$nStart);
$nOffset = $nEnd;
$nLen = $nEnd - $nStart + 1;
$aReturn[] = $nStart;
$aReturn[] = $nLen;
// return an array containing the start index and length of the next
rule
return $aReturn;
}
function alphabetize_rule($sRule)
{
preg_match_all('/[^:\s]*:[^;}]*;?/',$sRule,$aProps);
sort($aProps[0],SORT_STRING);
$sRule = implode(' ',$aProps[0]);
$sRule = '{ '.$sRule.' }';
return $sRule;
}
?>
[/code]
The problems, that I can see anyway, are in the regular expressions,
since a CSS rule can contain URLs and URLs can legitimately contain
both un-escaped semi-colons and I think even curly brackets. I can
spend a bit more time on those to search for '(' and ')' and allow
those characters between, so that's fine.
My real question is if anyone has any thoughts about managing the
whitespace that separates the rules. Generally I write CSS all on one
line, and my code will return attributes separated by spaces. Ideally
I'd like it to respect how the coder has written their CSS already,
leaving any newlines and indentation in place.
Does anyone have any thoughts on how best to do this? Do you think
this bundle would be useful?
Many thanks for any feedback.
Pete
More information about the textmate
mailing list