Module: GovukTechDocs::PathHelpers

Included in:
Pages, TableOfContents::Helpers
Defined in:
lib/govuk_tech_docs/path_helpers.rb

Instance Method Summary collapse

Instance Method Details

#get_path_to_resource(config, resource, current_page) ⇒ Object

Calculates the path to the sought resource, taking in to account whether the site has been configured to generate relative or absolute links. Identifies whether the sought resource is an internal or external target: External targets are returned untouched. Path calculation is performed for internal targets. Works for both “Middleman::Sitemap::Resource” resources and plain strings (which may be passed from the site configuration when generating header links).

Parameters:

  • config (Object)
  • resource (Object)
  • current_page (Object)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/govuk_tech_docs/path_helpers.rb', line 17

def get_path_to_resource(config, resource, current_page)
  if resource.is_a?(Middleman::Sitemap::Resource)
    config[:relative_links] ? get_resource_path_relative_to_current_page(config, current_page.path, resource.path) : resource.url
  elsif external_url?(resource)
    resource
  elsif config[:relative_links]
    get_resource_path_relative_to_current_page(config, current_page.path, resource)
  else
    resource
  end
end

#path_to_site_root(config, page_path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/govuk_tech_docs/path_helpers.rb', line 29

def path_to_site_root(config, page_path)
  if config[:relative_links]
    number_of_ascents_to_site_root = page_path.to_s.split("/").reject(&:empty?)[0..-2].length
    ascents = number_of_ascents_to_site_root.zero? ? ["."] : number_of_ascents_to_site_root.times.collect { ".." }
    path_to_site_root = ascents.join("/").concat("/")
  else
    middleman_http_prefix = config[:http_prefix]
    path_to_site_root = middleman_http_prefix.end_with?("/") ? middleman_http_prefix : "#{middleman_http_prefix}/"
  end
  path_to_site_root
end