Class: SC::Builder::Strings

Inherits:
Base show all
Defined in:
lib/sproutcore/builders/strings.rb

Overview

This builder is used to generate a file containing all of the loc strings for a particular manifest. The strings file is used when generating HTML try to map localized strings

Instance Attribute Summary

Attributes inherited from Base

#entry

Instance Method Summary collapse

Methods inherited from Base

build, #initialize, #joinlines, #read, #readlines, #replace_static_url, #sc_static_match, #static_url, #writeline, #writelinebinary, #writelines

Constructor Details

This class inherits a constructor from SC::Builder::Base

Instance Method Details

#build(dst_path) ⇒ Object



19
20
21
22
# File 'lib/sproutcore/builders/strings.rb', line 19

def build(dst_path)
  data = parse_strings_js(entry[:source_path])
  writelines dst_path, [data.to_json]
end

#parse_strings_js(source_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sproutcore/builders/strings.rb', line 24

def parse_strings_js(source_path)
  return {} if !File.exists?(source_path)

  # read the file in and strip out comments...
  str = File.read(source_path)
  str = str.gsub(/\/\/.*$/,'').gsub(/\/\*.*\*\//m,'')

  # Now build the hash
  ret = {}
  str.scan(/['"](.+)['"]\s*:\s*['"](.+)['"],?\s*$/) do |x,y|
    # x & y are JS strings that must be evaled as such..
    #x = eval(%("#{x}"))
    begin
      y = eval(%[<<__EOF__\n#{y}\n__EOF__]).chop
    rescue SyntaxError
      puts "Invalid string in #{source_path}:"
      puts $&
      exit
    end
    ret[x] = y
  end
  return ret
end