4 # Handle common directives that can occur in a block of text:
8 class RDoc::Markup::PreProcess
10 def initialize(input_file_name, include_path)
11 @input_file_name = input_file_name
12 @include_path = include_path
16 # Look for common options in a chunk of text. Options that we don't handle
17 # are yielded to the caller.
20 text.gsub!(/^([ \t]*#?[ \t]*):(\w+):([ \t]*)(.+)?\n/) do
21 next $& if $3.empty? and $4 and $4[0, 1] == ':'
24 directive = $2.downcase
29 filename = param.split[0]
30 include_file filename, prefix
33 result = yield directive, param
34 result = "#{prefix}:#{directive}: #{param}\n" unless result
43 # Include a file, indenting it correctly.
45 def include_file(name, indent)
46 if full_name = find_include_file(name) then
47 content = File.open(full_name) {|f| f.read}
48 # strip leading '#'s, but only if all lines start with them
50 content.gsub(/^/, indent)
52 content.gsub(/^#?/, indent)
55 $stderr.puts "Couldn't find file to include: '#{name}'"
61 # Look for the given file in the directory containing the current file,
62 # and then in each of the directories specified in the RDOC_INCLUDE path
64 def find_include_file(name)
65 to_search = [ File.dirname(@input_file_name) ].concat @include_path
66 to_search.each do |dir|
67 full_name = File.join(dir, name)
68 stat = File.stat(full_name) rescue next
69 return full_name if stat.readable?