Module: Twine::Placeholders

Extended by:
Placeholders
Included in:
Formatters::Android, Formatters::Flash, Formatters::Tizen, Placeholders
Defined in:
lib/twine/placeholders.rb

Constant Summary collapse

PLACEHOLDER_FLAGS_WIDTH_PRECISION_LENGTH =

Note: the ‘ ` (single space) flag is NOT supported

'([-+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]'
PLACEHOLDER_REGEX =
/%#{PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH}#{PLACEHOLDER_TYPES}/

Instance Method Summary collapse

Instance Method Details

#convert_placeholders_from_android_to_twine(input) ⇒ Object



55
56
57
58
59
60
# File 'lib/twine/placeholders.rb', line 55

def convert_placeholders_from_android_to_twine(input)
  placeholder_regex = /(%#{PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH})s/

  # %s -> %@
  input.gsub(placeholder_regex, '\1@')
end

#convert_placeholders_from_flash_to_twine(input) ⇒ Object



72
73
74
# File 'lib/twine/placeholders.rb', line 72

def convert_placeholders_from_flash_to_twine(input)
  input.gsub /\{\d+\}/, '%@'
end

#convert_placeholders_from_twine_to_android(input) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/twine/placeholders.rb', line 23

def convert_placeholders_from_twine_to_android(input)
  # %@ -> %s
  value = convert_twine_string_placeholder(input)

  number_of_placeholders = number_of_twine_placeholders(value)

  return value if number_of_placeholders == 0

  # got placeholders -> need to double single percent signs
  # % -> %% (but %% -> %%, %d -> %d)
  placeholder_syntax = PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH + PLACEHOLDER_TYPES
  single_percent_regex = /([^%])(%)(?!(%|#{placeholder_syntax}))/
  value.gsub! single_percent_regex, '\1%%'

  return value if number_of_placeholders < 2

  # number placeholders
  non_numbered_placeholder_regex = /%(#{PLACEHOLDER_FLAGS_WIDTH_PRECISION_LENGTH}#{PLACEHOLDER_TYPES})/

  number_of_non_numbered_placeholders = value.scan(non_numbered_placeholder_regex).size

  return value if number_of_non_numbered_placeholders == 0

  raise Twine::Error.new("The value \"#{input}\" contains numbered and non-numbered placeholders") if number_of_placeholders != number_of_non_numbered_placeholders

  # %d -> %$1d
  index = 0
  value.gsub!(non_numbered_placeholder_regex) { "%#{index += 1}$#{$1}" }

  value
end

#convert_placeholders_from_twine_to_flash(input) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/twine/placeholders.rb', line 64

def convert_placeholders_from_twine_to_flash(input)
  value = convert_twine_string_placeholder(input)

  value.gsub(PLACEHOLDER_REGEX).each_with_index do |match, index|
    "{#{index}}"
  end
end

#convert_twine_string_placeholder(input) ⇒ Object



15
16
17
18
# File 'lib/twine/placeholders.rb', line 15

def convert_twine_string_placeholder(input)
  # %@ -> %s
  input.gsub(/(%#{PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH})@/, '\1s')
end

#number_of_twine_placeholders(input) ⇒ Object



11
12
13
# File 'lib/twine/placeholders.rb', line 11

def number_of_twine_placeholders(input)
  input.scan(PLACEHOLDER_REGEX).size
end