module RJack::TarPit::ReadmeParser

Helper mixin for deriving default spec properties from the README (rdoc,txt) file

Public Instance Methods

parse_readme( file ) click to toggle source

Parse the given README file, setting properties homepage, summary, and description on self if possible.

# File lib/rjack-tarpit/readme_parser.rb, line 27
def parse_readme( file )

  in_desc = false
  desc = String.new

  readme_file_open( file ) do |fin|
    fin.each do |line|
      if homepage.nil? && line =~ /^\s*\*\s*(http\S+)\s*$/
        self.homepage = $1
      elsif line =~ /^=/ # section header
        in_desc = ( line =~ /^=+\s*Description\s*$/i )
        # Stop at new section if we already have a description
        break unless desc.empty?
      elsif in_desc
        # Stop if empty line after description, or bullet (*) list
        break if ( !desc.empty? && line =~ /^\s*$/ ) || line =~ /^\s*\*/
        desc << line
      end
    end
  end

  desc = desc.
    gsub( /\s+/, ' ' ). #Simplify whitespace
    gsub( /\{([^\}]+)\}\[[^\]]*\]/, '\1' ). # Replace rdoc link \w anchor
    gsub( /(\S)\[\s*http:[^\]]*\]/, '\1' ). # Replace bare rdoc links
    strip.
    sub( /:$/, '.' ) # Replace a final ':', like when term. at list

  # Summary is first sentence if we find one, or entire desc otherwise
  if desc =~ /^(.+[!?:.])\s/
    self.summary = $1.strip
    self.description = desc # Use remainder for description
  else
    self.summary = desc
  end

end