Class: Metrocot::MatchRange

Inherits:
Object
  • Object
show all
Defined in:
lib/metrocot.rb

Overview

represents a subtree withing a metrocot dom. the semantics are roughly equivalent to what you get from select-dragging your mouse pointer through a section of an html doc in your web browser. Thats is, a range specifies the first and last node in the pre-fix traversal of the dom. Additionally, the first and last node may be truncated (at their tail and head respectively) if they are text nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_scraper, start_index, start_offset, end_index, end_offset) ⇒ MatchRange

Returns a new instance of MatchRange.



81
82
83
84
85
86
87
88
# File 'lib/metrocot.rb', line 81

def initialize( node_scraper, start_index, start_offset, end_index, end_offset )
	@node_scraper = node_scraper
	@start_index  = start_index
	@start_offset = start_offset
	@end_index    = end_index
	@end_offset   = end_offset
	@verbose      = false
end

Instance Attribute Details

#end_indexObject

Returns the value of attribute end_index.



78
79
80
# File 'lib/metrocot.rb', line 78

def end_index
  @end_index
end

#end_offsetObject

Returns the value of attribute end_offset.



78
79
80
# File 'lib/metrocot.rb', line 78

def end_offset
  @end_offset
end

#node_scraperObject

Returns the value of attribute node_scraper.



78
79
80
# File 'lib/metrocot.rb', line 78

def node_scraper
  @node_scraper
end

#start_indexObject

Returns the value of attribute start_index.



78
79
80
# File 'lib/metrocot.rb', line 78

def start_index
  @start_index
end

#start_offsetObject

Returns the value of attribute start_offset.



78
79
80
# File 'lib/metrocot.rb', line 78

def start_offset
  @start_offset
end

#verboseObject

Returns the value of attribute verbose.



78
79
80
# File 'lib/metrocot.rb', line 78

def verbose
  @verbose
end

Instance Method Details

#crop(crop_start_index, crop_start_offset, crop_end_index, crop_end_offset) ⇒ Object



96
97
98
# File 'lib/metrocot.rb', line 96

def crop( crop_start_index, crop_start_offset, crop_end_index, crop_end_offset )
	MatchRange.new( node_scraper, crop_start_index, crop_start_offset, crop_end_index, crop_end_offset )
end

#describeObject



153
154
155
# File 'lib/metrocot.rb', line 153

def describe
	"[#{start_index}+#{start_offset} ... #{end_index}+#{end_offset}]"
end

#empty?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/metrocot.rb', line 101

def empty?
	return @start_index >= @end_index || (@start_index == @end_index && @start_offset >= end_offset)
end

#extend(other) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/metrocot.rb', line 133

def extend( other ) 

	extended_range = MatchRange.new( @node_scraper, @start_index, @start_offset, @end_index, @end_offset )
	if other.start_index < extended_range.start_index
		extended_range.start_index = other.start_index 
	elsif other.start_index == extended_range.start_index && other.start_offset < extended_range.start_offset
		extended_range.start_offset = other.start_offset 
	end

	if other.end_index > extended_range.end_index
		extended_range.end_index = other.end_index 
	elsif other.end_index == extended_range.end_index && other.end_offset > extended_range.end_offset
		extended_range.end_offset = other.end_offset 
	end

	extended_range

end

#following(other_range) ⇒ Object



106
107
108
# File 'lib/metrocot.rb', line 106

def following( other_range )
	MatchRange.new( node_scraper, other_range.end_index, other_range.end_offset, end_index, end_offset )
end

#head(head_end_index, head_end_offset) ⇒ Object



116
117
118
# File 'lib/metrocot.rb', line 116

def head( head_end_index, head_end_offset )
	MatchRange.new( @node_scraper, @start_index, @start_offset, head_end_index, head_end_offset )
end

#hnodesObject



91
92
93
# File 'lib/metrocot.rb', line 91

def hnodes
	@node_scraper.flattened_hnodes
end

#split_at(middle) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/metrocot.rb', line 121

def split_at( middle )
	parts = []
	if middle.start_index > 0 || middle.start_offset > 0
		parts << crop_at( 0, 0, middle.start_index, middle.start_offset )
	end
	parts << middle
	if middle.end_index < end_index || (middle.start_end_index == end_index && middle.end_offset < end_offset)
		parts << crop( middle.end_index, middle.end_offset, end_index, end_offset )
	end
end

#tail(tail_start_index, tail_start_offset) ⇒ Object



111
112
113
# File 'lib/metrocot.rb', line 111

def tail( tail_start_index, tail_start_offset )
	MatchRange.new( @node_scraper, tail_start_index, tail_start_offset, @end_index, @end_offset )
end