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)?'
PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH =
'(\d+\$)?' + PLACEHOLDER_FLAGS_WIDTH_PRECISION_LENGTH
PLACEHOLDER_TYPES =
'[diufFeEgGxXoscpaA]'

Instance Method Summary collapse

Instance Method Details

#convert_placeholders_from_android_to_twine(input) ⇒ Object



52
53
54
55
56
57
# File 'lib/twine/placeholders.rb', line 52

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



70
71
72
# File 'lib/twine/placeholders.rb', line 70

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

#convert_placeholders_from_twine_to_android(input) ⇒ Object

Raises:



18
19
20
21
22
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
# File 'lib/twine/placeholders.rb', line 18

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

  placeholder_syntax = PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH + PLACEHOLDER_TYPES
  placeholder_regex = /%#{placeholder_syntax}/

  number_of_placeholders = value.scan(placeholder_regex).size

  return value if number_of_placeholders == 0

  # got placeholders -> need to double single percent signs
  # % -> %% (but %% -> %%, %d -> %d)
  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



61
62
63
64
65
66
67
68
# File 'lib/twine/placeholders.rb', line 61

def convert_placeholders_from_twine_to_flash(input)
  value = convert_twine_string_placeholder(input)

  placeholder_regex = /%#{PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH}#{PLACEHOLDER_TYPES}/
  value.gsub(placeholder_regex).each_with_index do |match, index|
    "{#{index}}"
  end
end

#convert_twine_string_placeholder(input) ⇒ Object



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

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