Class: SportDb::Outline

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/quick/outline.rb

Overview

add a simple Outline convenience class

         for processing OUtlines with OUtlineReader
rename to simply Outline - why? why not?
  todo - add more processing options - why? why not?

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ Outline

Returns a new instance of Outline.



22
23
24
# File 'lib/sportdb/quick/outline.rb', line 22

def initialize( nodes )
   @nodes = nodes
end

Class Method Details

.parse(txt) ⇒ Object



16
17
18
19
# File 'lib/sportdb/quick/outline.rb', line 16

def self.parse( txt )
    nodes = OutlineReader.parse( txt )
    new( nodes )
end

.read(path) ⇒ Object



11
12
13
14
# File 'lib/sportdb/quick/outline.rb', line 11

def self.read( path )
    nodes = OutlineReader.read( path ) 
    new( nodes )
end

Instance Method Details

#each_para(&blk) ⇒ Object Also known as: each_paragraph



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sportdb/quick/outline.rb', line 26

def each_para( &blk )
  ## note: every (new) read call - resets errors list to empty
  ### @errors = []

  ##  process nodes
  h1 = nil
  h2 = nil
  orphans = 0    ## track paragraphs's with no heading

  @nodes.each do |node|
     type = node[0]

     if type == :h1
        h1 = node[1]  ## get heading text
        puts "  = Heading 1 >#{node[1]}<"
     elsif type == :h2
        if h1.nil?
          puts "!! WARN - no heading for subheading; skipping processing"
          next
        end
        h2 = node[1]  ## get heading text
        puts "  == Heading 2 >#{node[1]}<"
     elsif type == :p
        if h1.nil?
          orphans += 1    ## only warn once
          puts "!! WARN - no heading for #{orphans} text paragraph(s); skipping parse"
          next
        end

        lines = node[1]
        blk.call( lines )
     else
       pp node
       raise ArgumentError, "unsupported (node) type >#{type}<"
     end
  end  # each node
end

#each_para_text(&blk) ⇒ Object

or use each_text or ? - why? why not=



78
79
80
81
82
83
84
85
# File 'lib/sportdb/quick/outline.rb', line 78

def each_para_text( &blk )   ## or use each_text or ? - why? why not=
    each_para do |lines|
        txt = lines.reduce( String.new ) do |mem,line| 
                                  mem << line; mem << "\n"; mem 
                                 end
        blk.call( txt )
    end
end