Created
June 17, 2013 20:17
-
-
Save behrad/5799960 to your computer and use it in GitHub Desktop.
Flip CSS to RTL version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* flipcss.php 0.4 | |
* by daif alotaibi (http://daif.net) | |
* [email protected] | |
* | |
* Link: | |
* http://daif.net/script/flipcss.phps | |
* | |
* Description: | |
* This Tool flip CSS rules direction from LTR to RTL or vice versa . | |
* tool support these CSS rules: float, text-align, padding, margin | |
* and border-radius . | |
* | |
* Syntax: | |
* right to left | |
* FlipTheCSSFile('path_to_css_file', 'RTL'); | |
* left to right | |
* FlipTheCSSFile('path_to_css_file', 'LTR'); | |
* | |
* Changelog | |
* 0.1 first release | |
* 0.2 rewrite padding and margin | |
* 0.3 support border-radius | |
* 0.4 add GPL License | |
*/ | |
FlipTheCSSFile('css/bootstrap.css', 'RTL'); | |
FlipTheCSSFile('css/bootstrap-responsive.css', 'RTL'); | |
function FlipTheCSSFile($css_file, $dir='RTL') { | |
$css_data = file_get_contents($css_file); | |
//remove comments | |
$css_data = preg_replace('/\/\*(.*)?\*\//Usi','' ,$css_data); | |
//rewrite padding,margin | |
$css_data = preg_replace('/(\h*)(padding|margin):(.+)\h+(.+)\h+(.+)\h+(.+)\h*;/Ui',"\\1\\2-right:\\4;\\1\\2-left:\\5;" ,$css_data); | |
//rewrite border-radius | |
$css_data = preg_replace('/(\h*|)border-radius:(.+)\h+(.+)\h+(.+)\h+(.+)\h*;/Ui',"\\1border-top-left-radius:\\2;\\1border-top-". | |
"right-radius:\\3;\\1border-bottom-right-radius:\\4;\\1border-bottom-left-radius:\\5;", $css_data); | |
//start parsing css file | |
$css_data = preg_replace('/(@media .+){(.+)}\s*}/Uis', '\1$\2}$', $css_data); | |
preg_match_all('/(.+){(.+)(}\$|})/Uis', $css_data, $css_arr); | |
$css_flipped = "/* Created by flipcss.php 0.4 by daif alotaibi (http://daif.net) */\n\nbody{\n\tdirection:$dir;\n}\n\n"; | |
foreach($css_arr[0] as $key=>$val) { | |
if(preg_match('/left|right/i', $css_arr[2][$key])) { // TODO: direction rule | |
if($rules = FlipTheCSSRules($css_arr[2][$key])) { | |
$css_flipped .= trim(str_replace('$','{',$css_arr[1][$key])); | |
$css_flipped .= " {\n\t".trim($rules)."\n"; | |
$css_flipped .= str_replace('$',"\n}",$css_arr[3][$key])."\n\n"; | |
} | |
} | |
} | |
file_put_contents(substr($css_file,0,-4).'-'.strtolower($dir).'.css', $css_flipped); | |
print "CSS File $css_file has been flipped to $dir direction.<br />\n"; | |
} | |
function FlipTheCSSRules($rules) { | |
$return = ''; | |
$rules_arr = explode(";", $rules); | |
foreach($rules_arr as $rule) { | |
//ignore rules that doesn't need flipping | |
if(preg_match('/(left|right)/i', $rule)) { | |
//flip float | |
if(preg_match('/float\h*:\h*(.+)/i', $rule, $rule_arr)) { | |
$rule = 'float: '.((trim($rule_arr[1])=='left')?'right':'left'); | |
$return .="\t".trim($rule).";\n"; | |
//flip text-align | |
} elseif(preg_match('/text-align\h*:\h*(.+)/i', $rule, $rule_arr)) { | |
$rule = 'text-align: '.((trim($rule_arr[1])=='left')?'right':'left'); | |
$return .="\t".trim($rule).";\n"; | |
//flip padding, margin | |
} elseif(preg_match('/(\*|)(margin|padding)-(left|right)\h*:\h*(.+)/i', $rule, $rule_arr)) { | |
$dir = ((trim($rule_arr[3])=='left')?'right':'left'); | |
//reset direction rule | |
if((trim($rule_arr[3]) == 'left' && !preg_match('/'.trim($rule_arr[2]).'\-right/i', $rules)) || (trim($rule_arr[2]) == 'right' && !preg_match('/'.trim($rule_arr[2]).'\-left/i', $rules))) { | |
$rule = trim($rule_arr[1]).trim($rule_arr[2]).'-'.$rule_arr[3].": 0;\n\t"; | |
} else { | |
$rule = ''; | |
} | |
$rule .= trim($rule_arr[1]).trim($rule_arr[2]).'-'.$dir.': '.$rule_arr[4]; | |
$return .="\t".trim($rule).";\n"; | |
//flip border-radius | |
} elseif(preg_match('/border-(top|bottom)-(left|right)-radius\h*:\h*(.+)/i', $rule, $rule_arr)) { | |
$dir = ((trim($rule_arr[2])=='left')?'right':'left'); | |
//reset direction rule | |
if((trim($rule_arr[2]) == 'left' && !preg_match('/'.trim($rule_arr[1]).'\-right/i', $rules)) || (trim($rule_arr[2]) == 'right' && !preg_match('/'.trim($rule_arr[1]).'\-left/i', $rules))) { | |
$rule = 'border-'.$rule_arr[1].'-'.$rule_arr[2].'-radius: 0;'."\n\t"; | |
} else { | |
$rule = ''; | |
} | |
//write new direction rule | |
$rule .= 'border-'.$rule_arr[1].'-'.$dir.'-radius: '.$rule_arr[3]; | |
$return .="\t".trim($rule).";\n"; | |
//flip left, right | |
} elseif(preg_match('/\h+(left|right)\h*:\h*(.+)/i', $rule, $rule_arr)) { | |
$dir = ((trim($rule_arr[1])=='left')?'right':'left'); | |
//reset LTR rule | |
if((trim($rule_arr[1]) == 'left' && !preg_match('/\h+right\h*:/i', $rules)) || (trim($rule_arr[1]) == 'right' && !preg_match('/\h+left\h*:/i', $rules))) { | |
$rule = trim($rule_arr[1]).": auto;\n\t"; | |
} else { | |
$rule = ''; | |
} | |
$rule .= $dir.': '.$rule_arr[2]; | |
$return .="\t".trim($rule).";\n"; | |
} | |
} | |
} | |
return($return); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment