Class: I15R::PatternMatcher::HamlTransformer

Inherits:
Transformer
  • Object
show all
Defined in:
lib/i15r/pattern_matcher.rb

Instance Method Summary collapse

Methods inherited from Transformer

#initialize

Constructor Details

This class inherits a constructor from I15R::PatternMatcher::Transformer

Instance Method Details

#transform(match_data, match, line, translation_key) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/i15r/pattern_matcher.rb', line 112

def transform(match_data, match, line, translation_key)
  return line if line.match /\bt\(/
  leading_whitespace = line[/^(\s+)/, 1]
  no_leading_whitespace = if leading_whitespace
    line[leading_whitespace.size..-1]
  else
    line
  end
  if ['/', '-'].include?(no_leading_whitespace[0])
    return line
  end

  # Space can only occur in haml markup in an attribute list
  # enclosed in { } or ( ). If the first segment has { or (
  # we are still in the markup and need to go on to find the beginning
  # of the string to be replaced
  i = 0
  haml_segment = true
  attribute_list_start = nil
  segments = no_leading_whitespace.split(/\s+/)
  while haml_segment
    s = segments[i]
    if attribute_list_start
      attribute_list_end = [')', '}'].detect { |sym| s.index(sym) }
      if attribute_list_end
        haml_segment = false
      end
    else
      attribute_list_start = ['(', '{'].detect { |sym| s.index(sym) }
      unless attribute_list_start
        haml_segment = false
      end
    end
    i += 1
  end

  until_first_whitespace = segments[0...i].join(' ')
  if HAML_SYMBOLS.any? { |sym| until_first_whitespace.index(sym) }
    haml_markup = until_first_whitespace
    content = segments[i..-1].join(' ')
    if haml_markup[-1] == '='
      haml_markup += ' '
    else
      haml_markup += '= '
    end
  else
    haml_markup = ''
    content = no_leading_whitespace
    content.insert(0, '= ') unless content[0] == '='
  end

  new_line = (leading_whitespace or '') + haml_markup + content
  new_line.gsub(match.gsub(/\s+$/, ''), i18n_string(translation_key, match))
end