Moved formatting sceleton code to Abstract.

This commit is contained in:
Sebastian Ludwig 2015-08-24 09:05:18 +02:00
parent 46d71accbc
commit eab588735f
2 changed files with 69 additions and 43 deletions

View file

@ -113,8 +113,72 @@ module Twine
raise NotImplementedError.new("You must implement read_file in your formatter class.")
end
def format_file(lang, default_lang)
result = format_header(lang) + "\n"
sections = @strings.sections.map { |section| format_section(section, lang, default_lang) }
result += sections.join("\n")
end
def format_header(lang)
raise NotImplementedError.new("You must implement format_header in your formatter class.")
end
def format_section_header(section)
end
def format_section(section, lang, default_lang)
rows = section.rows.select { |row| row.matches_tags?(@options[:tags], @options[:untagged]) }
result = ""
unless rows.empty?
if section.name && section.name.length > 0
section_header = format_section_header(section)
result += "\n#{section_header}\n" if section_header
end
end
rows.map! { |row| format_row(row, lang, default_lang) }
rows.compact! # remove nil entries
rows.map! { |row| "\n#{row}\n" } # wrap with newlines
result += rows.join
end
def format_row(row, lang, default_lang)
value = row.translated_string_for_lang(lang, default_lang)
return nil unless value
result = ""
if row.comment
comment = format_comment(row.comment)
result += comment + "\n" if comment
end
result += key_value_pattern % { key: format_key(row.key), value: format_value(value) }
end
def format_comment(comment)
end
def key_value_pattern
raise NotImplementedError.new("You must implement key_value_pattern in your formatter class.")
end
def format_key(key)
raise NotImplementedError.new("You must implement format_key in your formatter class.")
end
def format_value(value)
raise NotImplementedError.new("You must implement format_value in your formatter class.")
end
def write_file(path, lang)
raise NotImplementedError.new("You must implement write_file in your formatter class.")
default_lang = @strings.language_codes[0]
encoding = @options[:output_encoding] || 'UTF-8'
File.open(path, "w:#{encoding}") do |f|
f.puts format_file(lang, default_lang)
end
end
def write_all_files(path)

View file

@ -82,17 +82,6 @@ module Twine
end
end
def escape_quotes(text)
text.gsub('"', '\\\\"')
end
def format_file(lang, default_lang)
result = format_header(lang) + "\n"
sections = @strings.sections.map { |section| format_section(section, lang, default_lang) }
result += sections.join("\n")
end
def format_header(lang)
"/**\n * Apple Strings File\n * Generated by Twine #{Twine::VERSION}\n * Language: #{lang}\n */"
end
@ -101,29 +90,8 @@ module Twine
"/********** #{section.name} **********/" if section.name && section.name.length > 0
end
def format_section(section, lang, default_lang)
rows = section.rows.select { |row| row.matches_tags?(@options[:tags], @options[:untagged]) }
result = ""
unless rows.empty?
section_header = format_section_header(section)
result += "\n#{section_header}\n" if section_header
end
rows.map! { |row| format_row(row, lang, default_lang) }
rows.compact! # remove nil entries
rows.map! { |row| "\n#{row}\n" } # wrap with newlines
result += rows.join
end
def format_row(row, lang, default_lang)
value = row.translated_string_for_lang(lang, default_lang)
return nil unless value
result = ""
result += format_comment(row.comment) + "\n" if row.comment
result += "\"#{format_key(row.key)}\" = \"#{format_value(value)}\";"
def key_value_pattern
"\"%{key}\" = \"%{value}\";"
end
def format_comment(comment)
@ -138,14 +106,8 @@ module Twine
escape_quotes(value)
end
def write_file(path, lang)
default_lang = @strings.language_codes[0]
encoding = @options[:output_encoding] || 'UTF-8'
File.open(path, "w:#{encoding}") do |f|
f.puts format_file(lang, default_lang)
end
def escape_quotes(text)
text.gsub('"', '\\\\"')
end
end