diff options
author | dave <dave@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-12-01 07:12:49 +0000 |
---|---|---|
committer | dave <dave@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-12-01 07:12:49 +0000 |
commit | 87762adcb0d38d6c575448f67c2906964215f3a1 (patch) | |
tree | c74f6e5b2b51f641cdc7895a37eb4c161b2c6d72 /lib/rdoc/markup/simple_markup/preprocess.rb | |
parent | c1c55573bdaecbd972f57b46dd22dfbd0e1a02dd (diff) |
Add RDoc
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5073 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rdoc/markup/simple_markup/preprocess.rb')
-rw-r--r-- | lib/rdoc/markup/simple_markup/preprocess.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/rdoc/markup/simple_markup/preprocess.rb b/lib/rdoc/markup/simple_markup/preprocess.rb new file mode 100644 index 0000000000..09892c2b6c --- /dev/null +++ b/lib/rdoc/markup/simple_markup/preprocess.rb @@ -0,0 +1,68 @@ +module SM + + ## + # Handle common directives that can occur in a block of text: + # + # : include : filename + # + + class PreProcess + + def initialize(input_file_name, include_path) + @input_file_name = input_file_name + @include_path = include_path + end + + # Look for common options in a chunk of text. Options that + # we don't handle are passed back to our caller + # as |directive, param| + + def handle(text) + text.gsub!(/^([ \t#]*):(\w+):\s*(.+)?\n/) do + + directive = $2.downcase + param = $3 + + case directive + + when "include" + include_file($3, $1) + + else + yield(directive, param) + end + end + end + + ####### + private + ####### + + # Include a file, indenting it correctly + + def include_file(name, indent) + if (full_name = find_include_file(name)) + content = File.open(full_name) {|f| f.read} + res = content.gsub(/^#?/, indent) + else + $stderr.puts "Couldn't find file to include: '#{name}'" + '' + end + end + + # Look for the given file in the directory containing the current + # file, and then in each of the directories specified in the + # RDOC_INCLUDE path + + def find_include_file(name) + to_search = [ File.dirname(@input_file_name) ].concat @include_path + to_search.each do |dir| + full_name = File.join(dir, name) + stat = File.stat(full_name) rescue next + return full_name if stat.readable? + end + nil + end + + end +end |