class Sitepress::Path

Constants

HANDLER_EXTENSIONS

Default handler extensions. Handlers are anything that render or manipulate the contents of the file into a different output, like ERB or HAML.

ROOT_NODE_NAME

The root node name is a blank string.

ROOT_PATH

The name of the root path

Attributes

handler_extensions[W]
basename[R]
dirname[R]
format[R]
handler[R]
node_name[R]
path[R]

Public Class Methods

handler_extensions() click to toggle source
# File lib/sitepress/path.rb, line 24
def handler_extensions
  @handler_extensions ||= HANDLER_EXTENSIONS
end
new(path, path_seperator: File::SEPARATOR, handler_extensions: self.class.handler_extensions) click to toggle source
# File lib/sitepress/path.rb, line 29
def initialize(path, path_seperator: File::SEPARATOR, handler_extensions: self.class.handler_extensions)
  @path = path.to_s
  @path_seperator = Regexp.new(path_seperator)
  @handler_extensions = handler_extensions
  parse
end

Public Instance Methods

==(path) click to toggle source
# File lib/sitepress/path.rb, line 47
def ==(path)
  to_s == path.to_s
end
exists?() click to toggle source
# File lib/sitepress/path.rb, line 51
def exists?
  File.exists? path
end
expand_path() click to toggle source
# File lib/sitepress/path.rb, line 55
def expand_path
  File.expand_path path
end
node_names() click to toggle source
# File lib/sitepress/path.rb, line 36
def node_names
  @node_names ||= node_name_ancestors.push(node_name)
end
to_s()
Alias for: to_str
to_str() click to toggle source

Necessary for operations like `File.read path` where `path` is an instance of this object.

# File lib/sitepress/path.rb, line 42
def to_str
  @path
end
Also aliased as: to_s

Private Instance Methods

handler_is_format?() click to toggle source

Rails has handlers, like `:html` and `:raw` that are both handlers and formats. If we don't account for this, then the object would return a `nil` for a file named `blah.html`.

# File lib/sitepress/path.rb, line 70
def handler_is_format?
  return false if @handler.nil?
  @format.nil? and MIME::Types.type_for(@handler.to_s).any?
end
node_name_ancestors() click to toggle source

If given a path `/a/b/c`, thsi would return `[“a”, “b”, “c”].

# File lib/sitepress/path.rb, line 109
def node_name_ancestors
  strip_leading_prefix(File.dirname(path)).split(@path_seperator)
end
parse() click to toggle source
# File lib/sitepress/path.rb, line 75
def parse
  @dirname, @basename = File.split(path)
  parse_basename
end
parse_basename() click to toggle source

Given a filename, this will work out the extensions, formats, and node_name.

# File lib/sitepress/path.rb, line 81
def parse_basename
  base = basename
  filename, extname = split_filename(base)

  # This is a root path, so we have to treat it a little differently
  # so that the node mapper and node names work properly.
  if filename == ROOT_PATH and extname.nil?
    @node_name = ROOT_NODE_NAME
  elsif extname
    extname = extname.to_sym

    # We have an extension! Let's figure out if its a handler or not.
    if @handler_extensions.include? extname
      # Yup, its a handler. Set those variables accordingly.
      @handler = extname
      base = filename
    end

    # Now let's get the format (e.g. :html, :xml, :json) for the path and
    # the key, which is just the basename without the format extension.
    @node_name, format = split_filename(base)
    @format = format
  else
    @node_name = filename
  end
end
split_filename(string) click to toggle source

Make it easier to split the last extension off a filename. For example, if you run `split_filename(“c.taco.html”)` it would return `[“c.taco”, “html”]`. If you ran it against something like `split_filename(“c”)`, it would return `[“c”]`

# File lib/sitepress/path.rb, line 117
def split_filename(string)
  base, _, extension = string.rpartition(".")
  base.empty? ? [extension] : [base, extension]
end
strip_leading_prefix(dirname) click to toggle source

Strips leading `/` or leading `.` if the path is relative.

# File lib/sitepress/path.rb, line 123
def strip_leading_prefix(dirname)
  dirname.to_s.gsub(/^#{@path_seperator}|\./, "")
end