Re-enable spec/library for full CI runs.
[rbx.git] / lib / rdoc / markup / preprocess.rb
blob00dd4be4ad141bbe614a1c6dea77aeeab64e764a
1 require 'rdoc/markup'
3 ##
4 # Handle common directives that can occur in a block of text:
6 # : include : filename
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
13   end
15   ##
16   # Look for common options in a chunk of text. Options that we don't handle
17   # are yielded to the caller.
19   def handle(text)
20     text.gsub!(/^([ \t]*#?[ \t]*):(\w+):([ \t]*)(.+)?\n/) do
21       next $& if $3.empty? and $4 and $4[0, 1] == ':'
23       prefix    = $1
24       directive = $2.downcase
25       param     = $4
27       case directive
28       when 'include' then
29         filename = param.split[0]
30         include_file filename, prefix
32       else
33         result = yield directive, param
34         result = "#{prefix}:#{directive}: #{param}\n" unless result
35         result
36       end
37     end
38   end
40   private
42   ##
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
49       if content =~ /^[^#]/
50         content.gsub(/^/, indent)
51       else
52         content.gsub(/^#?/, indent)
53       end
54     else
55       $stderr.puts "Couldn't find file to include: '#{name}'"
56       ''
57     end
58   end
60   ##
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?
70     end
71     nil
72   end
74 end