Class: ExtractI18n::Slimkeyfy::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/extract_i18n/slimkeyfy/word.rb

Direct Known Subclasses

JsWord

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Word

Returns a new instance of Word.



15
16
17
18
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 15

def initialize(line)
  @line = line
  @indentation = " " * (@line.size - unindented_line.size)
end

Instance Attribute Details

#indentationObject (readonly)

Returns the value of attribute indentation.



5
6
7
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 5

def indentation
  @indentation
end

#lineObject (readonly)

Returns the value of attribute line.



5
6
7
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 5

def line
  @line
end

#tokensObject (readonly)

Returns the value of attribute tokens.



5
6
7
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 5

def tokens
  @tokens
end

Class Method Details

.for(extension) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 7

def self.for(extension)
  if extension == 'vue'
    JsWord
  else
    Word
  end
end

Instance Method Details

#as_listObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 20

def as_list
  # check for div.foo-bar(foo=bar)
  has_html_form = !!@line[/^ *[\.#]?[a-z\.#-]+\(/]
  delimiter_items =
    @line.
    sub(/^(\s*\|)(\w)/, "\\1 \\2"). # add a whitespace to support "|string"
    split(has_html_form ? /(?<=[\(])| +/ : ' '). # split by whitespace or ( but keep (
    drop_while { |i| i == "" } # .. but that leaves leading ""
  items = []
  # div: div
  delimiter_items.reverse_each do |item|
    if item[/^([a-z]|\.[a-z]|#[a-z]).*:/] and items.length > 0
      items[-1] = "#{item} #{items[-1]}"
    else
      items << item
    end
  end
  items.reverse
end

#extract_arguments(translation) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 52

def extract_arguments(translation)
  args = {}
  translation.scan(/\#{[^}]*}/).each_with_index do |arg, index|
    stripped_arg = arg[2..-2]
    key = arg[/\w+/]
    key += index.to_s if index > 0
    translation = translation.gsub(arg, "%{#{key}}")
    args[key] = stripped_arg
  end
  [args, translation]
end

#extract_updated_key(translation_key_with_base) ⇒ Object



64
65
66
67
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 64

def extract_updated_key(translation_key_with_base)
  return "" if translation_key_with_base.blank?
  translation_key_with_base.split(".").last
end

#headObject



44
45
46
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 44

def head
  as_list.first
end

#tailObject



48
49
50
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 48

def tail
  as_list.drop(1)
end

#unindented_lineObject



40
41
42
# File 'lib/extract_i18n/slimkeyfy/word.rb', line 40

def unindented_line
  @line.sub(/^\s*/, "")
end