Class: Metrocot::PathPattern

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

Overview

matches a certain Hpricot path

Instance Attribute Summary

Attributes inherited from BasePattern

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasePattern

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

Constructor Details

#initialize(source, path) ⇒ PathPattern

Returns a new instance of PathPattern.



292
293
294
295
# File 'lib/metrocot.rb', line 292

def initialize( source, path )
  super(source)
  @path = path
end

Class Method Details

.parse(s) ⇒ Object



297
298
299
300
301
302
303
# File 'lib/metrocot.rb', line 297

def self.parse( s ) 
  return nil unless s.index(".") == 0
  return nil if s.index("..") == 0
  space_index = s.index(" ") || s.size
  return nil if space_index == 1
  self.new( s[0 .. space_index - 1], s[1 .. space_index - 1] )
end

Instance Method Details

#descriptionObject



311
312
313
# File 'lib/metrocot.rb', line 311

def description
  "path \"#{@path}\""
end

#each_match(match_range, match_map) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/metrocot.rb', line 316

def each_match( match_range, match_map )
  super(match_range, match_map)
  result = nil
  search_root = match_range.node_scraper.hnode
  search_root.search( @path ).each { |descendent|
    nix = node_scraper.hnode_index[descendent]
    next_node_nix = node_scraper.hnode_succ_index[descendent]
    unless nix
      @node_scraper.flattened_hnodes.each { |node|
        log( "#{@node_scraper.hnode_index[node]}: #{node}" )
      }
      raise "no node index for #{descendent.class} #{descendent}" 
    end
    if nix < match_range.start_index 
      log( "too far left: #{nix}" )
      next 
    end
    if nix >= match_range.end_index 
      log( "too far right: #{nix}" )
      break 
    end
    log( "matched path at node #{nix}" )
    result = with_scanned_match_data( match_map, descendent ) { |match_map|
      yield( match_range.crop(nix, 0, next_node_nix, 0) , match_map )
    }
    break if result
  }
  result
end

#priorityObject



347
348
349
# File 'lib/metrocot.rb', line 347

def priority
  1
end

#sourceObject



306
307
308
# File 'lib/metrocot.rb', line 306

def source
  @source
end