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
Personally this comment would confuse me for my own CSS. But I can imagine it being useful for working with other peoples crazy CSS files.
There are already reformatting macros in the CSS bundle that I made to completely compress or pretty-print all your rules. It might make sense to merge these commands somehow. Depending on the speed of the php.
Check out the regex I use in those macros. I tried to make it pretty flexable and just ignore the contents of of the rules. I haven yet heard of anyone running into a rule that my redormatter breaks.
Thomas Aylott [SubtleGradient] from iPhone
On Mar 10, 2008, at 2:47 AM, pete otaqui pete@otaqui.com wrote:
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
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate