Class: Docs::NormalizeUrlsFilter

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

Constant Summary collapse

ATTRIBUTES =
{ a: 'href', img: 'src', iframe: 'src' }

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


5
6
7
8
9
10
# File 'lib/docs/filters/core/normalize_urls.rb', line 5

def call
  ATTRIBUTES.each_pair do |tag, attribute|
    update_attribute(tag, attribute)
  end
  doc
end

#fix_url(url) ⇒ Object


33
34
35
36
37
38
39
40
# File 'lib/docs/filters/core/normalize_urls.rb', line 33

def fix_url(url)
  return unless context[:replace_paths]
  path = subpath_to(url)

  if context[:replace_paths].has_key?(path)
    url.path = url.path.sub %r[#{path}\z], context[:replace_paths][path]
  end
end

#fix_url_string(str) ⇒ Object


42
43
44
45
46
# File 'lib/docs/filters/core/normalize_urls.rb', line 42

def fix_url_string(str)
  str = context[:replace_urls][str]  || str if context[:replace_urls]
  str = context[:fix_urls].call(str) || str if context[:fix_urls]
  str
end

#normalize_url(str) ⇒ Object


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

def normalize_url(str)
  url = to_absolute_url(str)
  fix_url(url)
  fix_url_string(url.to_s)
rescue URI::InvalidURIError
  '#'
end

#to_absolute_url(str) ⇒ Object


28
29
30
31
# File 'lib/docs/filters/core/normalize_urls.rb', line 28

def to_absolute_url(str)
  url = URL.parse(str)
  url.relative? ? current_url.join(url) : url
end

#update_attribute(tag, attribute) ⇒ Object


12
13
14
15
16
17
18
# File 'lib/docs/filters/core/normalize_urls.rb', line 12

def update_attribute(tag, attribute)
  css(tag.to_s).each do |node|
    next unless value = node[attribute]
    next if fragment_url_string?(value)
    node[attribute] = normalize_url(value)
  end
end