Skip to content

Commit 27d71fb

Browse files
committed
New line-oriented .document file format
Introduce the new format similar to the `.gitignore` file by starting with the comment `rdoc.document: 1`. - one pattern per line - negative pattern starting with `!` - escaping space and `#` by a backslash
1 parent 7af1e30 commit 27d71fb

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

lib/rdoc/rdoc.rb

+57-5
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,67 @@ def output_flag_file(op_dir)
234234
# The .document file contains a list of file and directory name patterns,
235235
# representing candidates for documentation. It may also contain comments
236236
# (starting with '#')
237+
#
238+
# If the first line is the comment starts with +rdoc.document:+
239+
# (case-insensitive) followed by a version string, the file is
240+
# parsed as per the version. If a version is not written, it is
241+
# defaulted to 0.
242+
#
243+
# version 0::
244+
#
245+
# The file will be parsed as white-space separated glob patterns.
246+
#
247+
# - A <tt>#</tt> in middle starts a comment.
248+
#
249+
# - Multiple patterns can be in a single line.
250+
#
251+
# - That means patterns cannot contain white-spaces and <tt>#</tt>
252+
# marks.
253+
#
254+
# version 1::
255+
#
256+
# The file will be parsed as single glob pattern per each line.
257+
#
258+
# - Only lines starting with <tt>#</tt> at the first colmun are
259+
# comments. A <tt>#</tt> in middle is a part of the pattern.
260+
#
261+
# - Patterns starting with <tt>#</tt> need to be prefixed with a
262+
# backslash (<tt>\\</tt>).
263+
#
264+
# - Leading spaces are not stripped while trailing spaces which
265+
# are not escaped with a backslash are stripped.
266+
#
267+
# - The pattern starting with <tt>!</tt> is a negative pattern,
268+
# which rejects matching files.
237269

238270
def parse_dot_doc_file in_dir, filename
239-
# read and strip comments
240-
patterns = File.read(filename).gsub(/#.*/, '')
241-
242271
result = {}
272+
patterns = rejects = nil
273+
274+
content = File.read(filename)
275+
version = content[/\A#+\s*rdoc\.document:\s*\K\S+/i]&.to_i || 0
276+
if version >= 1
277+
content.each_line(chomp: true) do |line|
278+
next if line.start_with?("#") # skip comments
279+
line.sub!(/(?<!\\)\s*$/, "") # rstrip unescaped trailing spaces
280+
(line.sub!(/\A!/, "") ? (rejects ||= []) : (patterns ||= [])) << line
281+
end
282+
else
283+
# read and strip comments
284+
patterns = content.gsub(/#.*/, '').split(' ')
285+
end
243286

244-
patterns.split(' ').each do |patt|
245-
candidates = Dir.glob(File.join(in_dir, patt))
287+
if patterns
288+
patterns.each {|patt| patt.sub!(/\A\/+/, "")}
289+
candidates = Dir.glob(patterns, base: in_dir)
290+
if rejects
291+
rejects.each {|patt| patt.sub!(/\A\/+/, "")}
292+
flag = File::FNM_PATHNAME
293+
candidates.delete_if do |name|
294+
rejects.any? {|patt| File.fnmatch?(patt, name, flag)}
295+
end
296+
end
297+
candidates.map! {|name| File.join(in_dir, name)}
246298
result.update normalized_file_list(candidates, false, @options.exclude)
247299
end
248300

test/rdoc/test_rdoc_rdoc.rb

+43
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,49 @@ def test_normalized_file_list_with_dot_doc
182182
assert_equal expected_files, files
183183
end
184184

185+
def test_normalized_file_list_with_dot_doc_version_1
186+
expected_files = []
187+
files = temp_dir do |dir|
188+
a = 'a.rb'
189+
b = 'b.rb'
190+
a_b = 'a.rb b.rb'
191+
FileUtils.touch a
192+
FileUtils.touch b
193+
FileUtils.touch a_b
194+
195+
File.open('.document', 'w') do |f|
196+
f.puts '# rdoc.document: 1'
197+
f.puts a_b
198+
end
199+
expected_files << File.expand_path(a_b, dir)
200+
201+
@rdoc.normalized_file_list [dir]
202+
end
203+
204+
assert_equal expected_files, files.keys
205+
end
206+
207+
def test_normalized_file_list_with_dot_doc_negative_pattern
208+
expected_files = []
209+
files = temp_dir do |dir|
210+
a = 'a.rb'
211+
b = 'b.rb'
212+
FileUtils.touch a
213+
FileUtils.touch b
214+
215+
File.open('.document', 'w') do |f|
216+
f.puts '# rdoc.document: 1'
217+
f.puts '*.rb'
218+
f.puts '!b.rb'
219+
end
220+
expected_files << File.expand_path(a, dir)
221+
222+
@rdoc.normalized_file_list [dir]
223+
end
224+
225+
assert_equal expected_files, files.keys
226+
end
227+
185228
def test_normalized_file_list_with_dot_doc_overridden_by_exclude_option
186229
expected_files = []
187230
files = temp_dir do |dir|

0 commit comments

Comments
 (0)