Class: Sablon::Processor::Document::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/sablon/processor/document/blocks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_field, end_field) ⇒ Block

Returns a new instance of Block.



25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#end_fieldObject

Returns the value of attribute end_field.



5
6
7
# File 'lib/sablon/processor/document/blocks.rb', line 5

def end_field
  @end_field
end

#start_fieldObject

Returns the value of attribute start_field.



5
6
7
# File 'lib/sablon/processor/document/blocks.rb', line 5

def start_field
  @start_field
end

Class Method Details

.enclosed_by(start_field, end_field) ⇒ Object



7
8
9
10
11
# 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) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/sablon/processor/document/blocks.rb', line 13

def self.encloses?(start_field, end_field)
  parent(start_field) && parent(end_field)
end

.parent(node) ⇒ Object



17
18
19
# File 'lib/sablon/processor/document/blocks.rb', line 17

def self.parent(node)
  node.ancestors(parent_selector).first
end

.parent_selectorObject



21
22
23
# File 'lib/sablon/processor/document/blocks.rb', line 21

def self.parent_selector
  './/w:p'
end

Instance Method Details

#bodyObject



54
55
56
57
58
59
60
61
62
# 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_nodeObject



68
69
70
# File 'lib/sablon/processor/document/blocks.rb', line 68

def end_node
  @end_node ||= self.class.parent(end_field)
end

#process(env) ⇒ Object



34
35
36
37
38
39
# 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_elementsObject



46
47
48
49
50
51
52
# 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) ⇒ Object



41
42
43
44
# 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_nodeObject



64
65
66
# File 'lib/sablon/processor/document/blocks.rb', line 64

def start_node
  @start_node ||= self.class.parent(start_field)
end