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

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

Direct Known Subclasses

InlineParagraphBlock, ParagraphBlock, RowBlock

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enclosed_by(start_field, end_field) ⇒ Object



53
54
55
56
57
# File 'lib/sablon/processor/document.rb', line 53

def self.enclosed_by(start_field, end_field)
  @blocks ||= [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)


95
96
97
# File 'lib/sablon/processor/document.rb', line 95

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

Instance Method Details

#bodyObject



77
78
79
80
81
82
83
84
85
# File 'lib/sablon/processor/document.rb', line 77

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



91
92
93
# File 'lib/sablon/processor/document.rb', line 91

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

#process(context) ⇒ Object



59
60
61
62
63
64
# File 'lib/sablon/processor/document.rb', line 59

def process(context)
  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, context
  replaced_node.children
end

#remove_control_elementsObject



71
72
73
74
75
# File 'lib/sablon/processor/document.rb', line 71

def remove_control_elements
  body.each &:remove
  start_node.remove
  end_node.remove
end

#replace(content) ⇒ Object



66
67
68
69
# File 'lib/sablon/processor/document.rb', line 66

def replace(content)
  content.each { |n| start_node.add_next_sibling n }
  remove_control_elements
end

#start_nodeObject



87
88
89
# File 'lib/sablon/processor/document.rb', line 87

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