Class: Twine::Formatters::Flash

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

Constant Summary

Constants included from Placeholders

Placeholders::PLACEHOLDER_FLAGS_WIDTH_PRECISION_LENGTH, Placeholders::PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH, Placeholders::PLACEHOLDER_REGEX, Placeholders::PLACEHOLDER_TYPES

Instance Attribute Summary

Attributes inherited from Abstract

#options, #twine_file

Instance Method Summary collapse

Methods included from Placeholders

#convert_placeholders_from_android_to_twine, #convert_placeholders_from_flash_to_twine, #convert_placeholders_from_twine_to_android, #convert_placeholders_from_twine_to_flash, #convert_twine_string_placeholder, #number_of_twine_placeholders

Methods inherited from Abstract

#can_handle_directory?, #escape_quotes, #format_definition, #format_file, #format_key, #format_key_value, #format_section, #initialize, #output_path_for_language, #set_comment_for_key, #should_include_definition

Constructor Details

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

Instance Method Details

#default_file_nameObject



14
15
16
# File 'lib/twine/formatters/flash.rb', line 14

def default_file_name
  'resources.properties'
end

#determine_language_given_path(path) ⇒ Object



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

def determine_language_given_path(path)
  # match two-letter language code, optionally followed by a two letter region code
  path.split(File::SEPARATOR).reverse.find { |segment| segment =~ /^([a-z]{2}(-[a-z]{2})?)$/i }
end

#extensionObject



10
11
12
# File 'lib/twine/formatters/flash.rb', line 10

def extension
  '.properties'
end

#format_comment(definition, lang) ⇒ Object



57
58
59
# File 'lib/twine/formatters/flash.rb', line 57

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

#format_header(lang) ⇒ Object



49
50
51
# File 'lib/twine/formatters/flash.rb', line 49

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

#format_nameObject



6
7
8
# File 'lib/twine/formatters/flash.rb', line 6

def format_name
  'flash'
end

#format_section_header(section) ⇒ Object



53
54
55
# File 'lib/twine/formatters/flash.rb', line 53

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

#format_sections(twine_file, lang) ⇒ Object



45
46
47
# File 'lib/twine/formatters/flash.rb', line 45

def format_sections(twine_file, lang)
  super + "\n"
end

#format_value(value) ⇒ Object



65
66
67
# File 'lib/twine/formatters/flash.rb', line 65

def format_value(value)
  convert_placeholders_from_twine_to_flash(value)
end

#key_value_patternObject



61
62
63
# File 'lib/twine/formatters/flash.rb', line 61

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

#read(io, lang) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/twine/formatters/flash.rb', line 28

def read(io, lang)
  last_comment = nil
  while line = io.gets
    match = /((?:[^"\\]|\\.)+)\s*=\s*((?:[^"\\]|\\.)*)/.match(line)
    if match
      key = match[1]
      value = match[2].strip

      set_translation_for_key(key, lang, value)
      set_comment_for_key(key, last_comment) if last_comment
    end
    
    match = /# *(.*)/.match(line)
    last_comment = match ? match[1] : nil
  end
end

#set_translation_for_key(key, lang, value) ⇒ Object



23
24
25
26
# File 'lib/twine/formatters/flash.rb', line 23

def set_translation_for_key(key, lang, value)
  value = convert_placeholders_from_flash_to_twine(value)
  super(key, lang, value)
end