-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test documentation links against a TOC file
- Loading branch information
1 parent
a13c7a5
commit cf24aa3
Showing
4 changed files
with
97 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'json' | ||
require 'uri' | ||
|
||
class LinksChecker | ||
def initialize(toc:, filename_to_link:) | ||
toc_source = JSON.parse(File.read(toc)) | ||
f2l = JSON.parse(File.read(filename_to_link)) | ||
|
||
@toc = fix_folder_titles(toc_source, f2l) | ||
end | ||
|
||
def fix_folder_titles(toc, f2l) | ||
toc.transform_keys { |file| f2l[file] || file } | ||
end | ||
|
||
def test_link(url) | ||
doc_path, anchor = decompose_link(url) | ||
|
||
return false unless doc_path | ||
|
||
anchors = navigate_path(doc_path) | ||
|
||
return anchors unless anchor | ||
|
||
anchors&.include?(anchor) | ||
end | ||
|
||
private | ||
|
||
# Will decompose URL from https://localhost:3000/documentation/en-us/red_hat_satellite/6.15/html/managing_configurations_using_ansible_integration_in_red_hat_satellite/getting_started_with_ansible_in_satellite_ansible#Importing_Ansible_Roles_and_Variables_ansible | ||
# to two parts: | ||
# path: managing_configurations_using_ansible_integration_in_red_hat_satellite/getting_started_with_ansible_in_satellite_ansible | ||
# chapter: Importing_Ansible_Roles_and_Variables_ansible | ||
def decompose_link(link) | ||
uri = URI.parse(link) | ||
|
||
doc_path = extract_doc_path(uri.path) | ||
anchor = uri.fragment | ||
|
||
[doc_path, anchor] | ||
end | ||
|
||
def extract_doc_path(path) | ||
# take the right part of the path after the '/html/' from the original link | ||
%r{/html/(.*)}.match(path)&.[](1) | ||
end | ||
|
||
def navigate_path(path, hash = @toc) | ||
return nil unless hash | ||
|
||
split = path.match(%r{([^/]*)/(.*)}) | ||
first = split&.[](1) || path | ||
rest = split&.[](2) | ||
|
||
inner_hash = hash[first] | ||
|
||
return inner_hash if rest.empty? | ||
|
||
navigate_path(rest, inner_hash) | ||
end | ||
end |