Class: Rtlize::RTLizer
- Inherits:
-
Object
- Object
- Rtlize::RTLizer
- Defined in:
- lib/rtlize/rtlizer.rb
Overview
Originally ported from github.com/ded/R2
Class Method Summary collapse
- .cursor(v) ⇒ Object
- .deg(v) ⇒ Object
- .direction(v) ⇒ Object
- .quad(v) ⇒ Object
- .quad_radius(v) ⇒ Object
- .rect(v) ⇒ Object
- .rtltr(v) ⇒ Object
- .shadow(v) ⇒ Object
- .transform(css) ⇒ Object
- .transform_declarations(declarations, no_invert = false) ⇒ Object
Class Method Details
.cursor(v) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/rtlize/rtlizer.rb', line 154 def cursor(v) if v.match(/^[ns]?e-resize$/) v.gsub(/e-resize/, 'w-resize') elsif v.match(/^[ns]?w-resize$/) v.gsub(/w-resize/, 'e-resize') else v end end |
.deg(v) ⇒ Object
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/rtlize/rtlizer.rb', line 184 def deg(v) if v == '0' v else old_angle = v.to_f new_angle = 360 - old_angle new_angle = new_angle.to_i if new_angle == new_angle.to_i # If it's an integer, write it without a decimal part. v.gsub(/[0-9.]+/, new_angle.to_s) end end |
.direction(v) ⇒ Object
119 120 121 |
# File 'lib/rtlize/rtlizer.rb', line 119 def direction(v) v == 'ltr' ? 'rtl' : v == 'rtl' ? 'ltr' : v end |
.quad(v) ⇒ Object
134 135 136 137 138 |
# File 'lib/rtlize/rtlizer.rb', line 134 def quad(v) # 1px 2px 3px 4px => 1px 4px 3px 2px m = v.split(/\s+/) m.length == 4 ? [m[0], m[3], m[2], m[1]].join(' ') : v end |
.quad_radius(v) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rtlize/rtlizer.rb', line 140 def quad_radius(v) # top-left, top-right, bottom-right, bottom-left # when bottom-left is omitted, it takes the value of top-right # when bottom-right is omitted, it takes the value of top-left # when top-right is omitted, it takes the value of top-left m = v.split(/\s+/) case m.length when 4 then [m[1], m[0], m[3], m[2]].join(' ') when 3 then [m[1], m[0], m[1], m[2]].join(' ') when 2 then [m[1], m[0]].join(' ') else v end end |
.rect(v) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rtlize/rtlizer.rb', line 123 def rect(v) if v.match(/rect\([^)]*\)/) v.gsub(/\([^)]*\)/) do |m| parts = m.gsub(/[()]/, '').split(',').map(&:strip) "(#{parts[0]}, #{parts[3]}, #{parts[2]}, #{parts[1]})" end else v end end |
.rtltr(v) ⇒ Object
115 116 117 |
# File 'lib/rtlize/rtlizer.rb', line 115 def rtltr(v) v == 'left' ? 'right' : v == 'right' ? 'left' : v end |
.shadow(v) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rtlize/rtlizer.rb', line 164 def shadow(v) found = false v.gsub(/rgba\([^)]*\)|,|#[0-9A-Fa-f]*|[-0-9.px]+/) do |m| if m == "," # this property can take several comma-seperated values, we account for that, and transform each one correctly. found = false m elsif m.match(/rgba\([^)]*\)|#[0-9A-Fa-f]*/) || found m else found = true if m.to_f.zero? m else m.chars.first == '-' ? m[1..-1] : '-' + m end end end end |
.transform(css) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rtlize/rtlizer.rb', line 66 def transform(css) no_invert = false css.gsub(/([^{]+\{[^}]+\})+?/) do |rule| # Break rule into selector|declaration parts parts = rule.match(/([^{]+)\{([^}]+)/) if parts && !parts[1].gsub(/\/\*[\s\S]+?\*\//, '').match(/\.rtl\b/) # Don't transform rules that include the selector ".rtl" (remove comments first) selector, declarations = parts[1..2] # The CSS comment must start with "!" in order to be considered as important by the YUI compressor, otherwise, it will be removed by the asset pipeline before reaching this processor. if selector.match(/\/\*!= begin\(no-rtl\) \*\//) no_invert = true # selector.gsub!(/\/\*!= begin\(no-rtl\) \*\//, '') elsif selector.match(/\/\*!= end\(no-rtl\) \*\//) no_invert = false # selector.gsub!(/\/\*!= end\(no-rtl\) \*\//, '') end selector + '{' + self.transform_declarations(declarations, no_invert) + '}' else rule end end end |
.transform_declarations(declarations, no_invert = false) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rtlize/rtlizer.rb', line 90 def transform_declarations(declarations, no_invert = false) declarations.split(/;(?!base64)/).map do |decl| m = decl.match(/([^:]+):(.+)$/) if m && !no_invert prop, val = m[1..2] # Get the property, without comments or spaces, to be able to find it. prop_name = prop.strip.split(' ').last if @property_map[prop_name] prop = prop.sub(prop_name, @property_map[prop_name]) end if @value_map[prop_name] val = val.sub(val.strip, self.send(@value_map[prop_name], val.strip)) end prop + ':' + val + ';' elsif m decl + ';' else decl end end.join end |