Class: Docs::InternalUrlsFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/docs/filters/core/internal_urls.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
# File 'lib/docs/filters/core/internal_urls.rb', line 3

def call
  unless skip_links?
    follow_links? ? update_and_follow_links : update_links
  end
  doc
end

#effective_urlObject


91
92
93
# File 'lib/docs/filters/core/internal_urls.rb', line 91

def effective_url
  @effective_url ||= current_url == root_url ? index_url : current_url
end

#follow_links?Boolean


35
36
37
# File 'lib/docs/filters/core/internal_urls.rb', line 35

def follow_links?
  !(context[:follow_links] && context[:follow_links].call(self) == false)
end

#index_urlObject


87
88
89
# File 'lib/docs/filters/core/internal_urls.rb', line 87

def index_url
  @index_url ||= base_url.merge path: File.join(base_url.path, '')
end

#internal_path_to(url) ⇒ Object


81
82
83
84
85
# File 'lib/docs/filters/core/internal_urls.rb', line 81

def internal_path_to(url)
  url = index_url if url == root_url
  path = effective_url.relative_path_to(url)
  URL.new(path: path, query: url.query, fragment: url.fragment).to_s
end

#normalize_subpath(path) ⇒ Object


53
54
55
56
57
58
59
60
# File 'lib/docs/filters/core/internal_urls.rb', line 53

def normalize_subpath(path)
  case context[:trailing_slash]
  when true
    path << '/' unless path.end_with?('/')
  when false
    path.slice!(-1) if path.end_with?('/')
  end
end

#normalize_url(url, subpath) ⇒ Object


76
77
78
79
# File 'lib/docs/filters/core/internal_urls.rb', line 76

def normalize_url(url, subpath)
  url.merge! path: base_url.path + subpath
  url.normalize!
end

#parse_url(str) ⇒ Object


47
48
49
50
51
# File 'lib/docs/filters/core/internal_urls.rb', line 47

def parse_url(str)
  str && absolute_url_string?(str) && URL.parse(str)
rescue URI::InvalidURIError
  nil
end

#skip_links?Boolean


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

def skip_links?
  if context[:skip_links].is_a? Proc
    context[:skip_links].call self
  else
    context[:skip_links]
  end
end

#skip_subpath?(path) ⇒ Boolean


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/docs/filters/core/internal_urls.rb', line 62

def skip_subpath?(path)
  if context[:only] || context[:only_patterns]
    return true unless context[:only].try(:any?) { |value| path.casecmp(value) == 0 } ||
                       context[:only_patterns].try(:any?) { |value| path =~ value }
  end

  if context[:skip] || context[:skip_patterns]
    return true if context[:skip].try(:any?) { |value| path.casecmp(value) == 0 } ||
                   context[:skip_patterns].try(:any?) { |value| path =~ value }
  end

  false
end

#to_internal_url(str) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/docs/filters/core/internal_urls.rb', line 39

def to_internal_url(str)
  return unless (url = parse_url(str)) && (subpath = subpath_to(url))
  normalize_subpath(subpath)
  return if skip_subpath?(subpath)
  normalize_url(url, subpath)
  url
end

19
20
21
22
23
24
25
# File 'lib/docs/filters/core/internal_urls.rb', line 19

def update_and_follow_links
  urls = result[:internal_urls] = []
  update_links do |url|
    urls << url.merge!(fragment: nil).to_s
  end
  urls.uniq!
end

10
11
12
13
14
15
16
17
# File 'lib/docs/filters/core/internal_urls.rb', line 10

def update_links
  css('a').each do |link|
    next if context[:skip_link].is_a?(Proc) && context[:skip_link].call(link)
    next unless url = to_internal_url(link['href'])
    link['href'] = internal_path_to(url)
    yield url if block_given?
  end
end