Chris@909: module CodeRay Chris@909: module Encoders Chris@909: Chris@909: # Counts the LoC (Lines of Code). Returns an Integer >= 0. Chris@909: # Chris@909: # Alias: +loc+ Chris@909: # Chris@909: # Everything that is not comment, markup, doctype/shebang, or an empty line, Chris@909: # is considered to be code. Chris@909: # Chris@909: # For example, Chris@909: # * HTML files not containing JavaScript have 0 LoC Chris@909: # * in a Java class without comments, LoC is the number of non-empty lines Chris@909: # Chris@909: # A Scanner class should define the token kinds that are not code in the Chris@909: # KINDS_NOT_LOC constant, which defaults to [:comment, :doctype]. Chris@909: class LinesOfCode < TokenKindFilter Chris@909: Chris@909: register_for :lines_of_code Chris@909: Chris@909: NON_EMPTY_LINE = /^\s*\S.*$/ Chris@909: Chris@909: protected Chris@909: Chris@909: def setup options Chris@909: if scanner Chris@909: kinds_not_loc = scanner.class::KINDS_NOT_LOC Chris@909: else Chris@909: warn "Tokens have no associated scanner, counting all nonempty lines." if $VERBOSE Chris@909: kinds_not_loc = CodeRay::Scanners::Scanner::KINDS_NOT_LOC Chris@909: end Chris@909: Chris@909: options[:exclude] = kinds_not_loc Chris@909: Chris@909: super options Chris@909: end Chris@909: Chris@909: def finish options Chris@909: output @tokens.text.scan(NON_EMPTY_LINE).size Chris@909: end Chris@909: Chris@909: end Chris@909: Chris@909: end Chris@909: end