Class: Sablon::HTMLConverter::Node
- Inherits:
-
Object
- Object
- Sablon::HTMLConverter::Node
- Defined in:
- lib/sablon/html/ast.rb
Overview
A top level abstract class to handle common logic for all AST nodes
Constant Summary collapse
- PROPERTIES =
[].freeze
Class Method Summary collapse
-
.convert_style_property(key, value) ⇒ Object
handles conversion of a single attribute allowing recursion through super classes.
- .node_name ⇒ Object
-
.process_properties(properties) ⇒ Object
maps the CSS style property to it’s OpenXML equivalent.
-
.style_conversion ⇒ Object
Returns a hash defined on the configuration object by default.
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
-
#initialize(_env, _node, _properties) ⇒ Node
constructor
A new instance of Node.
-
#to_docx(tag) ⇒ Object
Simplifies usage at call sites by only requiring them to supply the tag name to use and any child AST nodes to render.
Constructor Details
#initialize(_env, _node, _properties) ⇒ Node
Returns a new instance of Node.
60 61 62 63 |
# File 'lib/sablon/html/ast.rb', line 60 def initialize(_env, _node, _properties) @properties ||= nil @attributes ||= {} end |
Class Method Details
.convert_style_property(key, value) ⇒ Object
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
48 49 50 51 52 53 54 55 56 57 58 |
# 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 |
.node_name ⇒ Object
11 12 13 |
# File 'lib/sablon/html/ast.rb', line 11 def self.node_name @node_name ||= name.split('::').last end |
.process_properties(properties) ⇒ Object
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).
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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 |
.style_conversion ⇒ Object
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
20 21 22 23 24 |
# 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 |
Instance Method Details
#accept(visitor) ⇒ Object
65 66 67 |
# File 'lib/sablon/html/ast.rb', line 65 def accept(visitor) visitor.visit(self) end |
#to_docx(tag) ⇒ Object
Simplifies usage at call sites by only requiring them to supply the tag name to use and any child AST nodes to render
71 72 73 74 75 |
# 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 |