2008-01-13 22:34:05 -05:00
|
|
|
require 'rdoc/markup'
|
|
|
|
|
|
|
|
##
|
|
|
|
# Handle common directives that can occur in a block of text:
|
|
|
|
#
|
|
|
|
# : include : filename
|
2010-04-26 23:45:22 -04:00
|
|
|
#
|
|
|
|
# RDoc plugin authors can register additional directives to be handled through
|
|
|
|
# RDoc::Markup::PreProcess::register
|
2008-01-13 22:34:05 -05:00
|
|
|
|
|
|
|
class RDoc::Markup::PreProcess
|
|
|
|
|
2010-04-26 23:45:22 -04:00
|
|
|
@registered = {}
|
|
|
|
|
|
|
|
##
|
|
|
|
# Registers +directive+ as one handled by RDoc. If a block is given the
|
|
|
|
# directive will be replaced by the result of the block, otherwise the
|
|
|
|
# directive will be removed from the processed text.
|
|
|
|
|
|
|
|
def self.register directive, &block
|
|
|
|
@registered[directive] = block
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Registered directives
|
|
|
|
|
|
|
|
def self.registered
|
|
|
|
@registered
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Creates a new pre-processor for +input_file_name+ that will look for
|
|
|
|
# included files in +include_path+
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
def initialize(input_file_name, include_path)
|
|
|
|
@input_file_name = input_file_name
|
|
|
|
@include_path = include_path
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-04-26 23:45:22 -04:00
|
|
|
# Look for directives in a chunk of +text+.
|
|
|
|
#
|
|
|
|
# Options that we don't handle are yielded. If the block returns false the
|
|
|
|
# directive is restored to the text. If the block returns nil or no block
|
|
|
|
# was given the directive is handled according to the registered directives.
|
|
|
|
# If a String was returned the directive is replaced with the string.
|
|
|
|
#
|
|
|
|
# If no matching directive was registered the directive is restored to the
|
|
|
|
# text.
|
|
|
|
#
|
|
|
|
# If +code_object+ is given and the param is set as metadata on the
|
|
|
|
# +code_object+. See RDoc::CodeObject#metadata
|
|
|
|
|
|
|
|
def handle text, code_object = nil
|
2008-07-17 20:46:16 -04:00
|
|
|
text.gsub!(/^([ \t]*#?[ \t]*):(\w+):([ \t]*)(.+)?\n/) do
|
|
|
|
next $& if $3.empty? and $4 and $4[0, 1] == ':'
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
prefix = $1
|
|
|
|
directive = $2.downcase
|
2008-07-17 20:46:16 -04:00
|
|
|
param = $4
|
2008-01-13 22:34:05 -05:00
|
|
|
|
|
|
|
case directive
|
2008-07-17 20:46:16 -04:00
|
|
|
when 'include' then
|
2008-01-13 22:34:05 -05:00
|
|
|
filename = param.split[0]
|
2008-07-17 20:46:16 -04:00
|
|
|
include_file filename, prefix
|
2008-01-13 22:34:05 -05:00
|
|
|
|
|
|
|
else
|
2010-04-26 23:45:22 -04:00
|
|
|
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
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
result
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
end
|
2010-04-26 23:45:22 -04:00
|
|
|
|
|
|
|
text
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Include a file, indenting it correctly.
|
|
|
|
|
|
|
|
def include_file(name, indent)
|
|
|
|
if full_name = find_include_file(name) then
|
2010-04-26 23:45:22 -04:00
|
|
|
content = if defined?(Encoding) then
|
|
|
|
File.binread full_name
|
|
|
|
else
|
|
|
|
File.read full_name
|
|
|
|
end
|
2010-04-22 22:32:20 -04:00
|
|
|
# HACK determine content type and force encoding
|
2010-04-10 02:36:13 -04:00
|
|
|
content = content.sub(/\A# .*coding[=:].*$/, '').lstrip
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
# strip leading '#'s, but only if all lines start with them
|
2010-04-01 03:45:16 -04:00
|
|
|
if content =~ /^[^#]/ then
|
2008-01-13 22:34:05 -05:00
|
|
|
content.gsub(/^/, indent)
|
|
|
|
else
|
|
|
|
content.gsub(/^#?/, indent)
|
|
|
|
end
|
|
|
|
else
|
2010-04-10 02:36:13 -04:00
|
|
|
warn "Couldn't find file to include '#{name}' from #{@input_file_name}"
|
2008-01-13 22:34:05 -05:00
|
|
|
''
|
|
|
|
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)
|
2010-04-10 02:36:13 -04:00
|
|
|
to_search = [File.dirname(@input_file_name)].concat @include_path
|
2008-01-13 22:34:05 -05:00
|
|
|
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
|
|
|
|
|