Skip to content

Commit

Permalink
Merge pull request #175 from haines/ruby-3
Browse files Browse the repository at this point in the history
Handle links generated by recent versions of RDoc
  • Loading branch information
haines authored Jun 12, 2023
2 parents d54a6d4 + b21139f commit 85bf488
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 54 deletions.
42 changes: 8 additions & 34 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,29 @@ jobs:
strategy:
matrix:
ruby:
- "2.6"
- "2.7"
- "3.0"
- "3.1"
- "3.2"

name: Ruby ${{ matrix.ruby }}

runs-on: ubuntu-latest

container:
image: ruby:${{ matrix.ruby }}-alpine

env:
BUNDLE_PATH: ${{ github.workspace }}/vendor/bundle

steps:
- name: Install dependencies
run: apk add build-base git tar

- name: Check out source code
uses: actions/checkout@v3

- name: Install Bundler
run: bin/install-bundler

- name: Compute cache key
id: cache-key
run: |
source /etc/os-release
printf \
"::set-output name=cache-key::alpine-%s-ruby-%s\n" \
"${VERSION_ID}" \
"${RUBY_VERSION}"
- name: Cache gems
uses: actions/cache@v3
- name: Install dependencies
uses: ruby/setup-ruby@v1
with:
key: ${{ steps.cache-key.outputs.cache-key }}-gems-${{ hashFiles('Gemfile.lock') }}
path: vendor/bundle

- name: Install gems
run: bin/bundle install
env:
BUNDLE_FROZEN: true
BUNDLE_JOBS: 4
BUNDLE_RETRY: 3
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Run RuboCop
run: bin/rake rubocop

- name: Run tests
run: bin/rake test

- name: Generate docs
run: bin/rake docs
14 changes: 13 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require:

AllCops:
NewCops: enable
TargetRubyVersion: 2.6
TargetRubyVersion: 3.0
Exclude:
- bin/bundle
- bin/rake
Expand All @@ -13,9 +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
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.1
3.2.2
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Changed
* Require Ruby ≥ 2.6 ([#107](https://github.com/haines/yard-relative_markdown_links/pull/107))
* Require Ruby ≥ 3.0 ([#175](https://github.com/haines/yard-relative_markdown_links/pull/175))
* Test against Ruby 3.1 ([#107](https://github.com/haines/yard-relative_markdown_links/pull/107))
* 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
Expand Down
21 changes: 12 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
yard-relative_markdown_links (0.4.1)
nokogiri (~> 1.8)
nokogiri (>= 1.14.3, < 2)

GEM
remote: https://rubygems.org/
Expand All @@ -11,22 +11,24 @@ GEM
coderay (1.1.3)
json (2.6.3)
method_source (1.0.0)
mini_portile2 (2.8.0)
mini_portile2 (2.8.2)
minitest (5.18.0)
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
nokogiri (1.15.2)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.13.10-x86_64-darwin)
nokogiri (1.15.2-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.10-x86_64-linux)
nokogiri (1.15.2-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.2-x86_64-linux)
racc (~> 1.4)
parallel (1.23.0)
parser (3.2.2.0)
ast (~> 2.4.1)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
racc (1.6.1)
racc (1.7.0)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.8.0)
Expand All @@ -52,8 +54,9 @@ GEM
yard (0.9.34)

PLATFORMS
arm64-darwin
ruby
x86_64-darwin-20
x86_64-darwin
x86_64-linux

DEPENDENCIES
Expand All @@ -68,4 +71,4 @@ DEPENDENCIES
yard-relative_markdown_links!

BUNDLED WITH
2.3.9
2.4.13
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
15 changes: 12 additions & 3 deletions yard-relative_markdown_links.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/haines/yard-relative_markdown_links"
spec.license = "MIT"

spec.files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0").reject { |path| path.match(%r{^test/}) } }
spec.files = Dir[
"lib/**/*.rb",
".yardopts",
"CHANGELOG.md",
"CODE_OF_CONDUCT.md",
"LICENSE.md",
"README.md",
"yard-relative_markdown_links.gemspec"
]

spec.require_paths = ["lib"]

spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
Expand All @@ -27,7 +36,7 @@ Gem::Specification.new do |spec|
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["yard.run"] = "yri"

spec.required_ruby_version = ">= 2.6"
spec.required_ruby_version = ">= 3.0"

spec.add_dependency "nokogiri", "~> 1.8"
spec.add_dependency "nokogiri", ">= 1.14.3", "< 2"
end

0 comments on commit 85bf488

Please sign in to comment.