Module: Sinatra

Defined in:
lib/sinatra/directory_listing.rb

Overview

Easy, CSS-styled, Apache-like directory listings for Sinatra.

Usage

list() will return HTML, so the following is a complete Sinatra app that will provide a directory listing of whatever path you navigate to and let you view any file that is served directly:

require ‘sinatra’ require ‘sinatra/directory_listing’

get ‘*’ do |path|

if File.exist?(File.join(settings.public_folder, path))
  if File.directory?(File.join(settings.public_folder, path))
    list()
  else
    send_file File.join(settings.public_folder, path)
  end
else
  not_found
end

end

not_found do

'Try again.'

end

Options

Options are passed in a hash:

list(

:stylesheet => "stylesheets/styles.css",
:readme => "<a>Welcome!</a>"

)

Available options:

stylesheet # a stylesheet that will be added to the <head> of the generated directory listing readme # an HTML string that will be appended at the footer of the generated directory listing should_list_invisibles # whether the directory listing should include invisibles (dotfiles) - “yes” or “no” last_modified_format # format for last modified date (www.ruby-doc.org/core-2.0/Time.html) - defaults to “%Y-%m-%d %H:%M:%S” filename_truncate_length # (integer) length to truncate file names to - defaults to 40

Styling

It’s pretty easy to figure out how to style directory_listing by looking at the source, but here are some gotchas:

Every item listed is a <td> element in a table. Directories will have a class of “dir” and regular files will have a class of “file”.

You can style the “File” column with this CSS:

table tr > td:first-child

text-align: left;

Second column: table tr > td:first-child + td

text-align: left;

Third column: table tr > td:first-child + td + td

text-align: left;

Defined Under Namespace

Modules: Directory_listing