class Sablon::HTMLConverter::NodeProperties

Manages the properties for an AST node, includes factory methods for easy use at calling sites.

Attributes

transferred_properties[R]

Public Class Methods

new(tagname, properties, whitelist) click to toggle source
# File lib/sablon/html/node_properties.rb, line 28
def initialize(tagname, properties, whitelist)
  @tagname = tagname
  filter_properties(properties, whitelist)
end
paragraph(properties) click to toggle source
# File lib/sablon/html/node_properties.rb, line 8
def self.paragraph(properties)
  new('w:pPr', properties, Paragraph::PROPERTIES)
end
run(properties) click to toggle source
# File lib/sablon/html/node_properties.rb, line 24
def self.run(properties)
  new('w:rPr', properties, Run::PROPERTIES)
end
table(properties) click to toggle source
# File lib/sablon/html/node_properties.rb, line 12
def self.table(properties)
  new('w:tblPr', properties, Table::PROPERTIES)
end
table_cell(properties) click to toggle source
# File lib/sablon/html/node_properties.rb, line 20
def self.table_cell(properties)
  new('w:tcPr', properties, TableCell::PROPERTIES)
end
table_row(properties) click to toggle source
# File lib/sablon/html/node_properties.rb, line 16
def self.table_row(properties)
  new('w:trPr', properties, TableRow::PROPERTIES)
end

Public Instance Methods

[](key) click to toggle source
# File lib/sablon/html/node_properties.rb, line 37
def [](key)
  @properties[key.to_sym]
end
[]=(key, value) click to toggle source
# File lib/sablon/html/node_properties.rb, line 41
def []=(key, value)
  @properties[key.to_sym] = value
end
inspect() click to toggle source
# File lib/sablon/html/node_properties.rb, line 33
def inspect
  @properties.map { |k, v| v ? "#{k}=#{v}" : k }.join(';')
end
to_docx() click to toggle source
# File lib/sablon/html/node_properties.rb, line 45
def to_docx
  "<#{@tagname}>#{properties_word_ml}</#{@tagname}>" unless @properties.empty?
end

Private Instance Methods

filter_properties(properties, whitelist) click to toggle source

processes properties adding those on the whitelist to the properties instance variable and those not to the transferred_properties isntance variable

# File lib/sablon/html/node_properties.rb, line 54
def filter_properties(properties, whitelist)
  @transferred_properties = {}
  @properties = {}
  #
  properties.each do |key, value|
    if whitelist.include? key.to_s
      @properties[key.to_sym] = value
    else
      @transferred_properties[key] = value
    end
  end
end
properties_word_ml() click to toggle source

processes attributes defined on the node into wordML property syntax

# File lib/sablon/html/node_properties.rb, line 68
def properties_word_ml
  @properties.map { |k, v| transform_attr(k, v) }.join
end
transform_attr(key, value) click to toggle source

properties that have a list as the value get nested in tags and each entry in the list is transformed. When a value is a hash the keys in the hash are used to explicitly build the XML tag attributes.

# File lib/sablon/html/node_properties.rb, line 75
def transform_attr(key, value)
  if value.is_a? Array
    sub_attrs = value.map do |sub_prop|
      sub_prop.map { |k, v| transform_attr(k, v) }
    end
    "<w:#{key}>#{sub_attrs.join}</w:#{key}>"
  elsif value.is_a? Hash
    props = value.map { |k, v| format('w:%s="%s"', k, v) if v }
    "<w:#{key} #{props.compact.join(' ')} />"
  else
    value = format('w:val="%s" ', value) if value
    "<w:#{key} #{value}/>"
  end
end