Class: Rtlize::Declaration

Inherits:
Object
  • Object
show all
Defined in:
lib/rtlize/declaration.rb

Class Method Summary collapse

Class Method Details

.cursor(v) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/rtlize/declaration.rb', line 136

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



166
167
168
169
170
171
172
173
174
175
# File 'lib/rtlize/declaration.rb', line 166

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



95
96
97
# File 'lib/rtlize/declaration.rb', line 95

def direction(v)
  v == 'ltr' ? 'rtl' : v == 'rtl' ? 'ltr' : v
end

.quad(v) ⇒ Object



116
117
118
119
120
# File 'lib/rtlize/declaration.rb', line 116

def quad(v)
  # 1px 2px 3px 4px => 1px 4px 3px 2px
  m = v.split(/\s+/)
  m.length == 4 && !v.include?('rgba(') ? [m[0], m[3], m[2], m[1]].join(' ') : v
end

.quad_radius(v) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rtlize/declaration.rb', line 122

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rtlize/declaration.rb', line 99

def rect(v)
  if v.match(/rect\([^)]*\)/)
    v.gsub(/\([^)]*\)/) do |m|
      parts = m.gsub(/[()]/, '').split(',').map(&:strip)
      if parts.size == 1
        # Using backwards compatible syntax
        parts = m.gsub(/[()]/, '').split(/\s+/).map(&:strip)
        "(#{parts[0]} #{parts[3]} #{parts[2]} #{parts[1]})"
      else
        "(#{parts[0]}, #{parts[3]}, #{parts[2]}, #{parts[1]})"
      end
    end
  else
    v
  end
end

.rtltr(v) ⇒ Object



91
92
93
# File 'lib/rtlize/declaration.rb', line 91

def rtltr(v)
  v == 'left' ? 'right' : v == 'right' ? 'left' : v
end

.shadow(v) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rtlize/declaration.rb', line 146

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(property, value) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rtlize/declaration.rb', line 65

def transform(property, value)
  # Get the property, without comments or spaces, to be able to find it.
  property_name = property.strip.split(' ').last.gsub(/^[*_]/, '')
  if @property_map[property_name]
    property = property.sub(property_name, @property_map[property_name])
  elsif @value_map[property_name]
    clean_value = value.sub(/;$/, '').sub(/\\9/, '').sub(/!\s*important/, '').strip
    value = value.sub(clean_value, self.send(@value_map[property_name], clean_value))
  end

  property + ':' + value
end

.transform_multiple(declarations) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rtlize/declaration.rb', line 78

def transform_multiple(declarations)
  declarations.split(/(;)(?!base64)/).map do |declaration|
    m = declaration.match(/([^:]+):(.+)/m)

    if m
      property, value = m[1..2]
      transform(property, value)
    else
      declaration
    end
  end.join
end