Class: ExtractI18n::Adapters::SlimAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/extract_i18n/adapters/slim_adapter.rb

Direct Known Subclasses

VueAdapter

Instance Attribute Summary

Attributes inherited from Adapter

#file_key, #file_path, #on_ask, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapter

for, #initialize

Constructor Details

This class inherits a constructor from ExtractI18n::Adapters::Adapter

Class Method Details

.join_multiline(strings_array) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/extract_i18n/adapters/slim_adapter.rb', line 36

def self.join_multiline(strings_array)
  result = []
  joining_str = ''
  indent_length = 0
  long_str_start = /^[ ]+\| */
  long_str_indent = /^[ ]+/
  long_str_indent_with_vertical_bar = /^[ ]+\| */
  strings_array.each do |str|
    if joining_str.empty?
      if str[long_str_start]
        joining_str = str
        indent_length = str[long_str_start].length
      else
        result << str
      end
      # multiline string continues with spaces
    elsif str[long_str_indent] && str[long_str_indent].length.to_i >= indent_length
      joining_str << str.gsub(long_str_indent, ' ')
      # muliline string continues with spaces and vertical bar with same indentation
    elsif str[long_str_indent_with_vertical_bar] && str[long_str_indent_with_vertical_bar].length.to_i == indent_length
      joining_str << str.gsub(long_str_indent_with_vertical_bar, ' ')
      # multiline string ends
    else
      result << joining_str
      joining_str = ''
      indent_length = 0
      result << str
    end
  end
  result << joining_str unless joining_str.empty?

  result
end

.supports_relative_keys?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/extract_i18n/adapters/slim_adapter.rb', line 5

def self.supports_relative_keys?
  true
end

Instance Method Details

#process_line(old_line) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/extract_i18n/adapters/slim_adapter.rb', line 21

def process_line(old_line)
  word = ExtractI18n::Slimkeyfy::Word.for('.slim').new(old_line)
  ExtractI18n::Slimkeyfy::SlimTransformer.new(word, @file_key).transform do |change|
    if change.nil? # nothing to do
      return old_line
    end

    if @on_ask.call(change)
      change.i18n_t(relative: @options[:relative])
    else
      old_line
    end
  end
end

#run(original_content) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/extract_i18n/adapters/slim_adapter.rb', line 9

def run(original_content)
  @content = self.class.join_multiline(original_content.split("\n"))
  @content << "" # new line at the end
  @transformer = ExtractI18n::Slimkeyfy::SlimTransformer

  @new_content =
    @content.map do |old_line|
      process_line(old_line)
    end
  @new_content.join("\n")
end