Class: Twine::TwineFile
- Inherits:
-
Object
- Object
- Twine::TwineFile
- Defined in:
- lib/twine/twine_file.rb
Instance Attribute Summary collapse
-
#definitions_by_key ⇒ Object
readonly
Returns the value of attribute definitions_by_key.
-
#language_codes ⇒ Object
readonly
Returns the value of attribute language_codes.
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
Instance Method Summary collapse
- #add_language_code(code) ⇒ Object
-
#initialize ⇒ TwineFile
constructor
A new instance of TwineFile.
- #read(path) ⇒ Object
- #set_developer_language_code(code) ⇒ Object
- #write(path) ⇒ Object
Constructor Details
#initialize ⇒ TwineFile
Returns a new instance of TwineFile.
79 80 81 82 83 |
# File 'lib/twine/twine_file.rb', line 79 def initialize @sections = [] @definitions_by_key = {} @language_codes = [] end |
Instance Attribute Details
#definitions_by_key ⇒ Object (readonly)
Returns the value of attribute definitions_by_key.
67 68 69 |
# File 'lib/twine/twine_file.rb', line 67 def definitions_by_key @definitions_by_key end |
#language_codes ⇒ Object (readonly)
Returns the value of attribute language_codes.
68 69 70 |
# File 'lib/twine/twine_file.rb', line 68 def language_codes @language_codes end |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
66 67 68 |
# File 'lib/twine/twine_file.rb', line 66 def sections @sections end |
Instance Method Details
#add_language_code(code) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/twine/twine_file.rb', line 85 def add_language_code(code) if @language_codes.length == 0 @language_codes << code elsif !@language_codes.include?(code) dev_lang = @language_codes[0] @language_codes << code @language_codes.delete(dev_lang) @language_codes.sort! @language_codes.insert(0, dev_lang) end end |
#read(path) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/twine/twine_file.rb', line 102 def read(path) if !File.file?(path) raise Twine::Error.new("File does not exist: #{path}") end File.open(path, 'r:UTF-8') do |f| line_num = 0 current_section = nil current_definition = nil while line = f.gets parsed = false line.strip! line_num += 1 if line.length == 0 next end if line.length > 4 && line[0, 2] == '[[' match = /^\[\[(.+)\]\]$/.match(line) if match current_section = TwineSection.new(match[1]) @sections << current_section parsed = true end elsif line.length > 2 && line[0, 1] == '[' key = match_key(line) if key current_definition = TwineDefinition.new(key) @definitions_by_key[current_definition.key] = current_definition if !current_section current_section = TwineSection.new('') @sections << current_section end current_section.definitions << current_definition parsed = true end else match = /^([^=]+)=(.*)$/.match(line) if match key = match[1].strip value = match[2].strip value = value[1..-2] if value[0] == '`' && value[-1] == '`' case key when 'comment' current_definition.comment = value when 'tags' current_definition. = value.split(',') when 'ref' current_definition.reference_key = value if value else if !@language_codes.include? key add_language_code(key) end current_definition.translations[key] = value end parsed = true end end if !parsed raise Twine::Error.new("Unable to parse line #{line_num} of #{path}: #{line}") end end end # resolve_references @definitions_by_key.each do |key, definition| next unless definition.reference_key definition.reference = @definitions_by_key[definition.reference_key] end end |
#set_developer_language_code(code) ⇒ Object
97 98 99 100 |
# File 'lib/twine/twine_file.rb', line 97 def set_developer_language_code(code) @language_codes.delete(code) @language_codes.insert(0, code) end |
#write(path) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/twine/twine_file.rb', line 177 def write(path) dev_lang = @language_codes[0] File.open(path, 'w:UTF-8') do |f| @sections.each do |section| if f.pos > 0 f.puts '' end f.puts "[[#{section.name}]]" section.definitions.each do |definition| f.puts "\t[#{definition.key}]" value = write_value(definition, dev_lang, f) if !value && !definition.reference_key Twine::stdout.puts "WARNING: #{definition.key} does not exist in developer language '#{dev_lang}'" end if definition.reference_key f.puts "\t\tref = #{definition.reference_key}" end if definition. && definition..length > 0 tag_str = definition..join(',') f.puts "\t\ttags = #{tag_str}" end if definition.raw_comment and definition.raw_comment.length > 0 f.puts "\t\tcomment = #{definition.raw_comment}" end @language_codes[1..-1].each do |lang| write_value(definition, lang, f) end end end end end |