Class: ListParser

Inherits:
Object
  • Object
show all
Includes:
ListParserUtils
Defined in:
lib/ribit/contentparser.rb

Instance Method Summary collapse

Methods included from ListParserUtils

#build_regex

Instance Method Details

#parse(text, contentDoc) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/ribit/contentparser.rb', line 424

def parse( text, contentDoc )
  @newContainers = Array.new
  
  # this is stack of currently active lists (list hierarchy)
  @activeLists = Array.new
  @buffer = ''
  @endConditionFound = false
  @currentContainer = nil
  # regex changes depending of a level of the list    
  @currentListRegex = build_regex( 1 )
  
  # NOTE: that lines contain line feeds
  text.each_line do |line|
    isListLine = Regexp.new( @currentListRegex ).match( line ) != nil
    isEmptyLine = ( line.strip.size() == 0 )
    
    listStarted = @activeLists.size() > 0
    isSiblingListLine = (listStarted and Regexp.new( @activeLists.last.get_sibling_regex() ).match( line ) != nil)
    
    #      puts "line = '#{line}'"
    #      puts "siblingregex=" + @activeLists.last.get_sibling_regex() if isSiblingListLine
    #      puts "empty=" + isEmptyLine.to_s \
    #        + ",listline=" + isListLine.to_s \
    #        + ",end=" + @endConditionFound.to_s \
    #        + ",listStart=" + listStarted.to_s \
    #        + ",sibling=" + isSiblingListLine.to_s
    
    # case: no active list but list item found => create a list 
    if ( isListLine == true and listStarted == false )
      create_list_container( 1 )       
      start_new_item()
      
      # case: new item found to the current list  
    elsif ( isListLine == true and listStarted == true )
      # new item found => save old data
      create_list_item( @buffer )
      start_new_item()
      
      # case: sibling line found
    elsif( isSiblingListLine == true and listStarted == true )
      # first store previous item 
      lastItem = create_list_item( @buffer )
      start_new_item()
      
      # then create new list container
      create_list_container( @activeLists.last.level + 1 )
      # ... that is child of last list item (inside <li></li>)
      lastItem.add_child( @currentContainer )     
      
      # case: previous line was empty => now empty or text line terminates the current list          
    elsif ( @endConditionFound == true and isListLine == false and listStarted == true )
      # first store last item
      create_list_item( @buffer )
      start_new_item()
      
      # remove the current list from the stack
      @activeLists.pop
      
      # end condition is common for all
      # => drop first last
      list = find_matching_parent_list( line )
      
      if ( list != nil )
        @currentContainer = list.container
        @currentListRegex = list.get_regex
        # hack!
        isListLine = true  
      else      
        # did we found any list container, if not then create new text
        @currentContainer = TextContainer.new
        @newContainers.push( @currentContainer )         
        @currentListRegex = build_regex( 1 )
      end
      
    elsif ( listStarted == true and isEmptyLine == true )
      @endConditionFound = true
      
    elsif ( listStarted == true )
      # just go forward adding data to current item
      @endConditionFound = false
      
      # check is data line upper level list item
      if ( @activeLists.size() > 1 )
        # yes, there are upper level items
        
        # go through all upper level lists
        endIndex = @activeLists.size() - 2
        for index in (0..endIndex)
          list = @activeLists[index]
          if ( Regexp.new( list.get_regex ).match( line ) != nil )
            # yes, the current line matches to the upper level item
            #  => lower level list ends
            create_list_item( @buffer )
            start_new_item()
            
            # drop all lists that are closed now
            @activeLists = @activeLists[0,index + 1]
            @currentContainer = list.container
            @currentListRegex = list.get_regex
            
            # hack! => to handle list item properly
            isListLine = true
            
            break
          end
        end # end of for loop
      end  
      
    end
    
    # situation might been changed  
    listStarted = @activeLists.size() > 0
    
    if ( listStarted and (isListLine or isSiblingListLine ) )
      add_list_start_line_to_buffer( line )
      
    elsif ( listStarted )
      add_normal_line_to_buffer( line )
      
    else
      if ( @currentContainer == nil )
        @currentContainer = TextContainer.new
        @newContainers.push( @currentContainer )
      end 
      add_to_text_container( line )
    end
    
  end
  
  # if list line was last item
  if ( @buffer.size() > 0 )
    create_list_item( @buffer )
  end
  
  return @newContainers
end