Skip to content

Commit

Permalink
Simplify relative link logic, removing unnecessary code
Browse files Browse the repository at this point in the history
* Most of the logic is legacy from the very early HTMLProofer, and is irrelevant not since `@filename` can only be the file where the link is defined, with `File.join` properly handling both same-directory, nested, and parent links, together with the ultimate `File.expand_path` in `absolute_path.
* The legacy logic comes pretty much from #6 and #23.
* This way we can also avoid `File.exist`, which can ultimately be delegated to checking the existence, not constructing the path.
  • Loading branch information
riccardoporreca committed Mar 14, 2023
1 parent b4c76f7 commit e0e0170
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions lib/html_proofer/attribute/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,28 +136,12 @@ def absolute_path
def file_path
return if path.nil? || path.empty?

path_dot_ext = ""

path_dot_ext = path + @runner.options[:assume_extension] unless blank?(@runner.options[:assume_extension])

base = if absolute_path?(path) # path relative to root
# either overwrite with root_dir; or, if source is directory, use that; or, just get the current file's dirname
@runner.options[:root_dir] || (File.directory?(@source) ? @source : File.dirname(@source))
# relative links, path is a file
elsif File.exist?(File.expand_path(
path,
@source,
)) || File.exist?(File.expand_path(path_dot_ext, @source))
File.dirname(@filename)
# relative links in nested dir, path is a file
elsif File.exist?(File.join(
File.dirname(@filename),
path,
)) || File.exist?(File.join(File.dirname(@filename), path_dot_ext))
File.dirname(@filename)
# relative link, path is a directory
else
@filename
File.dirname(@filename)
end

file = File.join(base, path)
Expand Down

0 comments on commit e0e0170

Please sign in to comment.