Skip to content

Commit 7391aa2

Browse files
authored
Updates to show GitHub source url (#5)
* Remove `SDoc::GitHub` module * Create `SDoc::Helpers::Git` module * Add `SDoc::Helpers#method_source_code_and_url`
1 parent 31bf90c commit 7391aa2

File tree

5 files changed

+82
-97
lines changed

5 files changed

+82
-97
lines changed

lib/rdoc/generator/template/rails/_context.rhtml

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -170,33 +170,22 @@
170170
</div>
171171
<% end %>
172172

173-
<% if method.token_stream %>
174-
<% markup = method.markup_code %>
175-
<div class="sourcecode">
176-
<%
177-
# generate github link
178-
github = if options.github
179-
if markup =~ /File\s(\S+), line (\d+)/
180-
path = $1
181-
line = $2.to_i
182-
end
183-
path && github_url(path)
184-
else
185-
false
186-
end
187-
%>
188-
<p class="source-link">
189-
Source:
190-
<% if github %>
191-
<a href="<%= "#{github}#L#{line}" %>" target="_blank" class="github_url">on GitHub</a>
192-
<% end %>
193-
</p>
194-
<div id="<%= method.aref %>_source">
195-
<pre><code class="ruby"><%= markup %></code></pre>
196-
</div>
197-
</div>
198-
<% end %>
199-
</div>
173+
<% source_code, source_url = method_source_code_and_url(method) %>
174+
<% if source_code %>
175+
<details class="method__source">
176+
<summary>
177+
<span class="label">Source code</span>
178+
</summary>
179+
180+
<pre><code class="ruby"><%= source_code %></code></pre>
181+
<% if source_url %>
182+
<a href="<%= source_url %>" target="_blank" class="github_url">See on GitHub</a>
183+
<% end %>
184+
</details>
185+
<% elsif source_url %>
186+
<a href="<%= source_url %>" target="_blank" class="github_url">See on GitHub</a>
187+
<% end %>
188+
</div>
200189
<% end %><%# methods.each %>
201190
<% end %><%# visibilities.each %>
202191
<% end %><%# context.methods_by_type %>

lib/sdoc/generator.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'fileutils'
44
require 'json'
55

6-
require 'sdoc/github'
76
require 'sdoc/templatable'
87
require 'sdoc/helpers'
98
require 'sdoc/version'
@@ -28,7 +27,6 @@ class RDoc::Generator::SDoc
2827
DESCRIPTION = 'Searchable HTML documentation'
2928

3029
include ERB::Util
31-
include SDoc::GitHub
3230
include SDoc::Templatable
3331
include SDoc::Helpers
3432

@@ -55,13 +53,16 @@ def self.setup_options(options)
5553
opt = options.option_parser
5654
opt.separator nil
5755
opt.separator "SDoc generator options:"
56+
57+
# FIXME: Use options
58+
# options.github = true
5859
opt.separator nil
5960
opt.on("--github", "-g",
6061
"Generate links to github.") do |value|
6162
options.github = true
6263
end
63-
opt.separator nil
6464

65+
opt.separator nil
6566
opt.on("--version", "-v", "Output current version") do
6667
puts SDoc::VERSION
6768
exit
@@ -109,7 +110,7 @@ def file_dir
109110
FILE_DIR
110111
end
111112

112-
private
113+
private
113114

114115
### Output progress information if debugging is enabled
115116
def debug_msg( *msg )

lib/sdoc/github.rb

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/sdoc/helpers.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module SDoc::Helpers
2+
require_relative "helpers/git"
3+
include ::SDoc::Helpers::Git
4+
25
# Strips out HTML tags from a given string.
36
#
47
# Example:
@@ -21,12 +24,13 @@ def truncate(text, options = {})
2124
end
2225
end
2326

24-
protected
25-
def group_name name
26-
if match = name.match(/^([a-z])/i)
27-
match[1].upcase
28-
else
29-
'#'
27+
def method_source_code_and_url(rdoc_method)
28+
source_code = rdoc_method.tokens_to_s if rdoc_method.token_stream
29+
30+
if source_code&.match(/File\s(\S+), line (\d+)/)
31+
source_url = github_url(Regexp.last_match(1), line: Regexp.last_match(2))
3032
end
33+
34+
[rdoc_method.instance_of?(RDoc::GhostMethod) ? nil : source_code, source_url]
3135
end
3236
end

lib/sdoc/helpers/git.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module SDoc::Helpers::Git
2+
def github_url(relative_path, line: nil)
3+
return unless github?
4+
line = "#L#{line}" if line
5+
"https://github.com/#{github_repository}/blob/#{git_head_sha1}/#{relative_path}#{line}"
6+
end
7+
8+
def github?
9+
@options.github && git? && github_repository
10+
end
11+
12+
attr_writer :git_repo_path
13+
14+
def git?
15+
@git_repo_path ||= Dir.chdir(@options.root) { `git rev-parse --show-toplevel 2> /dev/null`.chomp }
16+
!@git_repo_path.empty?
17+
end
18+
19+
def git_command(command)
20+
Dir.chdir(@options.root) { `git #{command}`.chomp } if git?
21+
end
22+
23+
attr_writer :git_head_branch
24+
25+
def git_head_branch
26+
@git_head_branch ||= git_command("rev-parse --abbrev-ref HEAD")
27+
end
28+
29+
attr_writer :git_head_sha1
30+
31+
def git_head_sha1
32+
@git_head_sha1 ||= git_command("rev-parse HEAD")
33+
end
34+
35+
attr_writer :git_head_timestamp
36+
37+
def git_head_timestamp
38+
@git_head_timestamp ||= git_command("show -s --format=%cI HEAD")
39+
end
40+
41+
attr_writer :git_origin_url
42+
43+
def git_origin_url
44+
@git_origin_url ||= git_command("config --get remote.origin.url")
45+
end
46+
47+
def github_repository
48+
return @github_repository if defined?(@github_repository)
49+
@github_repository = git_origin_url.chomp(".git")[%r"github\.com[/:](.+)", 1]
50+
end
51+
end

0 commit comments

Comments
 (0)