Class: CatalogDb::CardReader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/catalogdb/reader.rb

Defined Under Namespace

Classes: Card

Constant Summary collapse

LINK_ENTRY_REGEX =

example:

/\[
    (?<title>[^\]]+)    # 1st capture group (title)
  \]
  \(
    (?<url>[^\)]+)      # 2nd capture group (url)
  \)
/x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ CardReader



29
30
31
# File 'lib/catalogdb/reader.rb', line 29

def initialize( text )
  @text = text
end

Class Method Details

.from_file(path) ⇒ Object



20
21
22
# File 'lib/catalogdb/reader.rb', line 20

def self.from_file( path )
  self.from_string( File.read_utf8( path ) )
end

.from_string(text) ⇒ Object



24
25
26
# File 'lib/catalogdb/reader.rb', line 24

def self.from_string( text )
  self.new( text )
end

.from_url(src) ⇒ Object

note: src assumed a string



15
16
17
18
# File 'lib/catalogdb/reader.rb', line 15

def self.from_url( src )   # note: src assumed a string
  worker = Fetcher::Worker.new
  self.from_string( worker.read_utf8!( src ) )
end

Instance Method Details

#readObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/catalogdb/reader.rb', line 47

def read

  cards = []
  stack  = []   ## header/heading stack;  note: last_stack is stack.size; starts w/ 0


  # note: cut out; remove all html comments e.g. <!-- -->
  #   supports multi-line comments too (e.g. uses /m - make dot match newlines)
  text = @text.gsub( /<!--.+?-->/m, '' )  ## todo/fix: track/log cut out comments!!!

  text.each_line do |line|

    logger.debug "line: >#{line}<"
    line = line.rstrip  ## remove (possible) trailing newline

    ## todo/fix: add to event reader too!!!  - Thanks/Meta
    break if line =~ /^## (More|Thanks|Meta)/   #  stop when hitting >## More< or Thanks or Meta section
    next  if line =~ /^\s*$/      #  skip blank lines

    m = nil
    if line =~ /^[ ]*(#+)[ ]+/    ## heading/headers - note: must escpape #
        s = StringScanner.new( line )
        s.skip( /[ ]*/ )  ## skip whitespaces
        markers = s.scan( /#+/)
        level   = markers.size
        s.skip( /[ ]*/ )  ## skip whitespaces
        title   = s.rest.rstrip
        
        logger.debug " heading level: #{level}, title: >#{title}<"

        level_diff = level - stack.size

        if level_diff > 0
          logger.debug "[CardReader]    up  +#{level_diff}"
          if level_diff > 1
            logger.error "fatal: level step must be one (+1) is +#{level_diff}"
            fail "[CardReader] level step must be one (+1) is +#{level_diff}"
          end
        elsif level_diff < 0
          logger.debug "[CardReader]    down #{level_diff}"
          level_diff.abs.times { stack.pop }
          stack.pop
        else
          ## same level
          stack.pop
        end
        stack.push( [level,title] )
        logger.debug "  stack: #{stack.inspect}"

    elsif line =~ /^([ ]*)-[ ]+/     ## list item

      ## check indent level
      ##  note: skip sub list items for now (assume 2 or more spaces)
      indent = $1.to_s
      if indent.length >= 2
        logger.debug "  *** skip link entry w/ indent #{indent.length}: >#{line}<"
      elsif( m=LINK_ENTRY_REGEX.match( line ) )
        logger.debug " link entry: #{line}"

        s = StringScanner.new( line )
        s.skip( /[ ]*-[ ]*/ )  ## skip leading list


        ## collect links e.g.
        ## [["Lotus HQ", "http://lotusrb.org"],
        ##  [":octocat:", "https://github.com/lotus"],
        ##  [":gem:", "https://rubygems.org/gems/lotusrb"],
        ##  [":book:", "http://rubydoc.info/gems/lotusrb"]]

        links = s.rest.scan( LINK_ENTRY_REGEX )
        pp links

        categories = stack.map {|it| it[1] }.join('')
        logger.debug "  categories: #{categories}"

        card = Card.new( links, categories )
        ## pp card
        cards << card
      else
        logger.debug "  *** skip list item line: #{line}"
      end
    else
      logger.debug "  *** skip line: #{line}"
    end
  end
  
  cards
end