Class: Sablon::Configuration::HTMLTag
- Inherits:
-
Object
- Object
- Sablon::Configuration::HTMLTag
- 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
-
#allowed_children ⇒ Object
readonly
Returns the value of attribute allowed_children.
-
#ast_class ⇒ Object
readonly
Returns the value of attribute ast_class.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#allowed_child?(tag) ⇒ Boolean
checks if the given tag is a permitted child element.
-
#initialize(name, type, ast_class: nil, **options) ⇒ HTMLTag
constructor
Setup HTML tag information.
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, **) # 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 = .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 = .fetch(:properties, {}) @properties = Hash[@properties.map { |k, v| [k.to_s, v] }] # Set permitted child tags or tag groups self.allowed_children = [:allowed_children] end |
Instance Attribute Details
#allowed_children ⇒ Object
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_class ⇒ Object
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 |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
29 30 31 |
# File 'lib/sablon/configuration/html_tag.rb', line 29 def attributes @attributes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/sablon/configuration/html_tag.rb', line 29 def name @name end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
29 30 31 |
# File 'lib/sablon/configuration/html_tag.rb', line 29 def properties @properties end |
#type ⇒ Object (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
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 |