class Twine::Formatters::Abstract
Constants
- LANGUAGE_CODE_WITH_OPTIONAL_REGION_CODE
Attributes
options[RW]
twine_file[RW]
Public Class Methods
new()
click to toggle source
# File lib/twine/formatters/abstract.rb, line 11 def initialize @twine_file = TwineFile.new @options = {} end
Public Instance Methods
can_handle_directory?(path)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 24 def can_handle_directory?(path) Dir.entries(path).any? { |item| /^.+#{Regexp.escape(extension)}$/.match(item) } end
default_file_name()
click to toggle source
# File lib/twine/formatters/abstract.rb, line 28 def default_file_name raise NotImplementedError.new("You must implement default_file_name in your formatter class.") end
determine_language_given_path(path)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 80 def determine_language_given_path(path) only_language_and_region = /^#{LANGUAGE_CODE_WITH_OPTIONAL_REGION_CODE}$/i basename = File.basename(path, File.extname(path)) return basename if basename =~ only_language_and_region return basename if @twine_file.language_codes.include? basename path.split(File::SEPARATOR).reverse.find { |segment| segment =~ only_language_and_region } end
escape_quotes(text)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 165 def escape_quotes(text) text.gsub('"', '\\\\"') end
extension()
click to toggle source
# File lib/twine/formatters/abstract.rb, line 20 def extension raise NotImplementedError.new("You must implement extension in your formatter class.") end
format_comment(definition, lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 145 def format_comment(definition, lang) end
format_definition(definition, lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 141 def format_definition(definition, lang) [format_comment(definition, lang), format_key_value(definition, lang)].compact.join end
format_file(lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 97 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? header = format_header(lang) result = "" result += header + "\n" if header result += format_sections(processed_twine_file, lang) end
format_header(lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 109 def format_header(lang) end
format_key(key)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 157 def format_key(key) key end
format_key_value(definition, lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 148 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
format_name()
click to toggle source
# File lib/twine/formatters/abstract.rb, line 16 def format_name raise NotImplementedError.new("You must implement format_name in your formatter class.") end
format_section(section, lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 124 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_header = format_section_header(section) result += "\n#{section_header}" if section_header end definitions.map! { |definition| format_definition(definition, lang) } definitions.compact! # remove nil definitions definitions.map! { |definition| "\n#{definition}" } # prepend newline result += definitions.join end
format_section_header(section)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 117 def format_section_header(section) end
format_sections(twine_file, lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 112 def format_sections(twine_file, lang) sections = twine_file.sections.map { |section| format_section(section, lang) } sections.compact.join("\n") end
format_value(value)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 161 def format_value(value) value end
key_value_pattern()
click to toggle source
# File lib/twine/formatters/abstract.rb, line 153 def key_value_pattern raise NotImplementedError.new("You must implement key_value_pattern in your formatter class.") end
output_path_for_language(lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 89 def output_path_for_language(lang) lang end
read(io, lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 93 def read(io, lang) raise NotImplementedError.new("You must implement read in your formatter class.") end
set_comment_for_key(key, comment)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 66 def set_comment_for_key(key, comment) 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 comment != reference.raw_comment definition.comment = comment end end end
set_translation_for_key(key, lang, value)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 32 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::stdout.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::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
should_include_definition(definition, lang)
click to toggle source
# File lib/twine/formatters/abstract.rb, line 120 def should_include_definition(definition, lang) return !definition.translation_for_lang(lang).nil? end