Class: Bones::AnnotationExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/annotation_extractor.rb

Overview

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

Defined Under Namespace

Classes: Annotation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, tag, id) ⇒ AnnotationExtractor

Creates a new annotation extractor configured to use the project 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)



43
44
45
46
47
48
49
50
51
52
# File 'lib/bones/annotation_extractor.rb', line 43

def initialize( project, tag, id) 
  @project = project
  @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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



37
38
39
# File 'lib/bones/annotation_extractor.rb', line 37

def id
  @id
end

#projectObject (readonly)

Returns the value of attribute project.



37
38
39
# File 'lib/bones/annotation_extractor.rb', line 37

def project
  @project
end

#tagObject (readonly)

Returns the value of attribute tag.



37
38
39
# File 'lib/bones/annotation_extractor.rb', line 37

def tag
  @tag
end

Class Method Details

.enumerate(project, tag, id = nil, opts = {}) ⇒ Object

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



32
33
34
35
# File 'lib/bones/annotation_extractor.rb', line 32

def self.enumerate( project, tag, id = nil, opts = {} )
  extractor = new(project, tag, id)
  extractor.display(extractor.find, opts)
end

Instance Method Details

#display(results, opts = {}) ⇒ Object

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.



98
99
100
101
102
103
104
105
106
# File 'lib/bones/annotation_extractor.rb', line 98

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) ⇒ Object

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bones/annotation_extractor.rb', line 77

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| ANSICode.green(str)} if HAVE_COLOR
      list << Annotation.new(lineno, m[1], text)
    end
    list
  end
  result.empty? ? {} : { file => result }
end

#findObject

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bones/annotation_extractor.rb', line 57

def find
  results = {}
  rgxp = %r/(#{tag}):?\s*(.*?)(?:\s*(?:-?%>|\*+\/))?$/o

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

  project.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