summaryrefslogtreecommitdiffstats
path: root/lib/rdoc/markup/preprocess.rb
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-01-14 03:34:05 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-01-14 03:34:05 +0000
commit49ecbb73cc188b7eadac6cd1a459b3649a3262dc (patch)
tree347720b09a9bb0f08d442141b51ed074ce43e6b9 /lib/rdoc/markup/preprocess.rb
parent933da53cb15c52b34a4b4b3def7282f4b5fc9631 (diff)
downloadruby-49ecbb73cc188b7eadac6cd1a459b3649a3262dc.tar.gz
ruby-49ecbb73cc188b7eadac6cd1a459b3649a3262dc.tar.xz
ruby-49ecbb73cc188b7eadac6cd1a459b3649a3262dc.zip
Renamespace lib/rdoc/markup from SM::SimpleMarkup to RDoc::Markup.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@15033 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rdoc/markup/preprocess.rb')
-rw-r--r--lib/rdoc/markup/preprocess.rb71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/rdoc/markup/preprocess.rb b/lib/rdoc/markup/preprocess.rb
new file mode 100644
index 000000000..760351d38
--- /dev/null
+++ b/lib/rdoc/markup/preprocess.rb
@@ -0,0 +1,71 @@
+require 'rdoc/markup'
+
+##
+# Handle common directives that can occur in a block of text:
+#
+# : include : filename
+
+class RDoc::Markup::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
+ prefix = $1
+ directive = $2.downcase
+ param = $3
+
+ case directive
+ when "include"
+ filename = param.split[0]
+ include_file(filename, prefix)
+
+ 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) then
+ content = File.open(full_name) {|f| f.read}
+ # strip leading '#'s, but only if all lines start with them
+ if content =~ /^[^#]/
+ content.gsub(/^/, indent)
+ else
+ content.gsub(/^#?/, indent)
+ end
+ 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
+