Skip to content

Commit

Permalink
Include files relative to the framework root
Browse files Browse the repository at this point in the history
The Rails documentation uses the `:include:` directive to inline the
README of the framework into the main documentation page.
As the README's aren't in the root directory from where SDoc is run we
need to add the framework path to the include:

    # :include: activesupport/README.md

This results in a warning when installing the gems as generating the
rdoc for the gem is run from the gem/framework root:

    Couldn't find file to include 'activesupport/README.rdoc' from lib/active_support.rb

By Monkeypatching `find_include_file` to find files at the framework
root we can include without the framework path:

    # :include: README.md
  • Loading branch information
p8 committed Jan 17, 2024
1 parent fd538ad commit 8d18c1e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/sdoc/rdoc_monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ def record_location(*)
super unless @ignoring_constants
end
end)

RDoc::Markup::PreProcess.prepend(Module.new do
def find_include_file(name)
to_search = [File.dirname(@input_file_name, 2)].concat @include_path
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)
24 changes: 24 additions & 0 deletions spec/rdoc_monkey_patches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,28 @@ def foo; Float::INFINITY; end
end
end
end

describe RDoc::Markup::PreProcess do
it "includes files relative to the framework root" do
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
FileUtils.mkdir_p("railties/lib")
File.write("railties/lib/rails.rb", <<~RUBY)
# :include: README.md
module Rails
end
RUBY

File.write("railties/README.md", <<~RUBY)
Railties - Gluing the Engine to the Rails
RUBY

rdoc_store = rdoc_dry_run("--files", "railties/lib/rails.rb", "railties/README.md").store

_(rdoc_store.find_class_or_module("Rails").description).
must_match %r"Railties - Gluing the Engine to the Rails"
end
end
end
end
end

0 comments on commit 8d18c1e

Please sign in to comment.