To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / a7 / a728943093f6831306c2b123cde489f05fdadba9.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (1.1 KB)

1
module CodeRay
2
module Encoders
3
  
4
  # Counts the LoC (Lines of Code). Returns an Integer >= 0.
5
  # 
6
  # Alias: +loc+
7
  # 
8
  # Everything that is not comment, markup, doctype/shebang, or an empty line,
9
  # is considered to be code.
10
  # 
11
  # For example,
12
  # * HTML files not containing JavaScript have 0 LoC
13
  # * in a Java class without comments, LoC is the number of non-empty lines
14
  # 
15
  # A Scanner class should define the token kinds that are not code in the
16
  # KINDS_NOT_LOC constant, which defaults to [:comment, :doctype].
17
  class LinesOfCode < TokenKindFilter
18
    
19
    register_for :lines_of_code
20
    
21
    NON_EMPTY_LINE = /^\s*\S.*$/
22
    
23
  protected
24
    
25
    def setup options
26
      if scanner
27
        kinds_not_loc = scanner.class::KINDS_NOT_LOC
28
      else
29
        warn "Tokens have no associated scanner, counting all nonempty lines." if $VERBOSE
30
        kinds_not_loc = CodeRay::Scanners::Scanner::KINDS_NOT_LOC
31
      end
32
      
33
      options[:exclude] = kinds_not_loc
34
      
35
      super options
36
    end
37
    
38
    def finish options
39
      output @tokens.text.scan(NON_EMPTY_LINE).size
40
    end
41
    
42
  end
43
  
44
end
45
end