Class: Traduki::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/traduki/parser.rb

Constant Summary collapse

REGEXES =
{
  :'.m'     => /__\([\n\s]*@"([a-zA-Z0-9.-]+)"/,
  :'.mm'    => /__\([\n\s]*[@]?"([a-zA-Z0-9.-]+)/,
  :'.swift' => /__\([\n\s]*"([a-zA-Z0-9.-]+)"(?:[\n\s]*,[\n\s]*"([^"]*)")*([\n\s]*,[\n\s]*\[(?:[\n\s]*,*[\n\s]*"[^"]*"[\n\s]*:[\n\s]*[^,]+)*[\n\s]*\])*/,
  :'.js'    => /__\([\n\s]*["']([a-zA-Z0-9.-]+)["']/,
  :'.py'    => /__\([\n\s]*["']([a-zA-Z0-9.-]+)["']/
}.freeze
PARAM_REGEXS =
{
  :'.m'     => /"([^"]+)"[\n\s]*:/,
  :'.mm'    => /"([^"]+)"[\n\s]*:/,
  :'.swift' => /"([^"]+)"[\n\s]*:/,
  :'.js'    => /"([^"]+)"[\n\s]*:/,
  :'.py'    => /"([^"]+)"[\n\s]*:/
}.freeze
SUPPORTED_TYPE =
%w[.m .mm .swift .js .py].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



26
27
28
29
30
# File 'lib/traduki/parser.rb', line 26

def initialize
  @keys      = {}
  @contrasts = {}
  parse
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



24
25
26
# File 'lib/traduki/parser.rb', line 24

def keys
  @keys
end

Class Method Details

.extract(file_path) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/traduki/parser.rb', line 49

def self.extract(file_path)
  file    = open file_path
  extname = File.extname(file.path)
  return nil unless SUPPORTED_TYPE.include? extname
  regex = REGEXES[extname.to_sym]
  file.read.scan(regex).map do |k|
    k[2] = scan_params(k[2], extname) if k[2]
    k
  end
end

.scan_params(params, extname) ⇒ Object



60
61
62
# File 'lib/traduki/parser.rb', line 60

def self.scan_params(params, extname)
  params.scan(PARAM_REGEXS[extname.to_sym]).flatten
end

Instance Method Details

#keys_countObject



64
65
66
67
68
# File 'lib/traduki/parser.rb', line 64

def keys_count
  count = 0
  @keys.each { |_, v| count += v.count }
  count
end

#parseObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/traduki/parser.rb', line 32

def parse
  @keys = {}
  Dir.glob(SUPPORTED_TYPE.map { |s| "#{Traduki.config.workdir}/**/*#{s}" }) do |file_path|
    unless File.directory? file_path
      keys                   = Parser.extract(file_path)
      @keys[slice file_path] = keys unless keys.empty?
    end
  end
  self
end

#slice(path) ⇒ Object



43
44
45
46
47
# File 'lib/traduki/parser.rb', line 43

def slice(path)
  newpath = path.clone.path
  newpath.slice!(Traduki.config.workdir)
  newpath
end