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 passed back to our caller as |directive, param|
20 text.gsub!(/^([ \t#]*):(\w+):\s*(.+)?\n/) do
22 directive = $2.downcase
27 filename = param.split[0]
28 include_file(filename, prefix)
31 yield(directive, param)
39 # Include a file, indenting it correctly.
41 def include_file(name, indent)
42 if full_name = find_include_file(name) then
43 content = File.open(full_name) {|f| f.read}
44 # strip leading '#'s, but only if all lines start with them
46 content.gsub(/^/, indent)
48 content.gsub(/^#?/, indent)
51 $stderr.puts "Couldn't find file to include: '#{name}'"
57 # Look for the given file in the directory containing the current file,
58 # and then in each of the directories specified in the RDOC_INCLUDE path
60 def find_include_file(name)
61 to_search = [ File.dirname(@input_file_name) ].concat @include_path
62 to_search.each do |dir|
63 full_name = File.join(dir, name)
64 stat = File.stat(full_name) rescue next
65 return full_name if stat.readable?