Class: JekyllThemeGuidesMbland::NamespaceFlattener

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-theme-guides-mbland/namespace_flattener.rb

Class Method Summary collapse

Class Method Details

.check_for_collisions(flat_to_orig) ⇒ Object

Raises:

  • (StandardError)

28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll-theme-guides-mbland/namespace_flattener.rb', line 28

def self.check_for_collisions(flat_to_orig)
  collisions = flat_to_orig.map do |flattened, orig|
    [flattened, orig] if orig.size != 1
  end.compact

  return if collisions.empty?

  messages = collisions.map { |flat, orig| "#{flat}: #{orig.join(', ')}" }
  raise StandardError, "collisions in flattened namespace between\n  " +
    messages.join("\n  ")
end

.flat_url(url) ⇒ Object


24
25
26
# File 'lib/jekyll-theme-guides-mbland/namespace_flattener.rb', line 24

def self.flat_url(url)
  url == '/' ? url : "/#{url.split('/')[1..-1].last}/"
end

.flatten_page_urls(page, flat_to_orig) ⇒ Object


13
14
15
16
17
18
19
20
21
22
# File 'lib/jekyll-theme-guides-mbland/namespace_flattener.rb', line 13

def self.flatten_page_urls(page, flat_to_orig)
  return if page.data['title'].nil?
  orig_url = page.data[:working_url]
  flattened_url = flat_url(orig_url)
  (flat_to_orig[flattened_url] ||= []) << orig_url
  page.data['permalink'] = flattened_url
  (page.data['breadcrumbs'] || []).each do |crumb|
    crumb['url'] = flat_url(crumb['url'])
  end
end

.flatten_url_namespace(site, docs) ⇒ Object


3
4
5
# File 'lib/jekyll-theme-guides-mbland/namespace_flattener.rb', line 3

def self.flatten_url_namespace(site, docs)
  flatten_urls(docs) if site.config['flat_namespace']
end

.flatten_urls(docs) ⇒ Object


7
8
9
10
11
# File 'lib/jekyll-theme-guides-mbland/namespace_flattener.rb', line 7

def self.flatten_urls(docs)
  flat_to_orig = {}
  docs.each { |page| flatten_page_urls(page, flat_to_orig) }
  check_for_collisions(flat_to_orig)
end