ruby - 将 Ruby 源代码作为 HTML 添加到 RDoc

标签 ruby rake rdoc rakefile

如何将 ruby​​ 代码文件按原样包含到 RDoc 中?

我有一个 example.rb 文件,它记录了如何使用我的 gem,我想将它作为 README.rdoc 和 HISTORY.rdoc 等文件之一。

我已经知道如何使用 Syntax gem 将 ruby​​ 源代码转换为 HTML但我无法弄清楚如何让 RDoc 在不解析文件的情况下包含该文件。

当我告诉 RDoc 包含未列出的 html 文件时,如果我使用 rdoc 或 txt 作为文件扩展名来伪造它,它就无法正确显示(该文件实际上仍然是 html)。

我有一个可行的解决方案,它非常丑陋。必须有更好的方法来执行此操作,它是 rdoc 固有的,但我没有看到它。

这是我的 Rakefile 中的内容:

# Build rdocs
require 'rake/rdoctask'
require 'syntax/convertors/html'
rdoc_dir = 'rdoc'
# This is rdoc1 but it doesn't work unless you DON'T wrap it in a task
# Generate html files from example ruby files
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
replacement_key = "REPLACE_THIS_TEXT_WITH_PROPER_HTML"
# Create dummy files
Dir.glob('examples/*.rb').each do |file|
  File.open("#{file}.txt", "w") do |dummy_file|
    dummy_file.write(replacement_key)
  end
end

# Call the rdoc task
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
  # Now use a hammer to replace the dummy text with the
  # html we want to use in our ruby example code file.
  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]

最佳答案

抱歉,我不能给你一个实际的答案,但我正在查看 sdoc我。 您可以安装 gem来自 ruby 。

关于ruby - 将 Ruby 源代码作为 HTML 添加到 RDoc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4077047/

相关文章:

sql - ActiveRecord 查询,按关联排序,has_many 的最后一个

ruby - 安装 Thin (1.7.2) 时发生错误

ruby-on-rails - 在每个循环中创建动态 form_tag

ruby-on-rails - Ubuntu 10.10 上的 Rails 安装失败

ruby-on-rails - 如何使 README_FOR_APP 的内容出现在 doc/app/index.html

ruby - 如何使用 mongoDB 和 Ruby 实现原子堆栈

ruby-on-rails - 如何将你的 rake 任务从 gem 生成器中移动到 rails lib 目录?

mysql - 如何使用 csv 文件更新数据库中的特定行?(rails)

ruby-on-rails - Rake 数据库迁移失败,错误为 "undefined method ` double`"

github - 如何预览RDoc文件?