class Rabbit::READMEParser

Attributes

description[R]
logger[RW]
title[R]

Public Class Methods

new(logger=nil) click to toggle source
# File lib/rabbit/readme-parser.rb, line 27
def initialize(logger=nil)
  @logger = logger || Logger.default
  @title = nil
  @description = nil
end

Public Instance Methods

parse(path=nil) click to toggle source
# File lib/rabbit/readme-parser.rb, line 33
def parse(path=nil)
  path ||= remove_backup_paths(Dir.glob("README*"))[0]
  raise _("No README found") if path.nil?

  parse_content(File.read(path), File.extname(path))
end

Private Instance Methods

guess_type(content, extension) click to toggle source
# File lib/rabbit/readme-parser.rb, line 57
def guess_type(content, extension)
  guess_type_from_extension(extension) ||
    guess_type_from_content(content) ||
    :rd
end
guess_type_from_content(content) click to toggle source
# File lib/rabbit/readme-parser.rb, line 78
def guess_type_from_content(content)
  case content
  when /^==/
    :rd
  when /^!!/
    :hiki
  when /^\#\#/
    :markdown
  when /^h\d\./
    :textile
  else
    nil
  end
end
guess_type_from_extension(extension) click to toggle source
# File lib/rabbit/readme-parser.rb, line 63
def guess_type_from_extension(extension)
  case extension.downcase
  when ".rd", ".rab"
    :rd
  when ".hiki"
    :hiki
  when ".md"
    :markdown
  when ".textile"
    :textile
  else
    nil
  end
end
heading_mark_pattern(type) click to toggle source
# File lib/rabbit/readme-parser.rb, line 93
def heading_mark_pattern(type)
  case type
  when :rd
    /\A=+\s*/
  when :hiki
    /\A!+\s*/
  when :markdown
    /\A\#+\s*/
  when :textile
    /\Ah\d\.\s*/
  else
    heading_mark_pattern(:rd)
  end
end
parse_content(content, extension) click to toggle source
# File lib/rabbit/readme-parser.rb, line 41
def parse_content(content, extension)
  type = guess_type(content, extension)
  _heading_mark_pattern = heading_mark_pattern(type)

  blocks = content.split(/(?:\r?\n){2,}/)
  if blocks[0]
    @title = blocks[0].gsub(_heading_mark_pattern, "")
  end
  first_paragraph_blocks = []
  blocks[1..-1].each do |block|
    break if _heading_mark_pattern =~ block
    first_paragraph_blocks << block
  end
  @description = first_paragraph_blocks.join("\n\n")
end
remove_backup_paths(paths) click to toggle source
# File lib/rabbit/readme-parser.rb, line 108
def remove_backup_paths(paths)
  paths.reject do |path|
    path.end_with?("~")
  end
end