Class: PrawnHtml::Tag

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/prawn_html/tag.rb

Constant Summary collapse

CALLBACKS =
{
  'Background' => Callbacks::Background,
  'StrikeThrough' => Callbacks::StrikeThrough
}.freeze
TAG_CLASSES =
%w[A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup U Ul].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, attributes: {}, options: {}) ⇒ Tag

Init the Tag

Parameters:

  • tag (Symbol)

    tag name

  • attributes (Hash) (defaults to: {})

    hash of element attributes

  • options (Hash) (defaults to: {})

    options (container width/height/etc.)



24
25
26
27
28
# File 'lib/prawn_html/tag.rb', line 24

def initialize(tag, attributes: {}, options: {})
  @tag = tag
  @options = options
  @attrs = Attributes.new(attributes)
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



17
18
19
# File 'lib/prawn_html/tag.rb', line 17

def attrs
  @attrs
end

#parentObject

Returns the value of attribute parent.



16
17
18
# File 'lib/prawn_html/tag.rb', line 16

def parent
  @parent
end

#tagObject (readonly)

Returns the value of attribute tag.



17
18
19
# File 'lib/prawn_html/tag.rb', line 17

def tag
  @tag
end

Class Method Details

.class_for(tag_name) ⇒ Tag

Evaluate the Tag class from a tag name

Returns:

  • (Tag)

    the class for the tag if available or nil



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

def class_for(tag_name)
  @tag_classes ||= TAG_CLASSES.each_with_object({}) do |tag_class, res|
    klass = const_get("PrawnHtml::Tags::#{tag_class}")
    k = [klass] * klass::ELEMENTS.size
    res.merge!(klass::ELEMENTS.zip(k).to_h)
  end
  @tag_classes[tag_name]
end

Instance Method Details

#block?Boolean

Is a block tag?

Returns:

  • (Boolean)

    true if the type of the tag is block, false otherwise



33
34
35
# File 'lib/prawn_html/tag.rb', line 33

def block?
  false
end

#block_stylesHash

Styles to apply to the block

Returns:

  • (Hash)

    hash of styles to apply



40
41
42
43
44
# File 'lib/prawn_html/tag.rb', line 40

def block_styles
  block_styles = styles.slice(*Attributes::STYLES_APPLY[:block])
  block_styles[:mode] = attrs.data['mode'].to_sym if attrs.data.include?('mode')
  block_styles
end

#process_styles(element_styles: nil) ⇒ Object

Process tag styles

Parameters:

  • element_styles (String) (defaults to: nil)

    extra styles to apply to the element



49
50
51
52
53
54
# File 'lib/prawn_html/tag.rb', line 49

def process_styles(element_styles: nil)
  attrs.merge_text_styles!(tag_styles, options: options) if respond_to?(:tag_styles)
  attrs.merge_text_styles!(element_styles, options: options) if element_styles
  attrs.merge_text_styles!(attrs.style, options: options)
  attrs.merge_text_styles!(extra_styles, options: options) if respond_to?(:extra_styles)
end

#tag_close_stylesHash

Styles to apply on tag closing

Returns:

  • (Hash)

    hash of styles to apply



59
60
61
# File 'lib/prawn_html/tag.rb', line 59

def tag_close_styles
  styles.slice(*Attributes::STYLES_APPLY[:tag_close])
end

#tag_open_stylesHash

Styles to apply on tag opening

Returns:

  • (Hash)

    hash of styles to apply



66
67
68
# File 'lib/prawn_html/tag.rb', line 66

def tag_open_styles
  styles.slice(*Attributes::STYLES_APPLY[:tag_open])
end