class Sablon::HTMLConverter::Node
A top level abstract class to handle common logic for all AST nodes
Constants
- PROPERTIES
Public Class Methods
handles conversion of a single attribute allowing recursion through super classes. If the key exists and conversion is succesful a symbol is returned to avoid conflicts with a CSS prop sharing the same name. Keys without a conversion class are returned as is
# File lib/sablon/html/ast.rb, line 48 def self.convert_style_property(key, value) if style_conversion.key?(key) key, value = style_conversion[key].call(value) key = key.to_sym if key [key, value] elsif self == Node [key, value] else superclass.convert_style_property(key, value) end end
# File lib/sablon/html/ast.rb, line 60 def initialize(_env, _node, _properties) @properties ||= nil @attributes ||= {} end
# File lib/sablon/html/ast.rb, line 11 def self.node_name @node_name ||= name.split('::').last end
maps the CSS style property to it's OpenXML equivalent. Not all CSS properties have an equivalent, nor share the same behavior when defined on different node types (Paragraph
, Table
and Run
).
# File lib/sablon/html/ast.rb, line 29 def self.process_properties(properties) # process the styles as a hash and store values style_attrs = {} properties.each do |key, value| key = key.strip if key.respond_to? :strip value = value.strip if value.respond_to? :strip # unless key.is_a? Symbol key, value = *convert_style_property(key, value) end style_attrs[key] = value if key end style_attrs end
Returns a hash defined on the configuration object by default. However, this method can be overridden by subclasses to return a different node's style conversion config (i.e. :run) or a hash unrelated to the config itself. The config object is used for all built-in classes to allow for end-user customization via the configuration object
# File lib/sablon/html/ast.rb, line 20 def self.style_conversion # converts camelcase to underscored key = node_name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.to_sym Sablon::Configuration.instance.defined_style_conversions.fetch(key, {}) end
Public Instance Methods
# File lib/sablon/html/ast.rb, line 65 def accept(visitor) visitor.visit(self) end
Simplifies usage at call sites by only requiring them to supply the tag name to use and any child AST nodes to render
# File lib/sablon/html/ast.rb, line 71 def to_docx(tag) prop_str = @properties.to_docx if @properties # "<#{tag}#{attributes_to_docx}>#{prop_str}#{children_to_docx}</#{tag}>" end
Private Instance Methods
Gracefully handles conversion of an attributes hash into a string
# File lib/sablon/html/ast.rb, line 86 def attributes_to_docx return '' if @attributes.nil? || @attributes.empty? ' ' + @attributes.map { |k, v| %(#{k}="#{v}") }.join(' ') end
Acts like an abstract method allowing subclases full flexibility to define any content inside the tags.
# File lib/sablon/html/ast.rb, line 93 def children_to_docx '' end
Simplifies usage at call sites
# File lib/sablon/html/ast.rb, line 80 def transferred_properties @properties.transferred_properties end