Module: Sinatra

Defined in:
lib/sinatra/directory_listing.rb,
lib/sinatra/directory_listing/layout.rb

Overview

### directory_listing: easy, CSS-styled, Apache-like directory listings for Sinatra.

### Install:

For regular use:

“‘bash (sudo) gem install directory_listing “`

Or from source:

“‘bash rake install “`

### 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:

“‘ruby 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:

“‘ruby 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) - true or false, defaults to false

  • “‘last_modified_format“` - [format](www.ruby-doc.org/core-2.0/Time.html) for last modified date - 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:

“‘css table tr > td:first-child

text-align: left;

“‘

  • “Last modified” column:

“‘css table tr > td:first-child + td

text-align: left;

“‘

  • “Size” column:

“‘css table tr > td:first-child + td + td

text-align: left;

“‘

Defined Under Namespace

Modules: Directory_listing