Skip to content

Commit

Permalink
Merge pull request #249 from jonathanhefner/helpers-method_source_cod…
Browse files Browse the repository at this point in the history
…e_and_url

Factor out `method_source_code_and_url` helper
  • Loading branch information
jonathanhefner authored Jul 23, 2023
2 parents 260fd47 + 38e9a37 commit 3474888
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
37 changes: 13 additions & 24 deletions lib/rdoc/generator/template/rails/_context.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -158,39 +158,28 @@
</div>
<% end %>

<% if method.token_stream %>
<% markup = method.markup_code %>
<% source_code, source_url = method_source_code_and_url(method) %>
<% if source_code || source_url %>
<div class="sourcecode">
<%
# generate github link
github = if options.github
if markup =~ /File\s(\S+), line (\d+)/
path = $1
line = $2.to_i
end
path && github_url(path)
else
false
end

ghost = method.instance_of?(RDoc::GhostMethod)
%>
<p class="source-link">
<% if !ghost || github %> Source: <% end %>
Source:

<% unless ghost %>
<% if source_code %>
<a href="javascript:toggleSource('<%= method.aref %>_source')" id="l_<%= method.aref %>_source">show</a>
<% end %>

<% if !ghost && github %> | <% end %>
<% if source_code && source_url %> | <% end %>

<% if github %>
<a href="<%= "#{github}#L#{line}" %>" target="_blank" class="github_url">on GitHub</a>
<% if source_url %>
<a href="<%= source_url %>" target="_blank" class="github_url">on GitHub</a>
<% end %>
</p>
<div id="<%= method.aref %>_source" class="dyn-source">
<pre><code class="ruby"><%= markup %></code></pre>
</div>

<% if source_code %>
<div id="<%= method.aref %>_source" class="dyn-source">
<pre><code class="ruby"><%= source_code %></code></pre>
</div>
<% end %>
</div>
<% end %>
</div>
Expand Down
10 changes: 10 additions & 0 deletions lib/sdoc/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ def group_by_first_letter(rdoc_objects)
object.name[/^[a-z]/i]&.upcase || "#"
end
end

def method_source_code_and_url(rdoc_method)
source_code = rdoc_method.markup_code if rdoc_method.token_stream

if source_code&.match(/File\s(\S+), line (\d+)/)
source_url = github_url($1, line: $2)
end

[(source_code unless rdoc_method.instance_of?(RDoc::GhostMethod)), source_url]
end
end
5 changes: 3 additions & 2 deletions lib/sdoc/helpers/github.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module SDoc::Helpers::GitHub
def github_url(relative_path)
def github_url(relative_path, line: nil)
return unless github?
"https://github.com/#{github_repository}/blob/#{git_head_sha1}/#{relative_path}"
line = "#L#{line}" if line
"https://github.com/#{github_repository}/blob/#{git_head_sha1}/#{relative_path}#{line}"
end

def github?
Expand Down
71 changes: 71 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@

_(@helpers.github_url("foo/bar/qux.rb")).must_be_nil
end

it "supports :line option" do
_(@helpers.github_url("foo/bar/qux.rb", line: 123)).
must_match %r"\Ahttps://github.com/.+/foo/bar/qux\.rb#L123\z"
end
end

describe "#link_to" do
Expand Down Expand Up @@ -241,4 +246,70 @@ def Qux; end
_(@helpers.group_by_first_letter(context.method_list)).must_equal expected
end
end

describe "#method_source_code_and_url" do
before :each do
@helpers.options.github = true
end

it "returns source code and GitHub URL for a given RDoc::AnyMethod" do
method = rdoc_top_level_for(<<~RUBY).find_module_named("Foo").find_method("bar", false)
module Foo # line 1
def bar # line 2
# line 3
end
end
RUBY

source_code, source_url = @helpers.method_source_code_and_url(method)

_(source_code).must_match %r"# File .+\.rb, line 2\b"
_(source_code).must_include "line 3"
_(source_url).must_match %r"\Ahttps://github.com/.+\.rb#L2\z"
end

it "returns nil source code when given method is an RDoc::GhostMethod" do
method = rdoc_top_level_for(<<~RUBY).find_module_named("Foo").find_method("bar", false)
module Foo # line 1
##
# :method: bar
end
RUBY

source_code, source_url = @helpers.method_source_code_and_url(method)

_(source_code).must_be_nil
_(source_url).must_match %r"\Ahttps://github.com/.+\.rb#L3\z"
end

it "returns nil source code when given method is an alias" do
method = rdoc_top_level_for(<<~RUBY).find_module_named("Foo").find_method("bar", false)
module Foo # line 1
def qux; end
alias bar qux
end
RUBY

source_code, source_url = @helpers.method_source_code_and_url(method)

_(source_code).must_be_nil
# Unfortunately, source_url is also nil because RDoc does not provide the
# source code location in this case.
end

it "returns nil GitHub URL when options.github is false" do
@helpers.options.github = false

method = rdoc_top_level_for(<<~RUBY).find_module_named("Foo").find_method("bar", false)
module Foo # line 1
def bar; end # line 2
end
RUBY

source_code, source_url = @helpers.method_source_code_and_url(method)

_(source_code).must_match %r"# File .+\.rb, line 2\b"
_(source_url).must_be_nil
end
end
end

0 comments on commit 3474888

Please sign in to comment.