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
-
.callback_background(value, options: nil) ⇒ Array
Setup a background callback.
-
.callback_strike_through(value, options: nil) ⇒ Array
Setup a strike through callback.
-
.convert_color(value, options: nil) ⇒ String
Converts a color string.
-
.convert_float(value, options: nil) ⇒ Float
Converts a decimal number string.
-
.convert_size(value, options: nil) ⇒ Float
Converts a size string.
-
.convert_symbol(value, options: nil) ⇒ Symbol
Converts a string to symbol.
-
.copy_value(value, options: nil) ⇒ Object
Copy a value without conversion.
-
.filter_font_family(value, options: nil) ⇒ Symbol
Filter font family.
-
.normalize_style(value, accepted_values) ⇒ Symbol
Normalize a style value.
-
.unquote(value, options: nil) ⇒ String
Unquotes a string.
Class Method Details
.callback_background(value, options: nil) ⇒ Array
Setup a background callback
18 19 20 |
# File 'lib/prawn_html/utils.rb', line 18 def callback_background(value, options: nil) ['Background', convert_color(value, options: )] end |
.callback_strike_through(value, options: nil) ⇒ Array
Setup a strike through callback
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`.
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
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
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 && value&.include?('%') val.to_f * * 0.01 else val.to_f * PrawnHtml::PX end val.round(4) end |
.convert_symbol(value, options: nil) ⇒ Symbol
Converts a string to 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
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
108 109 110 111 |
# File 'lib/prawn_html/utils.rb', line 108 def filter_font_family(value, options: nil) result = unquote(value, options: ) result == 'inherit' ? nil : result end |
.normalize_style(value, accepted_values) ⇒ Symbol
Normalize a style value
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
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 |