Module: RTLit::Converter
- Defined in:
- lib/rtlit/converter.rb
Overview
CSS string LTR to RTL converter
Class Method Summary collapse
-
.to_rtl(css) ⇒ String
Convert CSS properties and values from LTR to RTL.
Class Method Details
.to_rtl(css) ⇒ String
Convert CSS properties and values from LTR to RTL. Accepts any valid CSS string. Returns a CSS string.
Example:
css = "
body {
text-align: left;
padding-right: 10px;
}"
rtl_css = to_rtl(css)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rtlit/converter.rb', line 26 def to_rtl(css) '' place_holder = '|====|' converted_css = css.gsub 'ltr', 'rtl' ltr_matches = converted_css.scan /^([\n\s]*[^:\n\t\s\.\#\{\}]*(left|right)[^:]*:)|(:[^\w\.\#\{\}]*[^;\.\#\{\}]*(left|right)[^;]*;)/ css_to_replace = ltr_matches.flatten.delete_if{|a| a.nil? || ['left','right'].include?(a) } css_to_replace.each do |match| next if match.include? 'right' or match =~ /(left|right)\s(top|bottom|\d+[\w%]+)/ converted_css.gsub! match, (match.gsub 'left', place_holder) end css_to_replace.each do |match| next if match.include? 'left' or match =~ /(left|right)\s(top|bottom|\d+[\w%]+)/ converted_css.gsub! match, (match.gsub 'right', 'left') end converted_css.gsub! place_holder, 'right' quad_matches = converted_css.scan /(-?\d+(?:px|pt|em|%)+|@\w+)\s+(-?\d+(?:px|pt|em|%)+|@\w+)\s+(-?\d+(?:px|pt|em|%)+|@\w+)\s+(-?\d+(?:px|pt|em|%)+|@\w+)/ #quad_matches = converted_css.scan /[\-\d\w\@\%]+\s+[\-\d\w\@\%]+\s+[\-\d\w\@\%]+\s+[\-\d\w\@\%]+/ quad_matches.each do |ar| m = ar.join(' ') t, r, b, l = m.split ' ' converted_css.gsub! m, [t,l,b,r].join(' ') end return converted_css end |