Class: Metrocot::OptSpacePattern

Inherits:
BasePattern show all
Defined in:
lib/metrocot.rb

Overview

matches zero or more white spaces

Instance Attribute Summary

Attributes inherited from BasePattern

#matched, #metrocot, #name, #node_scraper, #pattern_no, #pred, #source, #succ

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasePattern

#default_scanner, #dump, #dump_match_map, #log, #log_match_data, #with_scanned_match_data

Constructor Details

#initializeOptSpacePattern

Returns a new instance of OptSpacePattern.



359
360
361
# File 'lib/metrocot.rb', line 359

def initialize
	super(" ")
end

Class Method Details

.parse(s) ⇒ Object



371
372
373
374
# File 'lib/metrocot.rb', line 371

def self.parse( s ) 
	return nil unless s[0..0] == " "
	OptSpacePattern.new
end

Instance Method Details

#descriptionObject



363
364
365
# File 'lib/metrocot.rb', line 363

def description
	"spaces"
end

#each_match(match_range, match_map) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/metrocot.rb', line 380

def each_match( match_range, match_map )

	result = nil

	super(match_range, match_map)

	match_start_index  = match_range.start_index
	match_start_offset = match_range.start_offset
	match_end_index    = match_range.start_index
	match_end_offset   = match_range.start_offset

	raise "negative range #{match_range}" if match_start_index > match_end_index
	raise "negative range #{match_range}" if match_start_index == match_end_index && match_start_offset > match_end_offset

	# consume rest of first text node  

	hnodes = match_range.hnodes
	hnode_text = nil

	if hnodes[match_start_index] && hnodes[match_start_index].text?
	
		hnode_text = hnodes[match_start_index].inner_text
		while match_end_offset < hnode_text.size && (/^\s+$/.=== hnode_text[match_start_offset .. match_end_offset])
			match_end_offset += 1
		end
			
		if match_end_offset >= match_start_offset 
			if match_end_offset >= hnode_text.size
				log( "matched entire string of #{match_end_offset - match_start_offset} spaces" )
			else
				log( "matched first #{match_end_offset - match_start_offset} leading spaces" )
			end
		end
	end

	sp_match_range = match_range.crop( match_start_index, match_start_offset, match_end_index, match_end_offset )

	result = with_scanned_match_data( match_map, hnodes[match_start_index ... match_end_index] ) { |match_map|
		yield( sp_match_range, match_map ) 
	}
	
	result
end

#optionalObject



367
368
369
# File 'lib/metrocot.rb', line 367

def optional
	true
end

#priorityObject



376
377
378
# File 'lib/metrocot.rb', line 376

def priority
	-8
end