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

发布于 2024-09-30 10:15:52 字数 1836 浏览 4 评论 0原文

如何将 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]

How do I include a ruby code file, as is, into RDoc?

I have an example.rb file that documents how to use my gem and I would like to include that as one of the files like the README.rdoc and HISTORY.rdoc.

I've already figured out how to convert the ruby source code into HTML using the Syntax gem but I can't figure out how to make RDoc include the file without parsing it.

When I tell RDoc to include the html file it isn't listed and if I fake it out by using rdoc or txt as the file extension it doesn't display properly (the file is still actually html).

I've got a solution that works it's just incredibly ugly. There has got to be a better way to do this that's native to rdoc but I don't see it.

Here's what I have in my 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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流殇 2024-10-07 10:15:52

抱歉,我无法给您实际的答案,但我自己正在查看 sdoc
您可以从 ruby​​ gems 安装gem

Sorry I can't give you an actual answer but I'm looking at sdoc myself.
You can install the gem from ruby gems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文