Skip to content

Commit fa048a0

Browse files
committed
[ruby/rdoc] Add links to the commits
ruby/rdoc@1821628076
1 parent f3f1a66 commit fa048a0

File tree

2 files changed

+107
-36
lines changed

2 files changed

+107
-36
lines changed

lib/rdoc/parser/changelog.rb

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ def group_entries entries
132132
def parse_entries
133133
@time_cache ||= {}
134134

135-
if /\A(?:.*\n){,3}commit\s/ =~ @content
135+
if /\A((?:.*\n){,3})commit\s/ =~ @content
136136
class << self; prepend Git; end
137+
parse_info($1)
137138
return parse_entries
138139
end
139140

@@ -208,6 +209,11 @@ def scan
208209
end
209210

210211
module Git
212+
def parse_info(info)
213+
/^\s*base-url\s*=\s*(.*\S)/ =~ info
214+
@base_url = $1
215+
end
216+
211217
def parse_entries
212218
entries = []
213219

@@ -216,7 +222,9 @@ def parse_entries
216222
if /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+) *([-+]\d\d)(\d\d)/ =~ date
217223
time = Time.new($1, $2, $3, $4, $5, $6, "#{$7}:#{$8}")
218224
@time_cache[entry_name] = time
219-
entries << [entry_name, [author, date, entry_body]]
225+
author.sub!(/\s*<(.*)>/, '')
226+
email = $1
227+
entries << [entry_name, [author, email, date, entry_body]]
220228
end
221229
end
222230

@@ -226,31 +234,88 @@ def parse_entries
226234
def create_entries entries
227235
# git log entries have no strictly itemized style like the old
228236
# style, just assume Markdown.
229-
out = []
230-
entries.each do |entry, (author, date, body)|
231-
title = RDoc::Markup::Heading.new(3, "#{date} #{author}")
232-
title.extend(Aref)
233-
title.aref = "label-#{entry}"
234-
out << title
235-
out.concat parse_log_entry(body, entry)
237+
entries.map do |commit, entry|
238+
LogEntry.new(@base_url, commit, *entry)
236239
end
237-
out
238240
end
239241

240-
def parse_log_entry(content, sha)
241-
RDoc::Markdown.parse(content).parts.each do |body|
242-
case body
243-
when RDoc::Markup::Heading
244-
body.level += 3
245-
label = body.aref.sub(/\Alabel-/, "label-#{sha}-")
246-
body.extend(Aref)
247-
body.aref = label
242+
LogEntry = Struct.new(:base, :commit, :author, :email, :date, :contents) do
243+
HEADING_LEVEL = 3
244+
245+
def initialize(base, commit, author, email, date, contents)
246+
case contents
247+
when String
248+
contents = RDoc::Markdown.parse(contents).parts.each do |body|
249+
case body
250+
when RDoc::Markup::Heading
251+
body.level += HEADING_LEVEL + 1
252+
end
253+
end
254+
case first = contents[0]
255+
when RDoc::Markup::Paragraph
256+
contents[0] = RDoc::Markup::Heading.new(HEADING_LEVEL + 1, first.text)
257+
end
248258
end
259+
super
260+
end
261+
262+
def level
263+
HEADING_LEVEL
264+
end
265+
266+
def aref
267+
"label-#{commit}"
249268
end
250-
end
251269

252-
module Aref
253-
attr_accessor :aref
270+
def label context = nil
271+
aref
272+
end
273+
274+
def text
275+
case base
276+
when nil
277+
"#{date}"
278+
when /%s/
279+
"{#{date}}[#{base % commit}]"
280+
else
281+
"{#{date}}[#{base}#{commit}]"
282+
end + " {#{author}}[mailto:#{email}]"
283+
end
284+
285+
def accept visitor
286+
visitor.accept_heading self
287+
begin
288+
if visitor.respond_to?(:code_object=)
289+
code_object = visitor.code_object
290+
visitor.code_object = self
291+
end
292+
contents.each do |body|
293+
body.accept visitor
294+
end
295+
ensure
296+
if visitor.respond_to?(:code_object)
297+
visitor.code_object = code_object
298+
end
299+
end
300+
end
301+
302+
def pretty_print q # :nodoc:
303+
q.group(2, '[log_entry: ', ']') do
304+
q.text commit
305+
q.text ','
306+
q.breakable
307+
q.group(2, '[date: ', ']') { q.text date }
308+
q.text ','
309+
q.breakable
310+
q.group(2, '[author: ', ']') { q.text author }
311+
q.text ','
312+
q.breakable
313+
q.group(2, '[email: ', ']') { q.text email }
314+
q.text ','
315+
q.breakable
316+
q.pp contents
317+
end
318+
end
254319
end
255320
end
256321
end

