-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
normalize_paths.rb
56 lines (46 loc) · 1.1 KB
/
normalize_paths.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true
module Docs
class NormalizePathsFilter < Filter
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
def path
@path ||= root_page? ? 'index' : normalized_subpath
end
def store_path
File.extname(path) != '.html' ? "#{path}.html" : path
end
def normalized_subpath
normalize_path subpath.remove(/\A\//)
end
def normalize_href(href)
url = URL.parse(href)
url.send(:set_path, normalize_path(url.path))
url
rescue URI::InvalidURIError
href
end
def normalize_path(path)
path = path.downcase
if context[:decode_and_clean_paths]
path = CGI.unescape(path)
path = clean_path(path)
end
if path == '.'
'index'
elsif path.end_with? '/'
"#{path}index"
elsif path.end_with? '.html'
path[0..-6]
else
path
end
end
end
end