Class: Twine::Formatters::SwiftEscapingFormatter
- Inherits:
-
Abstract
- Object
- Abstract
- Twine::Formatters::SwiftEscapingFormatter
- 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
- #can_handle_directory?(path) ⇒ Boolean
- #default_file_name ⇒ Object
- #determine_language_given_path(path) ⇒ Object
- #extension ⇒ Object
- #format_comment(definition, lang) ⇒ Object
- #format_header(lang) ⇒ Object
- #format_key(key) ⇒ Object
- #format_name ⇒ Object
- #format_section_header(section) ⇒ Object
- #format_value(value) ⇒ Object
- #key_value_pattern ⇒ Object
- #output_path_for_language(lang) ⇒ Object
- #read(io, lang) ⇒ Object
Instance Method Details
#can_handle_directory?(path) ⇒ Boolean
115 116 117 |
# File 'lib/formatter.rb', line 115 def can_handle_directory?(path) Dir.entries(path).any? { |item| /^.+\.lproj$/.match(item) } end |
#default_file_name ⇒ Object
119 120 121 |
# File 'lib/formatter.rb', line 119 def default_file_name 'Localizable.strings' end |
#determine_language_given_path(path) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/formatter.rb', line 123 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 |
#extension ⇒ Object
111 112 113 |
# File 'lib/formatter.rb', line 111 def extension '.strings' end |
#format_comment(definition, lang) ⇒ Object
181 182 183 |
# File 'lib/formatter.rb', line 181 def format_comment(definition, lang) "/* #{definition.comment.gsub('*/', '* /')} */\n" if definition.comment end |
#format_header(lang) ⇒ Object
169 170 171 |
# File 'lib/formatter.rb', line 169 def format_header(lang) "//\n// Apple Strings File\n// Generated by Twine #{Twine::VERSION}\n//\n// Language: #{lang}\n//" end |
#format_key(key) ⇒ Object
185 186 187 |
# File 'lib/formatter.rb', line 185 def format_key(key) escape_quotes(key) end |
#format_name ⇒ Object
107 108 109 |
# File 'lib/formatter.rb', line 107 def format_name 'swift-escaping' end |
#format_section_header(section) ⇒ Object
173 174 175 |
# File 'lib/formatter.rb', line 173 def format_section_header(section) "// MARK: - #{section.name}\n" end |
#format_value(value) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/formatter.rb', line 189 def format_value(value) # added escaping for % (% -> %%) to support SwiftGen placeholder_syntax = PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH + PLACEHOLDER_TYPES single_percent_regex = /([^%])(%)(?!(%|#{placeholder_syntax}))/ value.gsub! single_percent_regex, '\1%%' escape_quotes(value) end |
#key_value_pattern ⇒ Object
177 178 179 |
# File 'lib/formatter.rb', line 177 def key_value_pattern "\"%{key}\" = \"%{value}\";" end |
#output_path_for_language(lang) ⇒ Object
139 140 141 |
# File 'lib/formatter.rb', line 139 def output_path_for_language(lang) "#{lang}.lproj" end |
#read(io, lang) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/formatter.rb', line 143 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 |