Class: Sablon::Configuration::HTMLTag

Inherits:
Object
  • Object
show all
Defined in:
lib/sablon/configuration/html_tag.rb

Overview

Stores the information for a single HTML tag. This information is used by the HTMLConverter. An optional AST class can be defined, and if so conversion stops there and it is assumed the AST class will handle any child nodes unless the element is a block level tag. In the case of a block level tag the child nodes are processed by the AST builder again. If the AST class is omitted it is assumed the node should be “passed through” only transferring it’s properties onto children. A block level tag must have an AST class associated with it. The block and inline status of tags is not affected by CSS. Permitted child tags are specified using the :allowed_children optional arg. The default value is [:_inline, :ul, :ol]. :_inline is a special reference to all inline type tags, :_block is equivalent for block type tags.

Parameters

* name - symbol or string of the HTML element tag name
* type - The type of HTML tag needs to be :inline or :block
* ast_class - class instance or symbol, the AST class or it's name
              used to process the HTML node
* options - collects all other keyword arguments, Current kwargs are
            `:properties`, `:attributes` and `:allowed_children`.

Example

HTMLTag.new(:div, :block, ast_class: Sablon::HTMLConverter::Paragraph,
            properties: { pStyle: 'Normal' })

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, ast_class: nil, **options) ⇒ HTMLTag

Setup HTML tag information



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sablon/configuration/html_tag.rb', line 33

def initialize(name, type, ast_class: nil, **options)
  # Set basic params converting some args to symbols for consistency
  @name = name.to_sym
  @type = type.to_sym
  @ast_class = nil
  # use self.ast_class to trigger setter method
  self.ast_class = ast_class if ast_class

  # Ensure block level tags have an AST class
  if @type == :block && @ast_class.nil?
    raise ArgumentError, "Block level tag #{name} must have an AST class."
  end

  # Set attributes from optinos hash, currently unused during AST generation
  @attributes = options.fetch(:attributes, {})
  # WordML properties defined by the tag, i.e. <w:b /> for the <b> tag,
  # etc. All the keys need to be symbols to avoid getting reparsed
  # with the element's CSS attributes.
  @properties = options.fetch(:properties, {})
  @properties = Hash[@properties.map { |k, v| [k.to_s, v] }]
  # Set permitted child tags or tag groups
  self.allowed_children = options[:allowed_children]
end

Instance Attribute Details

#allowed_childrenObject

Returns the value of attribute allowed_children.



29
30
31
# File 'lib/sablon/configuration/html_tag.rb', line 29

def allowed_children
  @allowed_children
end

#ast_classObject

Returns the value of attribute ast_class.



29
30
31
# File 'lib/sablon/configuration/html_tag.rb', line 29

def ast_class
  @ast_class
end

#attributesObject (readonly)

Returns the value of attribute attributes.



29
30
31
# File 'lib/sablon/configuration/html_tag.rb', line 29

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/sablon/configuration/html_tag.rb', line 29

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



29
30
31
# File 'lib/sablon/configuration/html_tag.rb', line 29

def properties
  @properties
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/sablon/configuration/html_tag.rb', line 29

def type
  @type
end

Instance Method Details

#allowed_child?(tag) ⇒ Boolean

checks if the given tag is a permitted child element

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sablon/configuration/html_tag.rb', line 58

def allowed_child?(tag)
  if @allowed_children.include?(tag.name)
    true
  elsif @allowed_children.include?(:_inline) && tag.type == :inline
    true
  elsif @allowed_children.include?(:_block) && tag.type == :block
    true
  else
    false
  end
end