diff --git a/lib/rdoc/rdoc.rb b/lib/rdoc/rdoc.rb
index a7f9239b62..04a00cd347 100644
--- a/lib/rdoc/rdoc.rb
+++ b/lib/rdoc/rdoc.rb
@@ -245,15 +245,67 @@ def output_flag_file(op_dir)
# The .document file contains a list of file and directory name patterns,
# representing candidates for documentation. It may also contain comments
# (starting with '#')
+ #
+ # If the first line is the comment starts with +rdoc.document:+
+ # (case-insensitive) followed by a version string, the file is
+ # parsed as per the version. If a version is not written, it is
+ # defaulted to 0.
+ #
+ # version 0::
+ #
+ # The file will be parsed as white-space separated glob patterns.
+ #
+ # - A # in middle starts a comment.
+ #
+ # - Multiple patterns can be in a single line.
+ #
+ # - That means patterns cannot contain white-spaces and #
+ # marks.
+ #
+ # version 1::
+ #
+ # The file will be parsed as single glob pattern per each line.
+ #
+ # - Only lines starting with # at the first colmun are
+ # comments. A # in middle is a part of the pattern.
+ #
+ # - Patterns starting with # need to be prefixed with a
+ # backslash (\\).
+ #
+ # - Leading spaces are not stripped while trailing spaces which
+ # are not escaped with a backslash are stripped.
+ #
+ # - The pattern starting with ! is a negative pattern,
+ # which rejects matching files.
def parse_dot_doc_file in_dir, filename
- # read and strip comments
- patterns = File.read(filename).gsub(/#.*/, '')
-
result = {}
+ patterns = rejects = nil
+
+ content = File.read(filename)
+ version = content[/\A#+\s*rdoc\.document:\s*\K\S+/i]&.to_i || 0
+ if version >= 1
+ content.each_line(chomp: true) do |line|
+ next if line.start_with?("#") # skip comments
+ line.sub!(/(?