Class: XRay::Runner

Inherits:
Object
  • Object
show all
Includes:
Helper::FileReader
Defined in:
lib/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::FileReader

#readfile

Constructor Details

#initialize(opt = {}) ⇒ Runner

Returns a new instance of Runner.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/runner.rb', line 50

def initialize(opt={})
  @opt = {
    :encoding => 'utf-8',
    :debug    => false,
    :log_level => :warn
  }.merge opt

  @rules = []

  if @opt[:debug]
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::INFO
  end

  Rule.import_all


end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



48
49
50
# File 'lib/runner.rb', line 48

def rules
  @rules
end

#sourceObject (readonly)

Returns the value of attribute source.



48
49
50
# File 'lib/runner.rb', line 48

def source
  @source
end

Instance Method Details

#check(text, filename = "") ⇒ Object



179
180
181
182
# File 'lib/runner.rb', line 179

def check(text, filename="")
  type = CodeType.guess(text, filename)
  send "check_#{type}", text
end

#check_css(css, opt = {}) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/runner.rb', line 69

def check_css(css, opt = {})
  @source              = css
  parser               = XRay::CSS::VisitableParser.new(css, @logger)
  visitor              = XRay::CSS::Rule::CheckListRule.new( opt )
  parser.add_visitor visitor
  run_parser( parser )
end

#check_css_file(file, opt = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/runner.rb', line 77

def check_css_file( file, opt={} )
  results = []
  begin
    "".extend(XRay::Rule).check_css_file(file).each do |msg, level, *pos|
      results.unshift LogEntry.new( msg, level, pos[0] || 0, pos[1] || 0 )
    end

    source = XRay::CSS::Reader.read( file, @opt )
    results.concat check_css( source, opt.merge({
      :scope => CodeType.scope(file)
    }))
  rescue EncodingError => e
    results.unshift LogEntry.new( "File can't be read as #{@opt[:encoding]} charset", :fatal)
  rescue => e
    if @opt[:debug]
      puts e
      puts e.backtrace 
    end
    results.unshift LogEntry.new( e.to_s, :fatal)
  end
  results
end

#check_file(file) ⇒ Object



174
175
176
177
# File 'lib/runner.rb', line 174

def check_file( file )
  type = CodeType.guess_by_name(file)
  send( "check_#{type}_file", file ) if CodeType.is_style_file? file
end

#check_html(text, opt = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/runner.rb', line 126

def check_html(text, opt={})
  @source = text
  parser = HTML::VisitableParser.new(text, @logger)
  visitor = HTML::Rule::CheckTagRule.new( opt )
  parser.add_visitor visitor
  results = run_parser( parser )
  unless @parsed_element.nil? or @parsed_element.empty?
    results += check_scripts_in_html(opt)
    results += check_styles_in_html(opt)
  end
  results
end

#check_html_file(file, opt = {}) ⇒ Object



169
170
171
172
# File 'lib/runner.rb', line 169

def check_html_file(file, opt={})
  source, encoding = readfile file
  check_html source
end

#check_js(js, opt = {}) ⇒ Object



100
101
102
103
104
105
# File 'lib/runner.rb', line 100

def check_js(js, opt={})
  @source = js
  parser = JS::VisitableParser.new(js, @logger)
  parser.add_visitors JS::Rule::All.new
  run_parser( parser )
end

#check_js_file(file, opt = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/runner.rb', line 107

def check_js_file(file, opt = {})
  results = []
  begin
    results.concat JS::Rule::FileChecker.new.check_file(file)

    source, encoding = readfile(file, opt)
    results.concat check_js(source)
  rescue  EncodingError => e
    results.unshift LogEntry.new( "File can't be read as #{@opt[:encoding]} charset", :fatal)
  rescue => e
    if @opt[:debug]
      puts e
      puts e.backtrace 
    end
    results.unshift LogEntry.new( e.to_s, :fatal )
  end
  results
end

#check_scripts_in_html(opt = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/runner.rb', line 139

def check_scripts_in_html(opt={})
  results = []
  @parsed_element.query('script') do |script|
    row = script.position.row
    col = script.position.column
    Runner.new.check_js(script.text, opt).each do |ret|
      ret.column += script.outer_html[/^\s*<script*?>/].size if ret.row == 1
      ret.row += row - 1
      results << ret
    end
  end

  results
end

#check_styles_in_html(opt = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/runner.rb', line 154

def check_styles_in_html(opt={})
  results = []
  @parsed_element.query('style') do |style|
    row = style.position.row
    col = style.position.column
    Runner.new.check_css(style.text, opt.merge({:scope => :in_page })).each do |ret|
      ret.column += style.outer_html[/^\s*<style*?>/].size if ret.row == 1
      ret.row += row - 1
      results << ret
    end
  end

  results
end

#filter_results(results) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/runner.rb', line 205

def filter_results(results)
  case log_level
    when :warn
      results
    when :error
      results.select {|r| r.level == :error || r.level == :fatal }
    when :fatal
      results.select {|r| r.level == :fatal }
  end
end

#log_levelObject



216
217
218
# File 'lib/runner.rb', line 216

def log_level
  @opt[:log_level]
end

#log_level=(lvl) ⇒ Object



220
221
222
# File 'lib/runner.rb', line 220

def log_level=(lvl)
  @opt[:log_level] = lvl
end

#minified_and_ignored(file) ⇒ Object



188
189
190
# File 'lib/runner.rb', line 188

def minified_and_ignored(file)
  !@opt[:check_min] && file.to_s =~ /-min\.(css|js)$/
end

#run_parser(parser) ⇒ Object



200
201
202
203
# File 'lib/runner.rb', line 200

def run_parser(parser)
  @parsed_element = parser.parse_no_throw
  filter_results( parser.results ).sort_by!(&:row)
end

#type_ok?(file) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
198
# File 'lib/runner.rb', line 192

def type_ok?(file)
  if @opt[:type]
    CodeType.guess_by_name(file) == @opt[:type]
  else
    true
  end
end

#valid_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/runner.rb', line 184

def valid_file? file
  CodeType.is_style_file?(file) and !minified_and_ignored(file) and type_ok?(file)
end