class Twine::Formatters::Abstract

Attributes

options[RW]
twine_file[RW]

Public Class Methods

new() click to toggle source
# File lib/twine/formatters/abstract.rb, line 9
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 22
def can_handle_directory?(path)
  raise NotImplementedError.new("You must implement can_handle_directory? in your formatter class.")
end
can_handle_file?(path) click to toggle source
# File lib/twine/formatters/abstract.rb, line 26
def can_handle_file?(path)
  raise NotImplementedError.new("You must implement can_handle_file? in your formatter class.")
end
default_file_name() click to toggle source
# File lib/twine/formatters/abstract.rb, line 30
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 89
def determine_language_given_path(path)
  raise NotImplementedError.new("You must implement determine_language_given_path in your formatter class.")
end
escape_quotes(text) click to toggle source
# File lib/twine/formatters/abstract.rb, line 169
def escape_quotes(text)
  text.gsub('"', "\\\"")
end
extension() click to toggle source
# File lib/twine/formatters/abstract.rb, line 18
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 149
def format_comment(definition, lang)
end
format_definition(definition, lang) click to toggle source
# File lib/twine/formatters/abstract.rb, line 145
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 101
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 113
def format_header(lang)
end
format_key(key) click to toggle source
# File lib/twine/formatters/abstract.rb, line 161
def format_key(key)
  key
end
format_key_value(definition, lang) click to toggle source
# File lib/twine/formatters/abstract.rb, line 152
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 14
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 128
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 121
def format_section_header(section)
end
format_sections(twine_file, lang) click to toggle source
# File lib/twine/formatters/abstract.rb, line 116
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 165
def format_value(value)
  value
end
get_section(name) click to toggle source
# File lib/twine/formatters/abstract.rb, line 177
def get_section(name)
  @twine_file.sections.each do |s|
    if s.name == name
      return s
    end
  end
  return nil
end
key_value_pattern() click to toggle source
# File lib/twine/formatters/abstract.rb, line 157
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 93
def output_path_for_language(lang)
  lang
end
read(io, lang) click to toggle source
# File lib/twine/formatters/abstract.rb, line 97
def read(io, lang)
  raise NotImplementedError.new("You must implement read in your formatter class.")
end
section_exists(section_name) click to toggle source
# File lib/twine/formatters/abstract.rb, line 173
def section_exists(section_name)
  @twine_file.sections.find { |s| s.name == section_name }
end
set_comment_for_key(key, comment) click to toggle source
# File lib/twine/formatters/abstract.rb, line 63
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_ios_comment_for_key(key, comment) click to toggle source
# File lib/twine/formatters/abstract.rb, line 77
def set_ios_comment_for_key(key, comment)
  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 ios_comment != reference.raw_ios_comment
      definition.ios_comment = comment
    end
  end
end
set_translation_for_key(current_section, key, lang, value) click to toggle source
# File lib/twine/formatters/abstract.rb, line 34
def set_translation_for_key(current_section, 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_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) click to toggle source
# File lib/twine/formatters/abstract.rb, line 124
def should_include_definition(definition, lang)
  return !definition.translation_for_lang(lang).nil?
end