directory_listing: easy, CSS-styled, Apache-like directory listings for Sinatra.
Description
directory_listing is a Sinatra plugin that generates Apache-like directory listings. It was designed to be:
- Easy to use and configure
- Style-able with CSS
- Able to replicate all the features of Apache's directory listings including sorting
directory_listing also includes a number of configuration options - see the Options section below.
A short blog post / announcement exists here.
Install
For regular use:
(sudo) gem install directory_listing
Or from source:
bundle install
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:
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 of the generated directory listingreadme- an HTML string that will be appended at the footer of the generated directory listingshould_list_invisibles- whether the directory listing should include invisibles (dotfiles) - true or false, defaults to falselast_modified_format- format for last modified date - defaults to%Y-%m-%d %H:%M:%Sfilename_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 ofdirand regular files will have a class offile. - You can style the "File" column with this CSS:
table tr > td:first-child {
text-align: left;
}
- "Last modified" column:
table tr > td:first-child + td {
text-align: left;
}
- "Size" column:
table tr > td:first-child + td + td {
text-align: left;
}
- The navigation bar is in a div called 'nav' and consists of hrefs embedded in h1s:
div.nav h1, div.nav a {
font-size: 16px;
font-weight: 600;
}
Getting Help
The best way to report a bug or feature request is to open an issue on GitHub.
Additionally, I'd love to hear your feedback about directory_listing through Twitter or email.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
License
directory_listing is licensed under the DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE. See the LICENSE file for details.