test/rdoc/test_rdoc_parser_changelog.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def test_parse_entries_git
281281

282282
expected = [
283283
[ "709bed2afaee50e2ce80",
284-
[ "git <[email protected]>",
284+
[ "git", "[email protected]",
285285
"2021-01-21 01:03:52 +0900",
286286
"* 2021-01-21 [ci skip]\n"]]]
287287

@@ -341,7 +341,7 @@ def test_scan_git
341341
342342
* 2021-01-20 [ci skip]
343343
344-
commit de5f8a92d5001799bedb3b1a271a2d9b23c6c8fb
344+
commit\ de5f8a92d5001799bedb3b1a271a2d9b23c6c8fb
345345
Author: Masataka Pocke Kuwabara <[email protected]>
346346
Date: 2021-01-01 14:25:08 +0900
347347
@@ -372,23 +372,26 @@ def test_scan_git
372372
blank_line,
373373
head(2, '2021-01-21'),
374374
blank_line,
375-
head(3, '2021-01-21 01:03:52 +0900 git <[email protected]>'),
376-
list(:BULLET, item(nil, para('2021-01-21 [ci skip]'))),
375+
log_entry(nil, '709bed2afaee50e2ce80',
376+
'git', '[email protected]', '2021-01-21 01:03:52 +0900',
377+
[list(:BULLET, item(nil, para('2021-01-21 [ci skip]')))]),
377378
head(2, '2021-01-20'),
378379
blank_line,
379-
head(3, '2021-01-20 01:58:26 +0900 git <[email protected]>'),
380-
list(:BULLET, item(nil, para('2021-01-20 [ci skip]'))),
380+
log_entry(nil, 'a8dc5156e183489c5121',
381+
'git', '[email protected]', '2021-01-20 01:58:26 +0900',
382+
[list(:BULLET, item(nil, para('2021-01-20 [ci skip]')))]),
381383
head(2, '2021-01-01'),
382384
blank_line,
383-
head(3, '2021-01-01 14:25:08 +0900 Masataka Pocke Kuwabara <[email protected]>'),
384-
para('Make args info for RubyVM::AST to available on endless method without parens'),
385-
head(4, 'Problem'),
386-
para("Arguments information is missing for endless method without parens.\n" +
387-
"For example:"),
388-
verb("# ok\n").tap {|v| v.format = :ruby},
389-
para('It causes an error if a program expects <code>args</code> node exists.'),
390-
head(4, 'Solution'),
391-
para('Call <code>new_args</code> on this case.'))
385+
log_entry(nil, 'de5f8a92d5001799bedb',
386+
'Masataka Pocke Kuwabara', '[email protected]', '2021-01-01 14:25:08 +0900',
387+
[head(4, 'Make args info for RubyVM::AST to available on endless method without parens'),
388+
head(5, 'Problem'),
389+
para("Arguments information is missing for endless method without parens.\n" +
390+
"For example:"),
391+
verb("# ok\n").tap {|v| v.format = :ruby},
392+
para('It causes an error if a program expects <code>args</code> node exists.'),
393+
head(5, 'Solution'),
394+
para('Call <code>new_args</code> on this case.')]))
392395

393396
expected.file = @top_level
394397

@@ -400,5 +403,8 @@ def util_parser content = ''
400403
@top_level, @tempfile.path, content, @options, @stats
401404
end
402405

406+
def log_entry(*a)
407+
RDoc::Parser::ChangeLog::Git::LogEntry.new(*a)
408+
end
403409
end
404410

0 commit comments

Comments
 (0)