Class: Twine::Formatters::Flash

Inherits:
Abstract
  • Object
show all
Defined in:
lib/twine/formatters/flash.rb

Instance Attribute Summary

Attributes inherited from Abstract

#options, #strings

Instance Method Summary collapse

Methods inherited from Abstract

#escape_quotes, #format_file, #format_key, #format_key_value, #format_row, #format_section, #format_sections, #initialize, #output_path_for_language, #set_comment_for_key, #set_translation_for_key, #should_include_row, #write_all_files, #write_file

Constructor Details

This class inherits a constructor from Twine::Formatters::Abstract

Instance Method Details

#can_handle_directory?(path) ⇒ Boolean



12
13
14
# File 'lib/twine/formatters/flash.rb', line 12

def can_handle_directory?(path)
  return false
end

#default_file_nameObject



16
17
18
# File 'lib/twine/formatters/flash.rb', line 16

def default_file_name
  return 'resources.properties'
end

#determine_language_given_path(path) ⇒ Object



20
21
22
# File 'lib/twine/formatters/flash.rb', line 20

def determine_language_given_path(path)
  return
end

#extensionObject



8
9
10
# File 'lib/twine/formatters/flash.rb', line 8

def extension
  '.properties'
end

#format_comment(row, lang) ⇒ Object



85
86
87
# File 'lib/twine/formatters/flash.rb', line 85

def format_comment(row, lang)
  "# #{row.comment}\n" if row.comment
end

#format_header(lang) ⇒ Object



77
78
79
# File 'lib/twine/formatters/flash.rb', line 77

def format_header(lang)
  "## Flash Strings File\n## Generated by Twine #{Twine::VERSION}\n## Language: #{lang}"
end

#format_nameObject



4
5
6
# File 'lib/twine/formatters/flash.rb', line 4

def format_name
  'flash'
end

#format_section_header(section) ⇒ Object



81
82
83
# File 'lib/twine/formatters/flash.rb', line 81

def format_section_header(section)
  "## #{section.name} ##\n"
end

#format_value(value) ⇒ Object



93
94
95
96
# File 'lib/twine/formatters/flash.rb', line 93

def format_value(value)
  placeHolderNumber = -1
  value.gsub(/%[d@]/) { placeHolderNumber += 1; '{%d}' % placeHolderNumber }
end

#key_value_patternObject



89
90
91
# File 'lib/twine/formatters/flash.rb', line 89

def key_value_pattern
  "%{key}=%{value}"
end

#read_file(path, lang) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
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
69
70
71
72
73
74
75
# File 'lib/twine/formatters/flash.rb', line 24

def read_file(path, lang)
  encoding = Twine::Encoding.encoding_for_path(path)
  sep = nil
  if !encoding.respond_to?(:encode)
    # This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
    if encoding.end_with? 'LE'
      sep = "\x0a\x00"
    elsif encoding.end_with? 'BE'
      sep = "\x00\x0a"
    else
      sep = "\n"
    end
  end

  if encoding.index('UTF-16')
    mode = "rb:#{encoding}"
  else
    mode = "r:#{encoding}"
  end

  File.open(path, mode) do |f|
    last_comment = nil
    while line = (sep) ? f.gets(sep) : f.gets
      if encoding.index('UTF-16')
        if line.respond_to? :encode!
          line.encode!('UTF-8')
        else
          require 'iconv'
          line = Iconv.iconv('UTF-8', encoding, line).join
        end
      end
      match = /((?:[^"\\]|\\.)+)\s*=\s*((?:[^"\\]|\\.)*)/.match(line)
      if match
        key = match[1]
        value = match[2].strip
        value.gsub!(/\{[0-9]\}/, '%@')
        set_translation_for_key(key, lang, value)
        if last_comment
          set_comment_for_key(key, last_comment)
        end
      end
      
      match = /# *(.*)/.match(line)
      if match
        last_comment = match[1]
      else
        last_comment = nil
      end

    end
  end
end