directory_listing 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/directory_listing.rb +70 -86
- metadata +1 -1
data/lib/directory_listing.rb
CHANGED
@@ -1,29 +1,30 @@
|
|
1
1
|
require 'truncate'
|
2
2
|
require 'filesize'
|
3
|
+
require 'pathname'
|
3
4
|
|
4
5
|
# = Easy Apache-style directory listings for Sinatra.
|
5
6
|
#
|
6
7
|
# == Usage
|
7
8
|
#
|
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
|
9
|
+
# Directory_listing.list will return HTML, so the following is a complete Sinatra
|
10
|
+
# app that will provide a directory listing of whatever path you navigate to
|
11
|
+
# and let you view any file that is served directly:
|
10
12
|
#
|
11
13
|
# require 'directory_listing
|
12
14
|
#
|
13
15
|
# get '*' do |path|
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# end
|
16
|
+
# if File.exist?(File.join(settings.public_folder, path))
|
17
|
+
# if File.directory?(File.join(settings.public_folder, path))
|
18
|
+
# "#{Directory_listing.list(
|
19
|
+
# :directory => path,
|
20
|
+
# :sinatra_public => settings.public_folder,
|
21
|
+
# )}"
|
22
|
+
# else
|
23
|
+
# send_file File.join(settings.public_folder, path)
|
24
|
+
# end
|
25
|
+
# else
|
26
|
+
# not_found
|
27
|
+
# end
|
27
28
|
# end
|
28
29
|
#
|
29
30
|
# not_found do
|
@@ -39,9 +40,31 @@ require 'filesize'
|
|
39
40
|
# stylesheet # pass a stylesheet to style the page with
|
40
41
|
# should_list_invisibles # should the directory listing include invisibles (dotfiles) - "yes" or "no"
|
41
42
|
# 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"
|
42
|
-
# dir_html_style # html style for directories - "bold", "italic", "underline", or "none" - defaults to "bold"
|
43
|
-
# regfile_html_style # html style for regular files - "bold", "italic", "underline", or "none" - defaults to "none"
|
44
43
|
# filename_truncate_length # (integer) length to truncate file names to - defaults to 40
|
44
|
+
#
|
45
|
+
# == Styling
|
46
|
+
#
|
47
|
+
# It's pretty easy to figure out how to style directory_listing by looking at the source.
|
48
|
+
#
|
49
|
+
# Some gotchas:
|
50
|
+
#
|
51
|
+
# 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".
|
52
|
+
#
|
53
|
+
# You can style the "File" column with this CSS:
|
54
|
+
#
|
55
|
+
# table tr > td:first-child {
|
56
|
+
# text-align: left;
|
57
|
+
# }
|
58
|
+
#
|
59
|
+
# Second column:
|
60
|
+
# table tr > td:first-child + td {
|
61
|
+
# text-align: left;
|
62
|
+
# }
|
63
|
+
#
|
64
|
+
# Third column:
|
65
|
+
# table tr > td:first-child + td + td {
|
66
|
+
# text-align: left;
|
67
|
+
# }
|
45
68
|
|
46
69
|
module Directory_listing
|
47
70
|
@@options = {}
|
@@ -57,8 +80,9 @@ module Directory_listing
|
|
57
80
|
def self.list(options)
|
58
81
|
options = @@options.merge options
|
59
82
|
raise(ArgumentError, ":directory is required") unless options[:directory]
|
60
|
-
|
61
|
-
|
83
|
+
raise(ArgumentError, ":sinatra_public is required") unless options[:sinatra_public]
|
84
|
+
pub = options[:sinatra_public]
|
85
|
+
dir = File.join(pub, options[:directory])
|
62
86
|
|
63
87
|
if options[:should_list_invisibles]
|
64
88
|
$should_list_invisibles = options[:should_list_invisibles]
|
@@ -70,111 +94,71 @@ module Directory_listing
|
|
70
94
|
else
|
71
95
|
$last_modified_format = "%Y-%m-%d %H:%M:%S"
|
72
96
|
end
|
73
|
-
if options[:dir_html_style]
|
74
|
-
$dir_html_style = options[:dir_html_style]
|
75
|
-
else
|
76
|
-
$dir_html_style = "bold"
|
77
|
-
end
|
78
|
-
if options[:regfile_html_style]
|
79
|
-
$regfile_html_style = options[:regfile_html_style]
|
80
|
-
else
|
81
|
-
$regfile_html_style = "none"
|
82
|
-
end
|
83
97
|
if options[:filename_truncate_length]
|
84
98
|
$filename_truncate_length = options[:filename_truncate_length]
|
85
99
|
else
|
86
100
|
$filename_truncate_length = 40
|
87
101
|
end
|
88
102
|
|
89
|
-
html = "<html>\n<head
|
103
|
+
html = "<html>\n<head>\n"
|
90
104
|
if options[:stylesheet]
|
91
|
-
html << "<link rel=\"stylesheet\" type=\"text/css\" href=\"/#{options[:stylesheet].sub(/^[\/]*/,"")}\"
|
105
|
+
html << "<link rel=\"stylesheet\" type=\"text/css\" href=\"/#{options[:stylesheet].sub(/^[\/]*/,"")}\">\n"
|
92
106
|
end
|
93
|
-
html << "</head>\n<body
|
94
|
-
html << "<h1>Index of #{options[:directory]}</h1
|
95
|
-
html << "<table
|
96
|
-
html << "<tr>\n<th>File</th>\n<th>Last modified</th>\n<th>Size</th>\n</tr>"
|
107
|
+
html << "</head>\n<body>\n"
|
108
|
+
html << "<h1>Index of #{options[:directory]}</h1>\n"
|
109
|
+
html << "<table>\n"
|
110
|
+
html << "\t<tr>\n\t\t<th>File</th>\n\t\t<th>Last modified</th>\n\t\t<th>Size</th>\n\t</tr>"
|
97
111
|
files = Array.new
|
98
112
|
Dir.foreach(dir, &files.method(:push))
|
99
113
|
files.sort.each do |file|
|
100
|
-
html << wrap(file, dir)
|
114
|
+
html << wrap(file, dir, pub)
|
101
115
|
end
|
102
|
-
html << "</table
|
103
|
-
|
116
|
+
html << "\n</table>\n"
|
117
|
+
html << "</body>\n</html>\n"
|
104
118
|
"#{html}"
|
105
119
|
end
|
106
120
|
|
107
121
|
private
|
108
122
|
|
109
123
|
def self.m_time(file, dir)
|
110
|
-
time = "
|
124
|
+
time = "\t<td>#{File.mtime(File.join(dir, file)).strftime $last_modified_format}</td>"
|
111
125
|
end
|
112
126
|
|
113
127
|
def self.size(file, dir)
|
114
128
|
if File.directory?(File.join(dir, file))
|
115
|
-
"
|
129
|
+
"\t<td>-</td>"
|
116
130
|
else
|
117
131
|
size = Filesize.from("#{File.stat(File.join(dir, file)).size} B").pretty
|
118
|
-
"
|
132
|
+
"\t<td>#{size}</td>"
|
119
133
|
end
|
120
134
|
end
|
121
135
|
|
122
|
-
def self.name(file, dir)
|
123
|
-
html = pre_dir = post_dir = pre_reg = post_reg = ""
|
136
|
+
def self.name(file, dir, pub)
|
124
137
|
tfile = file.truncate($filename_truncate_length, '...')
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
pre_dir = "<b>"
|
129
|
-
post_dir = "</b>"
|
130
|
-
when "italic"
|
131
|
-
pre_dir = "<i>"
|
132
|
-
post_dir = "</i>"
|
133
|
-
when "underline"
|
134
|
-
pre_dir = "<u>"
|
135
|
-
post_dir = "</u>"
|
136
|
-
else
|
137
|
-
pre_dir = "<b>"
|
138
|
-
post_dir = "</b>"
|
139
|
-
end
|
140
|
-
case $regfile_html_style
|
141
|
-
when "bold"
|
142
|
-
pre_reg = "<b>"
|
143
|
-
post_reg = "</b>"
|
144
|
-
when "italic"
|
145
|
-
pre_reg = "<i>"
|
146
|
-
post_reg = "</i>"
|
147
|
-
when "underline"
|
148
|
-
pre_reg = "<u>"
|
149
|
-
post_reg = "</u>"
|
150
|
-
else
|
151
|
-
pre_reg = ""
|
152
|
-
post_reg = ""
|
153
|
-
end
|
154
|
-
|
138
|
+
link = Pathname.new(File.join(dir, file)).relative_path_from(Pathname.new(pub))
|
139
|
+
|
140
|
+
html = ""
|
155
141
|
if File.directory?(File.join(dir, file))
|
156
|
-
html << "<a href='#{
|
142
|
+
html << "\t<td class='dir'><a href='#{link}'>#{tfile}</a></td>"
|
157
143
|
else
|
158
|
-
html << "#{
|
144
|
+
html << "\t<td class='file'><a href='#{link}'>#{tfile}</td>"
|
159
145
|
end
|
160
146
|
"#{html}"
|
161
147
|
end
|
162
148
|
|
163
|
-
def self.wrap(file, dir)
|
149
|
+
def self.wrap(file, dir, pub)
|
164
150
|
wrapped = ""
|
165
151
|
if $should_list_invisibles == "yes"
|
166
|
-
wrapped << "<tr>
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
</tr>"
|
152
|
+
wrapped << "\n\t<tr>
|
153
|
+
#{name(file, dir, pub)}
|
154
|
+
#{m_time(file, dir)}
|
155
|
+
#{size(file, dir)}\n\t</tr>"
|
171
156
|
else
|
172
157
|
if file[0] != "."
|
173
|
-
wrapped << "<tr>
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
</tr>"
|
158
|
+
wrapped << "\n\t<tr>
|
159
|
+
#{name(file, dir, pub)}
|
160
|
+
#{m_time(file, dir)}
|
161
|
+
#{size(file, dir)}\n\t</tr>"
|
178
162
|
end
|
179
163
|
end
|
180
164
|
"#{wrapped}"
|