Skip to content

Commit 083f0df

Browse files
nobuhsbt
authored andcommitted
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 9ed530b commit 083f0df

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
@@ -245,15 +245,67 @@ def output_flag_file(op_dir)
245245
# The .document file contains a list of file and directory name patterns,
246246
# representing candidates for documentation. It may also contain comments
247247
# (starting with '#')
248+
#
249+
# If the first line is the comment starts with +rdoc.document:+
250+
# (case-insensitive) followed by a version string, the file is
251+
# parsed as per the version. If a version is not written, it is
252+
# defaulted to 0.
253+
#
254+
# version 0::
255+
#
256+
# The file will be parsed as white-space separated glob patterns.
257+
#
258+
# - A <tt>#</tt> in middle starts a comment.
259+
#
260+
# - Multiple patterns can be in a single line.
261+
#
262+
# - That means patterns cannot contain white-spaces and <tt>#</tt>
263+
# marks.
264+
#
265+
# version 1::
266+
#
267+
# The file will be parsed as single glob pattern per each line.
268+
#
269+
# - Only lines starting with <tt>#</tt> at the first colmun are
270+
# comments. A <tt>#</tt> in middle is a part of the pattern.
271+
#
272+
# - Patterns starting with <tt>#</tt> need to be prefixed with a
273+
# backslash (<tt>\\</tt>).
274+
#
275+
# - Leading spaces are not stripped while trailing spaces which
276+
# are not escaped with a backslash are stripped.
277+
#
278+
# - The pattern starting with <tt>!</tt> is a negative pattern,
279+
# which rejects matching files.
248280

249281
def parse_dot_doc_file in_dir, filename
250-
# read and strip comments
251-
patterns = File.read(filename).gsub(/#.*/, '')
252-
253282
result = {}
283+
patterns = rejects = nil
284+
285+
content = File.read(filename)
286+
version = content[/\A#+\s*rdoc\.document:\s*\K\S+/i]&.to_i || 0
287+
if version >= 1
288+
content.each_line(chomp: true) do |line|
289+
next if line.start_with?("#") # skip comments
290+
line.sub!(/(?<!\\)\s*$/, "") # rstrip unescaped trailing spaces
291+
(line.sub!(/\A!/, "") ? (rejects ||= []) : (patterns ||= [])) << line
292+
end
293+
else
294+
# read and strip comments
295+
patterns = content.gsub(/#.*/, '').split(' ')
296+
end
254297

255-
patterns.split(' ').each do |patt|
256-
candidates = Dir.glob(File.join(in_dir, patt))
298+
if patterns
299+
patterns.each {|patt| patt.sub!(/\A\/+/, "")}
300+
candidates = Dir.glob(patterns, base: in_dir)
301+
if rejects
302+
rejects.each {|patt| patt.sub!(/\A\/+/, "")}
303+
flag = File::FNM_PATHNAME
304+
candidates.delete_if do |name|
305+
rejects.any? {|patt| File.fnmatch?(patt, name, flag)}
306+
end
307+
end
308+
candidates.map! {|name| File.join(in_dir, name)}
257309
result.update normalized_file_list(candidates, false, @options.exclude)
258310
end
259311

test/rdoc/test_rdoc_rdoc.rb

+43
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,49 @@ def test_normalized_file_list_with_dot_doc
177177
assert_equal expected_files, files
178178
end
179179

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

0 commit comments

Comments
 (0)