1##
2# Parse a non-source file. We basically take the whole thing as one big
3# comment.
4
5class RDoc::Parser::Simple < RDoc::Parser
6
7  include RDoc::Parser::Text
8
9  parse_files_matching(//)
10
11  attr_reader :content # :nodoc:
12
13  ##
14  # Prepare to parse a plain file
15
16  def initialize(top_level, file_name, content, options, stats)
17    super
18
19    preprocess = RDoc::Markup::PreProcess.new @file_name, @options.rdoc_include
20
21    preprocess.handle @content, @top_level
22  end
23
24  ##
25  # Extract the file contents and attach them to the TopLevel as a comment
26
27  def scan
28    comment = remove_coding_comment @content
29    comment = remove_private_comment comment
30
31    comment = RDoc::Comment.new comment, @top_level
32
33    @top_level.comment = comment
34    @top_level
35  end
36
37  ##
38  # Removes the encoding magic comment from +text+
39
40  def remove_coding_comment text
41    text.sub(/\A# .*coding[=:].*$/, '')
42  end
43
44  ##
45  # Removes private comments.
46  #
47  # Unlike RDoc::Comment#remove_private this implementation only looks for two
48  # dashes at the beginning of the line.  Three or more dashes are considered
49  # to be a rule and ignored.
50
51  def remove_private_comment comment
52    # Workaround for gsub encoding for Ruby 1.9.2 and earlier
53    empty = ''
54    empty.force_encoding comment.encoding if Object.const_defined? :Encoding
55
56    comment = comment.gsub(%r%^--\n.*?^\+\+\n?%m, empty)
57    comment.sub(%r%^--\n.*%m, empty)
58  end
59
60end
61
62