2007-01-18 22:24:27 +00:00
|
|
|
require 'erb'
|
|
|
|
|
|
|
|
class String
|
|
|
|
def lines
|
|
|
|
split $/
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_whitespace_at_line_ends
|
|
|
|
lines.map {|line| line.gsub(/\s+$/, '')} * $/
|
|
|
|
end
|
2008-12-14 04:36:59 +00:00
|
|
|
|
|
|
|
def strip_pdoc_comments
|
|
|
|
gsub %r{\s*/\*\*.*?\*\*/}m, "\n"
|
|
|
|
end
|
2007-01-18 22:24:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module Protodoc
|
|
|
|
module Environment
|
|
|
|
def include(*filenames)
|
2008-12-14 04:36:59 +00:00
|
|
|
filenames.map do |filename|
|
|
|
|
Preprocessor.new(expand_path(filename), @options).result
|
|
|
|
end.join("\n")
|
2007-01-18 22:24:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Preprocessor
|
|
|
|
include Environment
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
def initialize(filename, options = { })
|
2008-12-11 10:43:15 +00:00
|
|
|
filename = File.join(filename.split('/'))
|
2007-01-18 22:24:27 +00:00
|
|
|
@filename = File.expand_path(filename)
|
|
|
|
@template = ERB.new(IO.read(@filename), nil, '%')
|
2008-12-14 04:36:59 +00:00
|
|
|
@options = options
|
2007-01-18 22:24:27 +00:00
|
|
|
end
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
def expand_path(filename)
|
|
|
|
File.join(File.dirname(@filename), filename)
|
2007-01-18 22:24:27 +00:00
|
|
|
end
|
2008-12-14 04:36:59 +00:00
|
|
|
|
|
|
|
def result
|
|
|
|
result = @template.result(binding)
|
|
|
|
result = result.strip_whitespace_at_line_ends
|
|
|
|
result = result.strip_pdoc_comments if @options[:strip_documentation]
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :to_s, :result
|
2007-01-18 22:24:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
print Protodoc::Preprocessor.new(ARGV.first)
|
|
|
|
end
|