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(config, tag, id) ⇒ AnnotationExtractor

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)



34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



28
29
30
# File 'lib/bones/annotation_extractor.rb', line 28

def config
  @config
end

#idObject (readonly)

Returns the value of attribute id.



28
29
30
# File 'lib/bones/annotation_extractor.rb', line 28

def id
  @id
end

#tagObject (readonly)

Returns the value of attribute tag.



28
29
30
# File 'lib/bones/annotation_extractor.rb', line 28

def tag
  @tag
end

Class Method Details

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

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



23
24
25
26
# 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

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.



89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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

#findObject

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bones/annotation_extractor.rb', line 48

def find
  results = {}
  rgxp = %r/(#{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