Class: Rsssf::Parser::Linter

Inherits:
Object
  • Object
show all
Defined in:
lib/rsssf/parser/linter.rb

Overview

note - Linter for now nested inside Parser - keep? why? why not?

Constant Summary collapse

MAX_ERRORS =

stop after 13 errors

13

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLinter

Returns a new instance of Linter.



18
19
20
21
# File 'lib/rsssf/parser/linter.rb', line 18

def initialize
  @errors = []
  @parser = Parser.new   ## use own parser instance (not shared) - why? why not?
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/rsssf/parser/linter.rb', line 16

def errors
  @errors
end

Class Method Details

.debug=(value) ⇒ Object



9
# File 'lib/rsssf/parser/linter.rb', line 9

def self.debug=(value) @@debug = value; end

.debug?Boolean

note: default is FALSE

Returns:

  • (Boolean)


10
# File 'lib/rsssf/parser/linter.rb', line 10

def self.debug?() @@debug ||= false; end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


11
# File 'lib/rsssf/parser/linter.rb', line 11

def debug?()  self.class.debug?; end

#errors?Boolean

Returns:

  • (Boolean)


24
# File 'lib/rsssf/parser/linter.rb', line 24

def errors?() @errors.size > 0; end

#parse(txt, parse: false, path: 'path/to/filename/here') ⇒ Object



42
43
44
45
46
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
135
136
137
138
139
140
141
142
143
# File 'lib/rsssf/parser/linter.rb', line 42

def parse( txt, parse: false, 
                path: 'path/to/filename/here' )
  ## note: every (new) read call - resets errors list to empty
  @errors = []

  nodes = SportDb::OutlineReader.parse( txt ) 

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

  nodes.each do |node|
    type = node[0]
    
    if type == :h1
        h1 = node[1]  ## get heading text
        ## puts
        puts "  = Heading 1 >#{node[1]}<"
    elsif type == :p

      if h1.nil?
        orphans += 1    ## only warn once (at the end; see below)
        next
      end

      paragraphs += 1

      lines = node[1]

      tree = [] 
      lines.each_with_index do |line,i|
     
        if debug?
          puts
          puts "line >#{line}<"
        end

        t, error_messages  =  if parse
                                @parser.parse_with_errors( line )
                              else
                                @parser.tokenize_with_errors( line )                   
                              end
         

    if error_messages.size > 0
      ## add to "global" error list
      ##   make a triplet tuple (file / msg / line text)
            error_messages.each do |msg|

                ## note - stop processing / adding errors if hit MAX ERRORS 
                if @errors.size >= MAX_ERRORS
                   @errors << [ path,
                                 "stop after #{MAX_ERRORS} errors", 
                                 '']
                   return
                end

                @errors << [ path,
                             msg,
                             line
                           ]
            end
    end

    pp t   if debug?

    tree << t
  end    
  ## pp tree
else
  pp node
  raise ArgumentError, "unsupported (node) type >#{type}<"
end
end  # each node

  ## no heading and no orphans => assume empty file (comments only)!!!
  if h1.nil? && orphans == 0
    puts "  !! WARN - no heading(s) and paragraph(s) found"
     @errors << [ path,
                  "warn - no heading(s) and paragraph(s) found",
                  ""  ## pass along empty line
                ]
  end

  if orphans > 0
    puts "  !! WARN - no heading for #{orphans} text paragraph(s); skipping parse"
    @errors << [ path,
                  "warn - no heading for #{orphans} text paragraph(s); skipping parse",
                  ""  ## pass along empty line
               ]
  end

  if h1 && paragraphs == 0
    puts "  !! WARN - heading with no text paragraph(s)"
    @errors << [ path,
                  "warn - heading with no text paragraph(s)",
                  ""  ## pass along empty line
               ]
  end

end

#read(path, parse: false) ⇒ Object



28
29
30
31
# File 'lib/rsssf/parser/linter.rb', line 28

def read( path, parse: false )
     parse( read_text( path ), parse: parse,
                               path:  path )
end