Class: Applyrics::StringsFile::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/applyrics/stringsfile.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Parser

Returns a new instance of Parser.



37
38
39
40
41
42
43
# File 'lib/applyrics/stringsfile.rb', line 37

def initialize(hash)
  @hash = hash
  @property_regex = %r/\A(.*?)=(.*)\z/u
  @quote          = %r/"([^"]+)"/u
  @open_quote     = %r/\A\s*(".*)\z/u
  @comment_regex  = %r/\/\*([^*]+)\*\//u
end

Instance Method Details

#parse(data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/applyrics/stringsfile.rb', line 45

def parse(data)
  @hash.clear
  found_bad_bytes = false
  data.each_line do |line|
    @line = line.chomp
    if !found_bad_bytes
      first_bytes = @line[0..1].bytes.to_a
      if first_bytes.length == 2 && first_bytes[0] == 255
        @line = @line[2,@line.length].force_encoding('UTF-8')
        found_bad_bytes = true
      end
    end

    case @line
    when @comment_regex
      # Not implemented
    when @property_regex
      key = strip($1)
      value = strip($2)
      @hash[key] = value
    end
  end

  @hash
end

#strip(value) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/applyrics/stringsfile.rb', line 71

def strip(value)
  str = @quote.match(value.strip)
  if str.nil?
    value.strip
  else
    str[1]
  end
end