directory_listing 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/directory_listing.rb +177 -0
- metadata +75 -0
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'truncate'
|
2
|
+
require 'filesize'
|
3
|
+
|
4
|
+
# = Easy Apache-style directory listings for Sinatra.
|
5
|
+
#
|
6
|
+
# == Usage
|
7
|
+
#
|
8
|
+
# Directory_listing will return HTML, so the following is a complete Sinatra
|
9
|
+
# app that will provide a directory listing of whatever path you navigate to:
|
10
|
+
#
|
11
|
+
# require 'directory_listing
|
12
|
+
#
|
13
|
+
# get '*' do |path|
|
14
|
+
# if File.exist?(File.join(settings.public_folder, path))
|
15
|
+
# "#{Directory_listing.list(
|
16
|
+
# :directory => path,
|
17
|
+
# :sinatra_public => settings.public_folder,
|
18
|
+
# :should_list_invisibles => "no",
|
19
|
+
# :last_modified_format => "%Y-%m-%d %H:%M:%S",
|
20
|
+
# :dir_html_style => "bold",
|
21
|
+
# :regfile_html_style => "none",
|
22
|
+
# :filename_truncate_length => 40)}"
|
23
|
+
# else
|
24
|
+
# not_found
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# not_found do
|
29
|
+
# 'Try again.'
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# Any option key may be omitted except for :directory and :sinatra_public. Explanations of options are below.
|
33
|
+
#
|
34
|
+
# == Options
|
35
|
+
#
|
36
|
+
# directory # the directory to list
|
37
|
+
# sinatra_public # sinatra's public folder - your public folder (and the default) is likely "settings.public_folder"
|
38
|
+
# should_list_invisibles # should the directory listing include invisibles (dotfiles) - "yes" or "no"
|
39
|
+
# last_modified_format # format for last modified date (http://www.ruby-doc.org/core-2.0/Time.html) - defaults to "%Y-%m-%d %H:%M:%S"
|
40
|
+
# dir_html_style # html style for directories - "bold", "italic", "underline", or "none" - defaults to "bold"
|
41
|
+
# regfile_html_style # html style for regular files - "bold", "italic", "underline", or "none" - defaults to "none"
|
42
|
+
# filename_truncate_length # (integer) length to truncate file names to - defaults to 40
|
43
|
+
|
44
|
+
module Directory_listing
|
45
|
+
@@options = {}
|
46
|
+
|
47
|
+
def self.options=(value)
|
48
|
+
@@options = value
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.options()
|
52
|
+
@@options
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.list(options)
|
56
|
+
options = @@options.merge options
|
57
|
+
raise(ArgumentError, ":directory is required") unless options[:directory]
|
58
|
+
raise(ArgumentError, ":sinatra_public is required") unless options[:sinatra_public]
|
59
|
+
dir = File.join(options[:sinatra_public], options[:directory])
|
60
|
+
|
61
|
+
if options[:should_list_invisibles]
|
62
|
+
$should_list_invisibles = options[:should_list_invisibles]
|
63
|
+
else
|
64
|
+
$should_list_invisibles = "no"
|
65
|
+
end
|
66
|
+
if options[:last_modified_format]
|
67
|
+
$last_modified_format = options[:last_modified_format]
|
68
|
+
else
|
69
|
+
$last_modified_format = "%Y-%m-%d %H:%M:%S"
|
70
|
+
end
|
71
|
+
if options[:dir_html_style]
|
72
|
+
$dir_html_style = options[:dir_html_style]
|
73
|
+
else
|
74
|
+
$dir_html_style = "bold"
|
75
|
+
end
|
76
|
+
if options[:regfile_html_style]
|
77
|
+
$regfile_html_style = options[:regfile_html_style]
|
78
|
+
else
|
79
|
+
$regfile_html_style = "none"
|
80
|
+
end
|
81
|
+
if options[:filename_truncate_length]
|
82
|
+
$filename_truncate_length = options[:filename_truncate_length]
|
83
|
+
else
|
84
|
+
$filename_truncate_length = 40
|
85
|
+
end
|
86
|
+
|
87
|
+
html = "<html>\n<body>"
|
88
|
+
html << "<h1>Index of #{options[:directory]}</h1>"
|
89
|
+
html << "<table>"
|
90
|
+
html << "<tr>\n<th>File</th>\n<th>Last modified</th>\n<th>Size</th>\n</tr>"
|
91
|
+
files = Array.new
|
92
|
+
Dir.foreach(dir, &files.method(:push))
|
93
|
+
files.sort.each do |file|
|
94
|
+
html << wrap(file, dir)
|
95
|
+
end
|
96
|
+
html << "</table>"
|
97
|
+
html << "</html>\n</body>"
|
98
|
+
"#{html}"
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def self.m_time(file, dir)
|
104
|
+
time = "#{File.mtime(File.join(dir, file)).strftime $last_modified_format}"
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.size(file, dir)
|
108
|
+
if File.directory?(File.join(dir, file))
|
109
|
+
"-"
|
110
|
+
else
|
111
|
+
size = Filesize.from("#{File.stat(File.join(dir, file)).size} B").pretty
|
112
|
+
"#{size}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.name(file, dir)
|
117
|
+
html = pre_dir = post_dir = pre_reg = post_reg = ""
|
118
|
+
tfile = file.truncate($filename_truncate_length, '...')
|
119
|
+
|
120
|
+
case $dir_html_style
|
121
|
+
when "bold"
|
122
|
+
pre_dir = "<b>"
|
123
|
+
post_dir = "</b>"
|
124
|
+
when "italic"
|
125
|
+
pre_dir = "<i>"
|
126
|
+
post_dir = "</i>"
|
127
|
+
when "underline"
|
128
|
+
pre_dir = "<u>"
|
129
|
+
post_dir = "</u>"
|
130
|
+
else
|
131
|
+
pre_dir = "<b>"
|
132
|
+
post_dir = "</b>"
|
133
|
+
end
|
134
|
+
case $regfile_html_style
|
135
|
+
when "bold"
|
136
|
+
pre_reg = "<b>"
|
137
|
+
post_reg = "</b>"
|
138
|
+
when "italic"
|
139
|
+
pre_reg = "<i>"
|
140
|
+
post_reg = "</i>"
|
141
|
+
when "underline"
|
142
|
+
pre_reg = "<u>"
|
143
|
+
post_reg = "</u>"
|
144
|
+
else
|
145
|
+
pre_reg = ""
|
146
|
+
post_reg = ""
|
147
|
+
end
|
148
|
+
|
149
|
+
if File.directory?(File.join(dir, file))
|
150
|
+
html << "<a href='#{file}'>#{pre_dir}#{tfile}#{post_dir}</a>"
|
151
|
+
else
|
152
|
+
html << "#{pre_reg}#{tfile}#{post_reg}"
|
153
|
+
end
|
154
|
+
"#{html}"
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.wrap(file, dir)
|
158
|
+
wrapped = ""
|
159
|
+
if $should_list_invisibles == "yes"
|
160
|
+
wrapped << "<tr>
|
161
|
+
<td>#{name(file, dir)}</td>
|
162
|
+
<td>#{m_time(file, dir)}</td>
|
163
|
+
<td>#{size(file, dir)}</td>
|
164
|
+
</tr>"
|
165
|
+
else
|
166
|
+
if file[0] != "."
|
167
|
+
wrapped << "<tr>
|
168
|
+
<td>#{name(file, dir)}</td>
|
169
|
+
<td>#{m_time(file, dir)}</td>
|
170
|
+
<td>#{size(file, dir)}</td>
|
171
|
+
</tr>"
|
172
|
+
end
|
173
|
+
end
|
174
|
+
"#{wrapped}"
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: directory_listing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Richard Myers
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2013-06-24 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: filesize
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.2
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: truncate
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 0.0.4
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
description: A gem to use with Sinatra for generating Apache-style directory listings.
|
38
|
+
email: rick.myers@me.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/directory_listing.rb
|
47
|
+
homepage: https://rubygems.org/gems/directory_listing
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.23
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Easy Apache-style directory listings for Sinatra.
|
74
|
+
test_files: []
|
75
|
+
|