class DNote::Notes

Developer Notes

This class goes through you source files and compiles a list of any labeled comments. Labels are all-cap single word prefixes to a comment ending in a colon.

Special labels do not require the colon. By default these are TODO, FIXME, OPTIMIZE, THINK and DEPRECATE.

Constants

DEFAULT_LABELS

Default note labels to look for in source code. (NOT CURRENTLY USED!)

DEFAULT_PATHS

Default paths (all ruby scripts).

Attributes

colon[R]

Require label colon? Default is true.

context[R]

Number of lines of context to show.

files[R]

Files to search for notes.

labels[R]

Labels to document. Defaults are: TODO, FIXME, OPTIMIZE and DEPRECATE.

marker[R]

Specific remark marker (nil for auto).

url[R]

Link template.

Public Class Methods

new(files, options = {}) click to toggle source

New set of notes for give files and optional special labels.

# File lib/dnote/notes.rb, line 47
def initialize(files, options = {})
  @files = [files].flatten
  @labels = [options[:labels] || DEFAULT_LABELS].flatten.compact
  @colon = options[:colon].nil? ? true : options[:colon]
  @marker = options[:marker]
  @url = options[:url]
  @context = options[:context] || 0
  @remark = {}

  parse
end

Public Instance Methods

guess_marker(file) click to toggle source

Guess marker based on file extension. Fallsback to ‘#’ if the extension is unknown.

TODO: Continue to add comment types.

# File lib/dnote/notes.rb, line 176
def guess_marker(file)
  return @marker if @marker # forced marker

  case File.extname(file)
  when ".js", ".c", "cpp", ".css"
    "//"
  when ".bas"
    "'"
  when ".sql", ".ada"
    "--"
  when ".asm"
    ";"
  else
    "#"
  end
end
match(line, lineno, file) click to toggle source

Is this line a note?

# File lib/dnote/notes.rb, line 112
def match(line, lineno, file)
  if labels.empty?
    match_general(line, lineno, file)
  else
    match_special(line, lineno, file)
  end
end
match_general(line, lineno, file) click to toggle source

Match notes that are labeled with a colon.

# File lib/dnote/notes.rb, line 143
def match_general(line, lineno, file)
  rec = nil
  if (md = match_general_regex(file).match(line))
    label = md[1]
    text = md[2]
    rec = Note.new(self, file, label, lineno, text, remark(file))
  end
  rec
end
match_general_regex(file) click to toggle source

Keep in mind that general non-colon matches have a higher potential of false positives.

# File lib/dnote/notes.rb, line 155
def match_general_regex(file)
  mark = remark(file)
  if colon
    /#{mark}\s*([A-Z]+):\s+(.*?)$/
  else
    /#{mark}\s*([A-Z]+)\s+(.*?)$/
  end
end
match_special(line, lineno, file) click to toggle source

Match special notes.

# File lib/dnote/notes.rb, line 121
def match_special(line, lineno, file)
  rec = nil
  labels.each do |label|
    if (md = match_special_regex(label, file).match(line))
      text = md[1]
      rec = Note.new(self, file, label, lineno, text, remark(file))
    end
  end
  rec
end
match_special_regex(label, file) click to toggle source
# File lib/dnote/notes.rb, line 132
def match_special_regex(label, file)
  mark = remark(file)
  label = Regexp.escape(label)
  if colon
    /#{mark}\s*#{label}:\s+(.*?)$/
  else
    /#{mark}\s*#{label}:?\s+(.*?)$/
  end
end
notes_collection() click to toggle source
# File lib/dnote/notes.rb, line 59
def notes_collection
  @notes_collection ||= NotesCollection.new(@notes)
end
parse() click to toggle source

Gather notes.

# File lib/dnote/notes.rb, line 67
def parse
  records = []
  files.each do |fname|
    records += parse_file(fname)
  end

  @notes = records.sort
end
parse_file(fname) click to toggle source
# File lib/dnote/notes.rb, line 76
def parse_file(fname)
  return [] unless File.file?(fname)

  records = []
  mark = remark(fname)
  lineno = 0
  note = nil
  text = nil
  capt = nil
  File.readlines(fname).each do |line|
    lineno += 1
    note = match(line, lineno, fname)
    if note
      text = note.text
      capt = note.capture
      records << note
    elsif text
      case line
      when /^\s*#{mark}+\s*$/, /^\s*#{mark}--/, /^\s*#{mark}\+\+/
        text.strip!
        text = nil
      when /^\s*#{mark}/
        text << "\n" unless text[-1, 1] == "\n"
        text << line.gsub(/^\s*#{mark}\s*/, "")
      else
        text.strip!
        text = nil
      end
    elsif !/^\s*#{mark}/.match?(line)
      capt << line if capt && capt.size < context
    end
  end
  records
end
remark(file) click to toggle source
# File lib/dnote/notes.rb, line 164
def remark(file)
  @remark[File.extname(file)] ||=
    begin
      mark = guess_marker(file)
      Regexp.escape(mark)
    end
end