Module: PrawnHtml::Utils

Defined in:
lib/prawn_html/utils.rb

Constant Summary collapse

NORMALIZE_STYLES =
{
  'bold' => :bold,
  'italic' => :italic,
  'sub' => :subscript,
  'super' => :superscript,
  'underline' => :underline
}.freeze

Class Method Summary collapse

Class Method Details

.callback_background(value, options: nil) ⇒ Array

Setup a background callback

Parameters:

  • value (String)

    HTML string color

Returns:

  • (Array)

    callback name and argument value



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

def callback_background(value, options: nil)
  ['Background', convert_color(value, options: options)]
end

.callback_strike_through(value, options: nil) ⇒ Array

Setup a strike through callback

Parameters:

  • value (String)

    unused

Returns:

  • (Array)

    callback name and argument value



27
28
29
# File 'lib/prawn_html/utils.rb', line 27

def callback_strike_through(value, options: nil)
  ['StrikeThrough', nil]
end

.convert_color(value, options: nil) ⇒ String

Converts a color string

Supported formats:

  • 3 hex digits, ex. ‘color: #FB1`;

  • 6 hex digits, ex. ‘color: #abcdef`;

  • RGB, ex. ‘color: RGB(64, 0, 128)`;

  • color name, ex. ‘color: red`.

Parameters:

  • value (String)

    HTML string color

Returns:

  • (String)

    adjusted string color or nil if value is invalid



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/prawn_html/utils.rb', line 42

def convert_color(value, options: nil)
  val = value.to_s.strip.downcase
  return Regexp.last_match[1] if val.match /\A#([a-f0-9]{6})\Z/ # rubocop:disable Performance/RedundantMatch

  if val.match /\A#([a-f0-9]{3})\Z/ # rubocop:disable Performance/RedundantMatch
    r, g, b = Regexp.last_match[1].chars
    return (r * 2) + (g * 2) + (b * 2)
  end
  if val.match /\Argb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\Z/ # rubocop:disable Performance/RedundantMatch
    r, g, b = Regexp.last_match[1..3].map { |v| v.to_i.to_s(16) }
    return "#{r.rjust(2, '0')}#{g.rjust(2, '0')}#{b.rjust(2, '0')}"
  end

  COLORS[val]
end

.convert_float(value, options: nil) ⇒ Float

Converts a decimal number string

Parameters:

  • value (String)

    string decimal

Returns:

  • (Float)

    converted and rounded float number



63
64
65
66
# File 'lib/prawn_html/utils.rb', line 63

def convert_float(value, options: nil)
  val = value&.gsub(/[^0-9.]/, '') || ''
  val.to_f.round(4)
end

.convert_size(value, options: nil) ⇒ Float

Converts a size string

Parameters:

  • value (String)

    size string

  • options (Numeric) (defaults to: nil)

    container size

Returns:

  • (Float)

    converted and rounded size



74
75
76
77
78
79
80
81
82
83
# File 'lib/prawn_html/utils.rb', line 74

def convert_size(value, options: nil)
  val = value&.gsub(/[^0-9.]/, '') || ''
  val =
    if options && value&.include?('%')
      val.to_f * options * 0.01
    else
      val.to_f * PrawnHtml::PX
    end
  val.round(4)
end

.convert_symbol(value, options: nil) ⇒ Symbol

Converts a string to symbol

Parameters:

  • value (String)

    string

Returns:

  • (Symbol)

    symbol



90
91
92
# File 'lib/prawn_html/utils.rb', line 90

def convert_symbol(value, options: nil)
  value.to_sym if value && !value.match?(/\A\s*\Z/)
end

.copy_value(value, options: nil) ⇒ Object

Copy a value without conversion

Parameters:

  • value

Returns:

  • value



99
100
101
# File 'lib/prawn_html/utils.rb', line 99

def copy_value(value, options: nil)
  value
end

.filter_font_family(value, options: nil) ⇒ Symbol

Filter font family

Parameters:

  • value (String)

    string value

Returns:

  • (Symbol)

    unquoted font family or nil if the input value is ‘inherit’



108
109
110
111
# File 'lib/prawn_html/utils.rb', line 108

def filter_font_family(value, options: nil)
  result = unquote(value, options: options)
  result == 'inherit' ? nil : result
end

.normalize_style(value, accepted_values) ⇒ Symbol

Normalize a style value

Parameters:

  • value (String)

    string value

  • accepted_values (Array)

    allowlist of valid values (symbols)

Returns:

  • (Symbol)

    style value or nil



119
120
121
122
123
# File 'lib/prawn_html/utils.rb', line 119

def normalize_style(value, accepted_values)
  val = value&.strip&.downcase
  ret = NORMALIZE_STYLES[val]
  accepted_values.include?(ret) ? ret : nil
end

.unquote(value, options: nil) ⇒ String

Unquotes a string

Parameters:

  • value (String)

    string

Returns:

  • (String)

    string without quotes at the beginning/ending



130
131
132
133
134
# File 'lib/prawn_html/utils.rb', line 130

def unquote(value, options: nil)
  (value&.strip || +'').tap do |val|
    val.gsub!(/\A['"]|["']\Z/, '')
  end
end