Class: Twine::Formatters::Abstract
- Inherits:
-
Object
- Object
- Twine::Formatters::Abstract
show all
- Defined in:
- lib/twine/formatters/abstract.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#can_handle_directory?(path) ⇒ Boolean
-
#default_file_name ⇒ Object
-
#determine_language_given_path(path) ⇒ Object
-
#escape_quotes(text) ⇒ Object
-
#extension ⇒ Object
-
#format_comment(row, lang) ⇒ Object
-
#format_file(strings, lang) ⇒ Object
-
#format_header(lang) ⇒ Object
-
#format_key(key) ⇒ Object
-
#format_key_value(row, lang) ⇒ Object
-
#format_name ⇒ Object
-
#format_row(row, lang) ⇒ Object
-
#format_section(section, lang) ⇒ Object
-
#format_section_header(section) ⇒ Object
-
#format_sections(strings, lang) ⇒ Object
-
#format_value(value) ⇒ Object
-
#initialize ⇒ Abstract
constructor
A new instance of Abstract.
-
#key_value_pattern ⇒ Object
-
#output_path_for_language(lang) ⇒ Object
-
#read_file(path, lang) ⇒ Object
-
#set_comment_for_key(key, comment) ⇒ Object
-
#set_translation_for_key(key, lang, value) ⇒ Object
-
#should_include_row(row, lang) ⇒ Object
-
#write_all_files(path) ⇒ Object
-
#write_file(path, lang) ⇒ Object
Constructor Details
9
10
11
12
|
# File 'lib/twine/formatters/abstract.rb', line 9
def initialize
@strings = StringsFile.new
@options = {}
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
7
8
9
|
# File 'lib/twine/formatters/abstract.rb', line 7
def options
@options
end
|
#strings ⇒ Object
Returns the value of attribute strings.
6
7
8
|
# File 'lib/twine/formatters/abstract.rb', line 6
def strings
@strings
end
|
Instance Method Details
#can_handle_directory?(path) ⇒ Boolean
22
23
24
|
# File 'lib/twine/formatters/abstract.rb', line 22
def can_handle_directory?(path)
raise NotImplementedError.new("You must implement can_handle_directory? in your formatter class.")
end
|
#default_file_name ⇒ Object
26
27
28
|
# File 'lib/twine/formatters/abstract.rb', line 26
def default_file_name
raise NotImplementedError.new("You must implement default_file_name in your formatter class.")
end
|
#determine_language_given_path(path) ⇒ Object
78
79
80
|
# File 'lib/twine/formatters/abstract.rb', line 78
def determine_language_given_path(path)
raise NotImplementedError.new("You must implement determine_language_given_path in your formatter class.")
end
|
#escape_quotes(text) ⇒ Object
153
154
155
|
# File 'lib/twine/formatters/abstract.rb', line 153
def escape_quotes(text)
text.gsub('"', '\\\\"')
end
|
#extension ⇒ Object
18
19
20
|
# File 'lib/twine/formatters/abstract.rb', line 18
def extension
raise NotImplementedError.new("You must implement extension in your formatter class.")
end
|
133
134
|
# File 'lib/twine/formatters/abstract.rb', line 133
def (row, lang)
end
|
90
91
92
93
94
95
|
# File 'lib/twine/formatters/abstract.rb', line 90
def format_file(strings, lang)
= (lang)
result = ""
result += + "\n" if
result += format_sections(strings, lang)
end
|
97
98
|
# File 'lib/twine/formatters/abstract.rb', line 97
def (lang)
end
|
145
146
147
|
# File 'lib/twine/formatters/abstract.rb', line 145
def format_key(key)
key
end
|
136
137
138
139
|
# File 'lib/twine/formatters/abstract.rb', line 136
def format_key_value(row, lang)
value = row.translated_string_for_lang(lang)
key_value_pattern % { key: format_key(row.key.dup), value: format_value(value.dup) }
end
|
14
15
16
|
# File 'lib/twine/formatters/abstract.rb', line 14
def format_name
raise NotImplementedError.new("You must implement format_name in your formatter class.")
end
|
129
130
131
|
# File 'lib/twine/formatters/abstract.rb', line 129
def format_row(row, lang)
[(row, lang), format_key_value(row, lang)].compact.join
end
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/twine/formatters/abstract.rb', line 112
def format_section(section, lang)
rows = section.rows.select { |row| should_include_row(row, lang) }
return if rows.empty?
result = ""
if section.name && section.name.length > 0
= (section)
result += "\n#{section_header}" if
end
rows.map! { |row| format_row(row, lang) }
rows.compact!
rows.map! { |row| "\n#{row}" }
result += rows.join
end
|
105
106
|
# File 'lib/twine/formatters/abstract.rb', line 105
def (section)
end
|
100
101
102
103
|
# File 'lib/twine/formatters/abstract.rb', line 100
def format_sections(strings, lang)
sections = strings.sections.map { |section| format_section(section, lang) }
sections.compact.join("\n")
end
|
149
150
151
|
# File 'lib/twine/formatters/abstract.rb', line 149
def format_value(value)
value
end
|
#key_value_pattern ⇒ Object
141
142
143
|
# File 'lib/twine/formatters/abstract.rb', line 141
def key_value_pattern
raise NotImplementedError.new("You must implement key_value_pattern in your formatter class.")
end
|
#output_path_for_language(lang) ⇒ Object
82
83
84
|
# File 'lib/twine/formatters/abstract.rb', line 82
def output_path_for_language(lang)
lang
end
|
#read_file(path, lang) ⇒ Object
86
87
88
|
# File 'lib/twine/formatters/abstract.rb', line 86
def read_file(path, lang)
raise NotImplementedError.new("You must implement read_file in your formatter class.")
end
|
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/twine/formatters/abstract.rb', line 64
def (key, )
return unless @options[:consume_comments]
if @strings.strings_map.include?(key)
row = @strings.strings_map[key]
reference = @strings.strings_map[row.reference_key] if row.reference_key
if !reference or != reference.
row. =
end
end
end
|
#set_translation_for_key(key, lang, value) ⇒ Object
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
|
# File 'lib/twine/formatters/abstract.rb', line 30
def set_translation_for_key(key, lang, value)
value = value.gsub("\n", "\\n")
if @strings.strings_map.include?(key)
row = @strings.strings_map[key]
reference = @strings.strings_map[row.reference_key] if row.reference_key
if !reference or value != reference.translations[lang]
row.translations[lang] = value
end
elsif @options[:consume_all]
Twine::stderr.puts "Adding new string '#{key}' to strings data file."
current_section = @strings.sections.find { |s| s.name == 'Uncategorized' }
unless current_section
current_section = StringsSection.new('Uncategorized')
@strings.sections.insert(0, current_section)
end
current_row = StringsRow.new(key)
current_section.rows << current_row
if @options[:tags] && @options[:tags].length > 0
current_row.tags = @options[:tags]
end
@strings.strings_map[key] = current_row
@strings.strings_map[key].translations[lang] = value
else
Twine::stderr.puts "Warning: '#{key}' not found in strings data file."
end
if !@strings.language_codes.include?(lang)
@strings.add_language_code(lang)
end
end
|
#should_include_row(row, lang) ⇒ Object
108
109
110
|
# File 'lib/twine/formatters/abstract.rb', line 108
def should_include_row(row, lang)
row.translated_string_for_lang(lang)
end
|
#write_all_files(path) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/twine/formatters/abstract.rb', line 167
def write_all_files(path)
file_name = @options[:file_name] || default_file_name
if @options[:create_folders]
@strings.language_codes.each do |lang|
output_path = File.join(path, output_path_for_language(lang))
FileUtils.mkdir_p(output_path)
file_path = File.join(output_path, file_name)
write_file(file_path, lang)
end
else
language_written = false
Dir.foreach(path) do |item|
next if item == "." or item == ".."
item = File.join(path, item)
next unless File.directory?(item)
lang = determine_language_given_path(item)
next unless lang
file_path = File.join(item, file_name)
write_file(file_path, lang)
language_written = true
end
if !language_written
raise Twine::Error.new("Failed to generate any files: No languages found at #{path}")
end
end
end
|
#write_file(path, lang) ⇒ Object
157
158
159
160
161
162
163
164
165
|
# File 'lib/twine/formatters/abstract.rb', line 157
def write_file(path, lang)
output_processor = Processors::OutputProcessor.new(@strings, @options)
processed_strings = output_processor.process(lang)
encoding = @options[:output_encoding] || 'UTF-8'
File.open(path, "w:#{encoding}") do |f|
f.puts format_file(processed_strings, lang)
end
end
|