Class: Twine::Formatters::SwiftFormatter

Inherits:
Abstract
  • Object
show all
Defined in:
lib/formatter.rb

Constant Summary collapse

PLACEHOLDER_FLAGS_WIDTH_PRECISION_LENGTH =
'([-+0#])?(\d+|\*)?(\.(\d+|\*))?(hh?|ll?|L|z|j|t|q)?'
PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH =
'(\d+\$)?' + PLACEHOLDER_FLAGS_WIDTH_PRECISION_LENGTH
PLACEHOLDER_TYPES =
'[diufFeEgGxXoscpaA@]'

Instance Method Summary collapse

Instance Method Details

#can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/formatter.rb', line 22

def can_handle_directory?(path)
  Dir.entries(path).any? { |item| /^.+\.lproj$/.match(item) }
end

#default_file_nameObject



26
27
28
# File 'lib/formatter.rb', line 26

def default_file_name
  'Localizable.strings'
end

#determine_language_given_path(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/formatter.rb', line 30

def determine_language_given_path(path)
  path_arr = path.split(File::SEPARATOR)
  path_arr.each do |segment|
    match = /^(.+)\.lproj$/.match(segment)
    if match
      if match[1] == "Base"
        return @options[:developer_language]
      else
        return match[1]
      end
    end
  end

  return super
end

#extensionObject



18
19
20
# File 'lib/formatter.rb', line 18

def extension
  '.strings'
end

#format_comment(definition, lang) ⇒ Object



88
89
90
# File 'lib/formatter.rb', line 88

def format_comment(definition, lang)
  "/* #{definition.comment.gsub('*/', '* /')} */\n" if definition.comment
end

#format_header(lang) ⇒ Object



76
77
78
# File 'lib/formatter.rb', line 76

def format_header(lang)
  "//\n// Apple Strings File\n// Generated by Twine #{Twine::VERSION}\n//\n// Language: #{lang}\n//"
end

#format_key(key) ⇒ Object



92
93
94
# File 'lib/formatter.rb', line 92

def format_key(key)
  escape_quotes(key)
end

#format_nameObject



14
15
16
# File 'lib/formatter.rb', line 14

def format_name
  'swift'
end

#format_section_header(section) ⇒ Object



80
81
82
# File 'lib/formatter.rb', line 80

def format_section_header(section)
  "// MARK: - #{section.name}\n"
end

#format_value(value) ⇒ Object



96
97
98
# File 'lib/formatter.rb', line 96

def format_value(value)
    escape_quotes(value)
end

#key_value_patternObject



84
85
86
# File 'lib/formatter.rb', line 84

def key_value_pattern
  "\"%{key}\" = \"%{value}\";"
end

#output_path_for_language(lang) ⇒ Object



46
47
48
# File 'lib/formatter.rb', line 46

def output_path_for_language(lang)
  "#{lang}.lproj"
end

#read(io, lang) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/formatter.rb', line 50

def read(io, lang)
  last_comment = nil
  while line = io.gets
    # matches a `key = "value"` line, where key may be quoted or unquoted. The former may also contain escaped characters
    match = /^\s*((?:"(?:[^"\\]|\\.)+")|(?:[^"\s=]+))\s*=\s*"((?:[^"\\]|\\.)*)"/.match(line)
    if match
      key = match[1]
      key = key[1..-2] if key[0] == '"' and key[-1] == '"'
      key.gsub!('\\"', '"')
      value = match[2]
      value.gsub!('\\"', '"')
      set_translation_for_key(key, lang, value)
      if last_comment
        set_comment_for_key(key, last_comment)
      end
    end

    match = /\/\* (.*) \*\//.match(line)
    if match
      last_comment = match[1]
    else
      last_comment = nil
    end
  end
end