Class: PrawnHtml::Attributes
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- PrawnHtml::Attributes
- Defined in:
- lib/prawn_html/attributes.rb
Constant Summary collapse
- STYLES_APPLY =
{ block: %i[align bottom leading left margin_left padding_left position right top], tag_close: %i[margin_bottom padding_bottom break_after], tag_open: %i[margin_top padding_top break_before], text_node: %i[callback character_spacing color font link list_style_type size styles white_space] }.freeze
- STYLES_LIST =
{ # text node styles 'background' => { key: :callback, set: :callback_background }, 'color' => { key: :color, set: :convert_color }, 'font-family' => { key: :font, set: :filter_font_family }, 'font-size' => { key: :size, set: :convert_size }, 'font-style' => { key: :styles, set: :append_styles, values: %i[italic] }, 'font-weight' => { key: :styles, set: :append_styles, values: %i[bold] }, 'href' => { key: :link, set: :copy_value }, 'letter-spacing' => { key: :character_spacing, set: :convert_float }, 'list-style-type' => { key: :list_style_type, set: :unquote }, 'text-decoration' => { key: :styles, set: :append_styles, values: %i[underline] }, 'vertical-align' => { key: :styles, set: :append_styles, values: %i[subscript superscript] }, 'white-space' => { key: :white_space, set: :convert_symbol }, # tag opening styles 'break-before' => { key: :break_before, set: :convert_symbol }, 'margin-top' => { key: :margin_top, set: :convert_size }, 'padding-top' => { key: :padding_top, set: :convert_size }, # tag closing styles 'break-after' => { key: :break_after, set: :convert_symbol }, 'margin-bottom' => { key: :margin_bottom, set: :convert_size }, 'padding-bottom' => { key: :padding_bottom, set: :convert_size }, # block styles 'bottom' => { key: :bottom, set: :convert_size, options: :height }, 'left' => { key: :left, set: :convert_size, options: :width }, 'line-height' => { key: :leading, set: :convert_size }, 'margin-left' => { key: :margin_left, set: :convert_size }, 'padding-left' => { key: :padding_left, set: :convert_size }, 'position' => { key: :position, set: :convert_symbol }, 'right' => { key: :right, set: :convert_size, options: :width }, 'text-align' => { key: :align, set: :convert_symbol }, 'top' => { key: :top, set: :convert_size, options: :height }, # special styles 'text-decoration-line-through' => { key: :callback, set: :callback_strike_through } }.freeze
- STYLES_MERGE =
%i[margin_left padding_left].freeze
Instance Attribute Summary collapse
-
#initial ⇒ Object
readonly
Returns the value of attribute initial.
-
#styles ⇒ Object
readonly
Returns the value of attribute styles.
Class Method Summary collapse
-
.merge_attr!(attributes, key, value) ⇒ Hash
Merges attributes.
-
.parse_styles(styles) ⇒ Hash
Parses a string of styles.
Instance Method Summary collapse
-
#data ⇒ Hash
Processes the data attributes.
-
#initialize(attributes = {}) ⇒ Attributes
constructor
Init the Attributes.
-
#merge_text_styles!(text_styles, options: {}) ⇒ Object
Merge text styles.
-
#remove_value(context_styles, rule) ⇒ Object
Remove an attribute value from the context styles.
-
#update_styles(context_styles) ⇒ Hash
Update context styles applying the initial rules (if set).
Constructor Details
#initialize(attributes = {}) ⇒ Attributes
Init the Attributes
56 57 58 59 60 |
# File 'lib/prawn_html/attributes.rb', line 56 def initialize(attributes = {}) super @styles = {} # result styles @initial = Set.new end |
Instance Attribute Details
#initial ⇒ Object (readonly)
Returns the value of attribute initial.
8 9 10 |
# File 'lib/prawn_html/attributes.rb', line 8 def initial @initial end |
#styles ⇒ Object (readonly)
Returns the value of attribute styles.
8 9 10 |
# File 'lib/prawn_html/attributes.rb', line 8 def styles @styles end |
Class Method Details
.merge_attr!(attributes, key, value) ⇒ Hash
Merges attributes
116 117 118 119 120 121 122 |
# File 'lib/prawn_html/attributes.rb', line 116 def merge_attr!(attributes, key, value) return unless key return (attributes[key] = value) unless Attributes::STYLES_MERGE.include?(key) attributes[key] ||= 0 attributes[key] += value end |
.parse_styles(styles) ⇒ Hash
Parses a string of styles
129 130 131 |
# File 'lib/prawn_html/attributes.rb', line 129 def parse_styles(styles) (styles || '').scan(/\s*([^:;]+)\s*:\s*([^;]+)\s*/).to_h end |
Instance Method Details
#data ⇒ Hash
Processes the data attributes
65 66 67 68 69 70 |
# File 'lib/prawn_html/attributes.rb', line 65 def data to_h.each_with_object({}) do |(key, value), res| data_key = key.match /\Adata-(.+)/ res[data_key[1]] = value.strip if data_key end end |
#merge_text_styles!(text_styles, options: {}) ⇒ Object
Merge text styles
76 77 78 79 |
# File 'lib/prawn_html/attributes.rb', line 76 def merge_text_styles!(text_styles, options: {}) hash_styles = Attributes.parse_styles(text_styles) process_styles(hash_styles, options: ) unless hash_styles.empty? end |
#remove_value(context_styles, rule) ⇒ Object
Remove an attribute value from the context styles
85 86 87 88 89 90 91 92 |
# File 'lib/prawn_html/attributes.rb', line 85 def remove_value(context_styles, rule) if rule[:set] == :append_styles context_styles[rule[:key]] -= rule[:values] if context_styles[:styles] else default = Context::DEFAULT_STYLES[rule[:key]] default ? (context_styles[rule[:key]] = default) : context_styles.delete(rule[:key]) end end |
#update_styles(context_styles) ⇒ Hash
Update context styles applying the initial rules (if set)
99 100 101 102 103 104 105 106 |
# File 'lib/prawn_html/attributes.rb', line 99 def update_styles(context_styles) initial.each do |rule| next unless rule remove_value(context_styles, rule) end context_styles end |