@@ -67,6 +67,7 @@ def self.for(components, encoding: Encoding)
6767 #
6868 # @parameter target [String] The destination path (where you want to go).
6969 # @parameter from [String] The source path (where you are starting from).
70+ # @parameter explicit [Boolean] Whether same-directory paths should start with `./`.
7071 # @returns [String] The relative path from `from` to `target`.
7172 #
7273 # @example Calculate relative path between pages.
@@ -76,8 +77,8 @@ def self.for(components, encoding: Encoding)
7677 # @example Calculate relative path in same directory.
7778 # Path.relative("/docs/guide.html", "/docs/index.html")
7879 # # => "guide.html"
79- def self . relative ( target , from )
80- return Path [ target ] . relative ( from ) . to_s
80+ def self . relative ( target , from , explicit : false )
81+ return Path [ target ] . relative ( from , explicit : explicit ) . to_s
8182 end
8283
8384 # Initialize a path from either its complete encoded representation or encoded segments.
@@ -357,8 +358,9 @@ def join(other, pop: true, simplify: true)
357358 # Calculate this path relative to another path.
358359 #
359360 # @parameter from [String | Array(String) | Path] The source path.
361+ # @parameter explicit [Boolean] Whether same-directory paths should start with `./`.
360362 # @returns [Path] The relative path from `from` to this path.
361- def relative ( from )
363+ def relative ( from , explicit : false )
362364 target_segments = self . segments
363365 from_segments = Path [ from ] . segments
364366
@@ -386,6 +388,9 @@ def relative(from)
386388 # An empty reference identifies the current document, so identify the current directory explicitly:
387389 if relative_segments == [ "" ]
388390 relative_segments = [ "." , "" ]
391+ elsif explicit && relative_segments . first != ".."
392+ # Identify same-directory references explicitly:
393+ relative_segments . unshift ( "." )
389394 elsif relative_segments . first &.include? ( ":" )
390395 # A colon in the first segment would be interpreted as a URI scheme:
391396 relative_segments . unshift ( "." )
0 commit comments