class RDoc::Markup::PreProcess
def self.register directive, &block
def self.register directive, &block @registered[directive] = block end
def self.registered
def self.registered @registered end
def find_include_file(name)
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
def handle text, code_object = nil
def handle text, code_object = nil text.gsub!(/^([ \t]*#?[ \t]*):(\w+):([ \t]*)(.+)?\n/) do next $& if $3.empty? and $4 and $4[0, 1] == ':' prefix = $1 directive = $2.downcase param = $4 case directive when 'include' then filename = param.split[0] include_file filename, prefix else result = yield directive, param if block_given? case result when nil then code_object.metadata[directive] = param if code_object if RDoc::Markup::PreProcess.registered.include? directive then handler = RDoc::Markup::PreProcess.registered[directive] result = handler.call directive, param if handler else result = "#{prefix}:#{directive}: #{param}\n" end when false then result = "#{prefix}:#{directive}: #{param}\n" end result end end text end
def include_file(name, indent)
def include_file(name, indent) if full_name = find_include_file(name) then content = if defined?(Encoding) then File.binread full_name else File.read full_name end # HACK determine content type and force encoding content = content.sub(/\A# .*coding[=:].*$/, '').lstrip # strip leading '#'s, but only if all lines start with them if content =~ /^[^#]/ then content.gsub(/^/, indent) else content.gsub(/^#?/, indent) end else warn "Couldn't find file to include '#{name}' from #{@input_file_name}" '' end end
def initialize(input_file_name, include_path)
def initialize(input_file_name, include_path) @input_file_name = input_file_name @include_path = include_path end