class Sablon::Processor::Document::Block

Attributes

end_field[RW]
start_field[RW]

Public Class Methods

enclosed_by(start_field, end_field) click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 7
def self.enclosed_by(start_field, end_field)
  @blocks ||= [ImageBlock, RowBlock, ParagraphBlock, InlineParagraphBlock]
  block_class = @blocks.detect { |klass| klass.encloses?(start_field, end_field) }
  block_class.new start_field, end_field
end
encloses?(start_field, end_field) click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 13
def self.encloses?(start_field, end_field)
  parent(start_field) && parent(end_field)
end
new(start_field, end_field) click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 25
def initialize(start_field, end_field)
  @start_field = start_field
  @end_field = end_field

  # update reference counts for control fields
  @start_field.block_reference_count += 1
  @end_field.block_reference_count += 1
end
parent(node) click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 17
def self.parent(node)
  node.ancestors(parent_selector).first
end
parent_selector() click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 21
def self.parent_selector
  './/w:p'
end

Public Instance Methods

body() click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 54
def body
  return @body if defined?(@body)
  @body = []
  node = start_node
  while (node = node.next_element) && node != end_node
    @body << node
  end
  @body
end
end_node() click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 68
def end_node
  @end_node ||= self.class.parent(end_field)
end
process(env) click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 34
def process(env)
  replaced_node = Nokogiri::XML::Node.new("tmp", start_node.document)
  replaced_node.children = Nokogiri::XML::NodeSet.new(start_node.document, body.map(&:dup))
  Processor::Document.process replaced_node, env
  replaced_node.children
end
remove_control_elements() click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 46
def remove_control_elements
  body.each(&:remove)
  # we only want to remove the start and end nodes if they belong
  # to a single block.
  start_field.remove_parent(self.class.parent_selector)
  end_field.remove_parent(self.class.parent_selector)
end
replace(content) click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 41
def replace(content)
  content.each { |n| start_node.add_next_sibling n }
  remove_control_elements
end
start_node() click to toggle source
# File lib/sablon/processor/document/blocks.rb, line 64
def start_node
  @start_node ||= self.class.parent(start_field)
end