Class: Twine::Formatters::Yaml

Inherits:
Abstract
  • Object
show all
Defined in:
lib/yaml-twine-formatter.rb

Instance Method Summary collapse

Instance Method Details

#can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/yaml-twine-formatter.rb', line 14

def can_handle_directory?(path)
  false
end

#default_file_nameObject



18
19
20
# File 'lib/yaml-twine-formatter.rb', line 18

def default_file_name
  'strings.yml'
end

#extensionObject



10
11
12
# File 'lib/yaml-twine-formatter.rb', line 10

def extension
  '.yml'
end

#format_header(lang) ⇒ Object



22
23
24
# File 'lib/yaml-twine-formatter.rb', line 22

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

#format_nameObject



6
7
8
# File 'lib/yaml-twine-formatter.rb', line 6

def format_name
  'yaml'
end

#format_sections(twine_file, lang) ⇒ Object



26
27
28
29
# File 'lib/yaml-twine-formatter.rb', line 26

def format_sections(twine_file, lang)
  sections = get_key_value_table(twine_file.sections, lang)
  YAML.dump(sections)
end

#get_key_value_table(sections, lang) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yaml-twine-formatter.rb', line 31

def get_key_value_table(sections, lang)
  table = {}
  
  sections.each do |section|
    section.definitions.each do |definition|
    	next unless definition.translations.include? lang
      table["[[#{section.name}]].[#{definition.key}]"] = definition.translations[lang]
    end
  end
  
  table
end

#read(io, lang) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/yaml-twine-formatter.rb', line 44

def read(io, lang)
  entries = YAML.load(io)
  entries.each do |key, value|
    tokens = /\[\[(.*)?\]\]\.\[(.*)\]/.match(key)
    section = tokens[1]
    def_key = tokens[2]
    set_translation_in_section(section, def_key, lang, value)
  end
end

#set_translation_in_section(section, key, lang, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/yaml-twine-formatter.rb', line 54

def set_translation_in_section(section, key, lang, value)
  value = value.gsub("\n", "\\n")

  if @twine_file.definitions_by_key.include?(key)
    definition = @twine_file.definitions_by_key[key]
    reference = @twine_file.definitions_by_key[definition.reference_key] if definition.reference_key
    
    ref_section = nil
    @twine_file.sections.each do |s|
      if s.definitions.find { |d| d.key == key }
        ref_section = s
        break
      end
    end
    
    if !ref_section or ref_section.name != section
      ref_section_name = (ref_section && ref_section.name) or ""
      Twine::stdout.puts "WARNING: '#{key}' in section [[#{section}]] found in different section [[#{ref_section_name}]]. Translation not written."
    elsif !reference or value != reference.translations[lang]
      definition.translations[lang] = value
    end
  elsif @options[:consume_all]
    Twine::stdout.puts "Adding new definition '#{key}' in section [[#{section}]] to twine file."
    current_section = @twine_file.sections.find { |s| s.name == section }
    unless current_section
      current_section = TwineSection.new(section)
      @twine_file.sections.insert(0, current_section)
    end
    current_definition = TwineDefinition.new(key)
    current_section.definitions << current_definition
    
    if @options[:tags] && @options[:tags].length > 0
      current_definition.tags = @options[:tags]            
    end
    
    @twine_file.definitions_by_key[key] = current_definition
    @twine_file.definitions_by_key[key].translations[lang] = value
  else
    Twine::stdout.puts "WARNING: '#{key}' not found in twine file."
  end

  if !@twine_file.language_codes.include?(lang)
    @twine_file.add_language_code(lang)
  end
end