Skip to content

Commit 2f441c5

Browse files
nobukou
andauthored
Expand GitHub style references in ChangeLog to URL (#1547)
Currently, ruby/ruby formats ChangeLog and expands such references to links. But such links in ChangeLog that is a plain text does not make sense. I think it is suitable to be done at conversion from ChangeLog to HTML --------- Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
1 parent 8fe3e30 commit 2f441c5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/rdoc/parser/changelog.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ def create_entries(entries)
274274
def initialize(base, commit, author, email, date, contents)
275275
case contents
276276
when String
277+
if base&.match(%r[\A([^:/]+:/+[^/]+/)[^/]+/[^/]+/])
278+
repo, host = $&, $1
279+
contents = contents.dup
280+
# base: https://github.com/ruby/ruby/
281+
# Fix #15791 -> Fix [#15791](https://github.com/ruby/ruby/pull/15791)
282+
# GH-15791 -> [GH-15791](https://github.com/ruby/ruby/pull/15791)
283+
# (#15791) -> ([#15791](https://github.com/ruby/ruby/pull/15791))
284+
contents.gsub!(/\b(?:(?i:fix(?:e[sd])?) +)\K\#(\d+\b)|\bGH-(\d+)\b|\(\K\#(\d+)(?=\))/) do
285+
"[#{$&}](#{repo}pull/#{$1 || $2 || $3})"
286+
end
287+
# repo#PR, repo@HASH
288+
# ruby/ruby#15791 -> [ruby/ruby#15791](https://github.com/ruby/ruby/pull/15791)
289+
# ruby/ruby@a8a989b6 -> [ruby/ruby@a8a989b6](https://github.com/ruby/ruby/commit/a8a989b6)
290+
# ref in branckets is not extended
291+
# [ruby/net-imap#543][ruby/ruby#15791] -> [ruby/net-imap#543][ruby/ruby#15791]
292+
contents.gsub!(%r[(?<![-\w#/@]|\]\[)([-\w]+/[-\w]+)(?:@(\h{8,40})|\#(\d+))(?![-\w#/@]|\]\[)]) do
293+
path = defined?($2) ? "commit/#{$2}" : "pull/#{$3}"
294+
"[#{$&}](#{host}#{$1}/#{path})"
295+
end
296+
end
297+
277298
contents = RDoc::Markdown.parse(contents).parts.each do |body|
278299
case body
279300
when RDoc::Markup::Heading

test/rdoc/parser/changelog_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,32 @@ def test_scan_git_commit_date
473473
assert_equal expected, @top_level.comment.parse
474474
end
475475

476+
def test_scan_git_reference
477+
ruby = "https://github.com/ruby/"
478+
repo = "#{ruby}ruby/"
479+
entry = log_entry("#{repo}commit/",
480+
"a8a989b6f651b878c690f5fd0a728e19a54dd2b9",
481+
"Nobuyoshi Nakada", "nobu@ruby-lang.org",
482+
"2026-01-03 13:28:58 +0900",
483+
<<~LOG)
484+
Test net-imap with ruby/net-imap#593
485+
486+
Fix #15791
487+
488+
GH-15791
489+
490+
(#15791)
491+
492+
Fix up [ruby/net-imap#543].
493+
LOG
494+
head, *bodies = entry.contents
495+
assert_include head.text, "{ruby/net-imap#593}[#{ruby}net-imap/pull/593]"
496+
assert_include bodies.shift.text, "Fix {#15791}[#{repo}pull/15791]"
497+
assert_include bodies.shift.text, "{GH-15791}[#{repo}pull/15791]"
498+
assert_include bodies.shift.text, "({#15791}[#{repo}pull/15791])"
499+
assert_include bodies.shift.text, "{ruby/net-imap#543}[#{ruby}net-imap/pull/543]"
500+
end
501+
476502
def util_parser(content = '')
477503
RDoc::Parser::ChangeLog.new @top_level, content, @options, @stats
478504
end

0 commit comments

Comments
 (0)