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(definition, lang) ⇒ Object
-
#format_definition(definition, lang) ⇒ Object
-
#format_file(lang) ⇒ Object
-
#format_header(lang) ⇒ Object
-
#format_key(key) ⇒ Object
-
#format_key_value(definition, lang) ⇒ Object
-
#format_name ⇒ Object
-
#format_section(section, lang) ⇒ Object
-
#format_section_header(section) ⇒ Object
-
#format_sections(twine_file, 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(io, lang) ⇒ Object
-
#set_comment_for_key(key, comment) ⇒ Object
-
#set_translation_for_key(key, lang, value) ⇒ Object
-
#should_include_definition(definition, lang) ⇒ Object
Constructor Details
Returns a new instance of Abstract.
9
10
11
12
|
# File 'lib/twine/formatters/abstract.rb', line 9
def initialize
@twine_file = TwineFile.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
|
#twine_file ⇒ Object
Returns the value of attribute twine_file.
6
7
8
|
# File 'lib/twine/formatters/abstract.rb', line 6
def twine_file
@twine_file
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)
Dir.entries(path).any? { |item| /^.+#{Regexp.escape(extension)}$/.match(item) }
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
158
159
160
|
# File 'lib/twine/formatters/abstract.rb', line 158
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
|
138
139
|
# File 'lib/twine/formatters/abstract.rb', line 138
def (definition, lang)
end
|
134
135
136
|
# File 'lib/twine/formatters/abstract.rb', line 134
def format_definition(definition, lang)
[(definition, lang), format_key_value(definition, lang)].compact.join
end
|
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/twine/formatters/abstract.rb', line 90
def format_file(lang)
output_processor = Processors::OutputProcessor.new(@twine_file, @options)
processed_twine_file = output_processor.process(lang)
return nil if processed_twine_file.definitions_by_key.empty?
= (lang)
result = ""
result += + "\n" if
result += format_sections(processed_twine_file, lang)
end
|
102
103
|
# File 'lib/twine/formatters/abstract.rb', line 102
def (lang)
end
|
150
151
152
|
# File 'lib/twine/formatters/abstract.rb', line 150
def format_key(key)
key
end
|
141
142
143
144
|
# File 'lib/twine/formatters/abstract.rb', line 141
def format_key_value(definition, lang)
value = definition.translation_for_lang(lang)
key_value_pattern % { key: format_key(definition.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
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/twine/formatters/abstract.rb', line 117
def format_section(section, lang)
definitions = section.definitions.select { |definition| should_include_definition(definition, lang) }
return if definitions.empty?
result = ""
if section.name && section.name.length > 0
= (section)
result += "\n#{}" if
end
definitions.map! { |definition| format_definition(definition, lang) }
definitions.compact! definitions.map! { |definition| "\n#{definition}" } result += definitions.join
end
|
110
111
|
# File 'lib/twine/formatters/abstract.rb', line 110
def (section)
end
|
105
106
107
108
|
# File 'lib/twine/formatters/abstract.rb', line 105
def format_sections(twine_file, lang)
sections = twine_file.sections.map { |section| format_section(section, lang) }
sections.compact.join("\n")
end
|
154
155
156
|
# File 'lib/twine/formatters/abstract.rb', line 154
def format_value(value)
value
end
|
#key_value_pattern ⇒ Object
146
147
148
|
# File 'lib/twine/formatters/abstract.rb', line 146
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(io, lang) ⇒ Object
86
87
88
|
# File 'lib/twine/formatters/abstract.rb', line 86
def read(io, lang)
raise NotImplementedError.new("You must implement read 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 @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
if !reference or != reference.
definition. =
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 @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
if !reference or value != reference.translations[lang]
definition.translations[lang] = value
end
elsif @options[:consume_all]
Twine::stderr.puts "Adding new definition '#{key}' to twine file."
current_section = @twine_file.sections.find { |s| s.name == 'Uncategorized' }
unless current_section
current_section = TwineSection.new('Uncategorized')
@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::stderr.puts "Warning: '#{key}' not found in twine file."
end
if !@twine_file.language_codes.include?(lang)
@twine_file.add_language_code(lang)
end
end
|
#should_include_definition(definition, lang) ⇒ Object
113
114
115
|
# File 'lib/twine/formatters/abstract.rb', line 113
def should_include_definition(definition, lang)
return !definition.translation_for_lang(lang).nil?
end
|