How to include a Ruby code file, as is, in RDoc?
I have an example.rb file that documents how to use my gem, and I would like to include it in one of the files like README.rdoc and HISTORY.rdoc.
I already figured out how to convert ruby source code to HTML using the syntax stone , but I can't figure out how make RDoc includes a file without parsing it.
When I tell RDoc to include an html file, it is not specified, and if I fake it using rdoc or txt as the file extension, it does not display properly (the file is still actually html).
I have a solution that works just incredibly ugly. There should be a better way to do this, which is native to rdoc, but I do not see it.
Here is what I have in my Rakefile:
require 'rake/rdoctask'
require 'syntax/convertors/html'
rdoc_dir = 'rdoc'
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
replacement_key = "REPLACE_THIS_TEXT_WITH_PROPER_HTML"
Dir.glob('examples/*.rb').each do |file|
File.open("#{file}.txt", "w") do |dummy_file|
dummy_file.write(replacement_key)
end
end
Rake::RDocTask.new(:rdoc2) do |rdoc|
rdoc.rdoc_dir = rdoc_dir
rdoc.title = "pickled_optparse #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('HISTORY*')
rdoc.rdoc_files.include('examples/*.txt')
rdoc.rdoc_files.include('lib/**/*.rb')
end
task :rdoc3 do
html_header = File.read('rake_reqs/html_header.html')
Dir.glob('examples/*.rb').each do |file|
html_ruby = convertor.convert(File.read(file))
rdoc_file = "#{rdoc_dir}/examples/#{File.basename(file,".rb")}_rb_txt.html"
fixed_html = File.read(rdoc_file).gsub!(replacement_key, "#{html_header}#{html_ruby}")
File.open(rdoc_file, "w") {|f| f.write(fixed_html)}
File.delete("#{file}.txt")
end
end
task :rdoc => [:rdoc2, :rdoc3]
Mike bethany
source
share