class Bones::AnnotationExtractor

A helper class used to find and display any annotations in a collection of project files.

Constants

Annotation

Attributes

config[R]
id[R]
tag[R]

Public Class Methods

enumerate( config, tag, id = nil, opts = {} ) click to toggle source

Enumerate all the annoations for the given config and tag. This will search for all athe annotations and display them on standard output.

# File lib/bones/annotation_extractor.rb, line 23
def self.enumerate( config, tag, id = nil, opts = {} )
  extractor = new(config, tag, id)
  extractor.display(extractor.find, opts)
end
new( config, tag, id) click to toggle source

Creates a new annotation extractor configured to use the config open strcut and to search for the given tag (which can be more than one tag via a regular expression 'or' operation – i.e. THIS|THAT|OTHER)

# File lib/bones/annotation_extractor.rb, line 34
def initialize( config, tag, id) 
  @config = config
  @tag = tag
  @id = @id_rgxp = nil

  unless id.nil? or id.empty?
    @id = id
    @id_rgxp = Regexp.new(Regexp.escape(id), Regexp::IGNORECASE)
  end
end

Public Instance Methods

display( results, opts = {} ) click to toggle source

Print the results of the annotation extraction to the screen. If the :tags option is set to true, then the annotation tag will be displayed.

# File lib/bones/annotation_extractor.rb, line 89
def display( results, opts = {} )
  results.keys.sort.each do |file|
    puts "#{file}:"
    results[file].each do |note|
      puts "  * #{note.to_s(opts)}"
    end
    puts
  end
end
extract_annotations_from( file, pattern ) click to toggle source

Extract any annotations from the given file using the regular expression pattern provided.

# File lib/bones/annotation_extractor.rb, line 68
def extract_annotations_from( file, pattern )
  lineno = 0
  result = File.readlines(file).inject([]) do |list, line|
    lineno += 1
    next list unless m = pattern.match(line)
    next list << Annotation.new(lineno, m[1], m[2]) unless id

    text = m[2]
    if text =~ @id_rgxp
      text.gsub!(@id_rgxp) {|str| Bones::Colors.colorize(str, :green)}
      list << Annotation.new(lineno, m[1], text)
    end
    list
  end
  result.empty? ? {} : { file => result }
end
find() click to toggle source

Iterate over all the files in the project and extract annotations from the those files. Returns the results as a hash for display.

# File lib/bones/annotation_extractor.rb, line 48
def find
  results = {}
  rgxp = /(#{tag}):?\s*(.*?)(?:\s*(?:-?%>|\*+\/))?$/o

  extensions = config.notes.extensions.dup
  exclude = if config.notes.exclude.empty? then nil
            else Regexp.new(config.notes.exclude.join('|')) end

  config.gem.files.each do |fn|
    next if exclude && exclude =~ fn
    next unless extensions.include? File.extname(fn)
    results.update(extract_annotations_from(fn, rgxp))
  end

  results
end