class JekyllFileProtocol::RelativePathRenderer

Public Class Methods

new(context, path) click to toggle source
# File lib/jekyll-file-protocol/relative_path_renderer.rb, line 5
def initialize(context, path)
  @context  = context
  @page_url = @context.registers[:page]['url']
  @path     = path.strip

  generate_relative_path!
end

Public Instance Methods

render() click to toggle source
# File lib/jekyll-file-protocol/relative_path_renderer.rb, line 13
def render
  @relative_path
end

Protected Instance Methods

count_depth(path) click to toggle source
# File lib/jekyll-file-protocol/relative_path_renderer.rb, line 19
def count_depth(path)
  clear_leading_slashes(@page_url).scan('/').size
end
generate_relative_path!() click to toggle source
# File lib/jekyll-file-protocol/relative_path_renderer.rb, line 27
def generate_relative_path!
  @relative_path = @path
  relative       = @relative_path
  
  return unless is_absolute_path?

  relative       = clear_leading_slashes(relative)

  relative       = ("../" * count_depth(@page_url)) + relative
  @relative_path = relative
end
is_absolute_path?() click to toggle source
# File lib/jekyll-file-protocol/relative_path_renderer.rb, line 23
def is_absolute_path?
  @path.start_with?('/')
end

Private Instance Methods

clear_leading_slashes(path) click to toggle source
# File lib/jekyll-file-protocol/relative_path_renderer.rb, line 41
def clear_leading_slashes(path)
  return path unless path.start_with?('/')
  real_path = path

  if real_path.start_with?('//')
    real_path = real_path[2..-1]
  else
    real_path = real_path[1..-1]
  end
end