Module: JekyllFileProtocol::Jekyll::Filters

Defined in:
lib/jekyll-file-protocol/jekyll/filters/relative_path.rb

Instance Method Summary collapse

Instance Method Details

#relative_path(input) ⇒ Object



11
12
13
14
15
# File 'lib/jekyll-file-protocol/jekyll/filters/relative_path.rb', line 11

def relative_path(input)
  return input if input.nil?

  ::JekyllFileProtocol::RelativePathRenderer.new(@context, input).render
end

#relative_tag(input) ⇒ Object



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
# File 'lib/jekyll-file-protocol/jekyll/filters/relative_path.rb', line 17

def relative_tag(input)
  return input if input.nil?

  node   = Nokogiri::HTML.parse(input)
  tags   = nil
  
  # Stylesheet
  if (tags = node.css('link').to_a).size > 0
    return tags.map do |tag|
      tag['href'] = ::JekyllFileProtocol::RelativePathRenderer.new(@context, tag['href']).render
      tag
    end.map(&:to_html)
  end

  # Javascript
  if (tags = node.css('script').to_a).size > 0
    return tags.map do |tag|
      tag['src'] = ::JekyllFileProtocol::RelativePathRenderer.new(@context, tag['src']).render
      tag
    end.map(&:to_html)
  end

  # Image
  if (tags = node.css('img').to_a).size > 0
    return tags.map do |tag|
      tag['src'] = ::JekyllFileProtocol::RelativePathRenderer.new(@context, tag['src']).render
      tag
    end.map(&:to_html)
  end

  input
end