module RDoc::Text

def expand_tabs text

def expand_tabs text
  expanded = []
  text.each_line do |line|
    line.gsub!(/^(.{8}*?)([^\t\r\n]{0,7})\t/) do
      "#{$1}#{$2}#{' ' * (8 - $2.size)}"
    end until line !~ /\t/
    expanded << line
  end
  expanded.join
end

def flush_left text

def flush_left text
  indents = []
  text.each_line do |line|
    indents << (line =~ /[^\s]/ || 9999)
  end
  indent = indents.min
  flush = []
  text.each_line do |line|
    line[/^ {0,#{indent}}/] = ''
    flush << line
  end
  flush.join
end

def markup text

def markup text
  document = parse text
  document.accept formatter
end

def normalize_comment text

def normalize_comment text
  return text if text.empty?
  text = strip_hashes text
  text = expand_tabs text
  text = flush_left text
  strip_newlines text
end

def parse text

def parse text
  return text if RDoc::Markup::Document === text
  text = normalize_comment text
  return RDoc::Markup::Document.new if text =~ /\A\n*\z/
  RDoc::Markup::Parser.parse text
rescue RDoc::Markup::Parser::Error => e
  $stderr.puts <<-EOF
ile parsing markup, RDoc encountered a #{e.class}:
e}
from #{e.backtrace.join "\n\tfrom "}
-8<---
text}
-8<---
oc #{RDoc::VERSION}
by #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_RELEASE_DATE}
ease file a bug report with the above information at:
tp://rubyforge.org/tracker/?atid=2472&group_id=627&func=browse
  EOF
  raise
end

def strip_hashes text

def strip_hashes text
  return text if text =~ /^(?>\s*)[^\#]/
  text.gsub(/^\s*(#+)/) { $1.tr '#',' ' }
end

def strip_newlines text

def strip_newlines text
  text.gsub(/\A\n*(.*?)\n*\z/m, '\1')
end

def strip_stars text

def strip_stars text
  text = text.gsub %r%Document-method:\s+[\w:.#]+%, ''
  text.sub!  %r%/\*+%       do " " * $&.length end
  text.sub!  %r%\*+/%       do " " * $&.length end
  text.gsub! %r%^[ \t]*\*%m do " " * $&.length end
  text
end