Class: Docs::NormalizePathsFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/docs/filters/core/normalize_paths.rb

Constant Summary

Constants inherited from Filter

Filter::SCHEME_RGX

Instance Method Summary collapse

Methods inherited from Filter

#absolute_url_string?, #at_css, #at_xpath, #base_url, #css, #current_url, #fragment_url_string?, #initial_page?, #parse_html, #relative_url_string?, #root_page?, #root_path, #root_url, #slug, #subpath, #subpath_to, #xpath

Instance Method Details

#callObject


3
4
5
6
7
8
9
10
11
12
13
# File 'lib/docs/filters/core/normalize_paths.rb', line 3

def call
  result[:path] = path
  result[:store_path] = store_path

  css('a').each do |link|
    next unless (href = link['href']) && relative_url_string?(href)
    link['href'] = normalize_href(href)
  end

  doc
end

#normalize_href(href) ⇒ Object


27
28
29
30
31
32
33
# File 'lib/docs/filters/core/normalize_paths.rb', line 27

def normalize_href(href)
  url = URL.parse(href)
  url.path = normalize_path(url.path)
  url
rescue URI::InvalidURIError
  href
end

#normalize_path(path) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/docs/filters/core/normalize_paths.rb', line 35

def normalize_path(path)
  path = path.downcase

  if path == '.'
    'index'
  elsif path.end_with? '/'
    "#{path}index"
  elsif path.end_with? '.html'
    path[0..-6]
  else
    path
  end
end

#normalized_subpathObject


23
24
25
# File 'lib/docs/filters/core/normalize_paths.rb', line 23

def normalized_subpath
  normalize_path subpath.remove(/\A\//)
end

#pathObject


15
16
17
# File 'lib/docs/filters/core/normalize_paths.rb', line 15

def path
  @path ||= root_page? ? 'index' : normalized_subpath
end

#store_pathObject


19
20
21
# File 'lib/docs/filters/core/normalize_paths.rb', line 19

def store_path
  File.extname(path) != '.html' ? "#{path}.html" : path
end