Skip to content

Commit

Permalink
Handle links generated by recent versions of RDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
haines committed Jun 12, 2023
1 parent 9c3ae2c commit b21139f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ jobs:

- name: Run tests
run: bin/rake test

- name: Generate docs
run: bin/rake docs
9 changes: 9 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ AllCops:
Layout/LineLength:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/BlockLength:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Naming/FileName:
Exclude:
- lib/yard-relative_markdown_links.rb
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Test against Ruby 3.2 ([#175](https://github.com/haines/yard-relative_markdown_links/pull/175))
* Require Nokogiri ≥ 1.14.3 ([#175](https://github.com/haines/yard-relative_markdown_links/pull/175))

### Fixed
* Handle links generated by recent versions of RDoc ([#175](https://github.com/haines/yard-relative_markdown_links/pull/175))

## [0.4.1] - 2021-11-15
### Changed
* Require MFA to publish gem ([#92](https://github.com/haines/yard-relative_markdown_links/pull/92))
Expand Down
17 changes: 13 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ end

RuboCop::RakeTask.new

desc "Generate documentation"
YARD::Rake::YardocTask.new :doc
CLOBBER << "doc/"
namespace :docs do
desc "Generate documentation"
YARD::Rake::YardocTask.new :generate
CLOBBER << "doc/"

task :default => [:doc, :rubocop, :test]
desc "Check relative links in documentation"
task :check do
abort "Incorrect relative links" unless File.read("doc/file.README.html").include?('href="file.LICENSE.html"')
end
end

task docs: ["docs:generate", "docs:check"]

task default: [:docs, :rubocop, :test]
24 changes: 23 additions & 1 deletion lib/yard/relative_markdown_links.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "nokogiri"
require "set"
require "uri"
require "yard"
require "yard/relative_markdown_links/version"
Expand All @@ -18,14 +19,35 @@ module RelativeMarkdownLinks
# @param [String] text the HTML fragment in which to resolve links.
# @return [String] HTML with relative links to extra files converted to `{file:}` links.
def resolve_links(text)
return super unless options.files

filenames = options.files.to_set(&:filename)

rdoc_filenames = filenames.filter_map { |filename|
# https://github.com/ruby/rdoc/blob/0e060c69f51ec4a877e5cde69b31d47eaeb2a2b9/lib/rdoc/markup/to_html.rb#L364-L366
match = %r{\A(?<dirname>(?:[^/#]*/)*+)(?<basename>[^/#]+)\.(?<ext>rb|rdoc|md)\z}i.match(filename)
next unless match

["#{match[:dirname]}#{match[:basename].tr('.', '_')}_#{match[:ext]}.html", filename]
}.to_h

html = Nokogiri::HTML.fragment(text)

html.css("a[href]").each do |link|
href = URI(link["href"])
next unless href.relative?

if filenames.include?(href.path)
link.replace "{file:#{href} #{link.inner_html}}"
next
end

next unless href.relative? && options.files.map(&:filename).include?(href.path)
href.path = rdoc_filenames[href.path]
next unless href.path && filenames.include?(href.path)

link.replace "{file:#{href} #{link.inner_html}}"
end

super(html.to_s)
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/yard/relative_markdown_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def test_relative_markdown_links
assert_equal expected_output, @template.resolve_links(input)
end

def test_relative_markdown_links_from_rdoc
input = <<~HTML
<p>Hello, <a href="world_md.html">World</a></p>
HTML

expected_output = <<~HTML
<p>Hello, {file:world.md World}</p>
HTML

assert_equal expected_output, @template.resolve_links(input)
end

def test_relative_nonmarkdown_links
input = <<~HTML
<p>Hello, <a href="planet.yaml">Planet</a></p>
Expand Down

0 comments on commit b21139f

Please sign in to comment.