Module: RelativeUrl

Defined in:
lib/almirah/relative_url.rb

Overview

Computes the relative URL between two generated pages, each given as a path relative to the build root (e.g. “decisions/release 0.4.1/adr-185.html”). Produces forward-slash separators with spaces percent-encoded. Shared by all internal cross-document links (ADR-186).

Class Method Summary collapse

Class Method Details

.between(from_output_rel, to_output_rel, fragment: nil) ⇒ Object



12
13
14
15
16
17
# File 'lib/almirah/relative_url.rb', line 12

def between(from_output_rel, to_output_rel, fragment: nil)
  from_dir = Pathname.new(from_output_rel).dirname
  rel = Pathname.new(to_output_rel).relative_path_from(from_dir).to_s
  url = rel.split('/').map { |segment| encode_segment(segment) }.join('/')
  fragment && !fragment.empty? ? "#{url}##{fragment}" : url
end

.encode_segment(segment) ⇒ Object



19
20
21
# File 'lib/almirah/relative_url.rb', line 19

def encode_segment(segment)
  segment.gsub(' ', '%20')
end