directory_listing 0.2.0 → 0.2.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sinatra/directory_listing.rb +89 -42
  3. metadata +10 -13
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d00948424b8f2e0c6d75a2285b1b3e6805ef790
4
+ data.tar.gz: f23432bf5b3a41207e46aa0b0027bd7168caa049
5
+ SHA512:
6
+ metadata.gz: bd32a90508a9425edede1ee5ac86d430961300e72f5f122f045b492c322b559bd1bdab41a1dded0ebbe49368f88c3cd5de6a7619c58f0149e8b6edab980d223d
7
+ data.tar.gz: c3271f6bb076d059ff678287305c31cb2d0662903628f03ce4e6a0cc6ea5423f268dcc1a033b7455ef43b5915b5d73794a453b69cec97a46f5628447739deca0
@@ -3,6 +3,7 @@ require 'truncate'
3
3
  require 'filesize'
4
4
  require 'pathname'
5
5
  require 'uri'
6
+ require 'erb'
6
7
 
7
8
  # = Easy, CSS-styled, Apache-like directory listings for Sinatra.
8
9
  #
@@ -73,54 +74,47 @@ require 'uri'
73
74
  module Sinatra
74
75
  module Directory_listing
75
76
 
76
- def list(o={})
77
- options = {
78
- :should_list_invisibles => false,
79
- :last_modified_format => "%Y-%m-%d %H:%M:%S",
80
- :filename_truncate_length => 40,
81
- :stylesheet => "",
82
- :readme => ""
83
- }.merge(o)
84
-
85
- $should_list_invisibles = options[:should_list_invisibles]
86
- $last_modified_format = options[:last_modified_format]
87
- $filename_truncate_length = options[:filename_truncate_length]
88
-
89
- html = "<html>\n<head>\n"
90
- html << "<title>Index of #{URI.unescape(request.path)}</title>\n"
91
- html << "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>\n"
92
- if options[:stylesheet]
93
- html << "<link rel=\"stylesheet\" type=\"text/css\" href=\"/#{options[:stylesheet].sub(/^[\/]*/,"")}\">\n"
94
- end
95
- html << "</head>\n<body>\n"
96
- html << "<h1>Index of #{URI.unescape(request.path)}</h1>\n"
97
- if URI.unescape(request.path) != "/"
98
- html << "<a href='#{Pathname.new(URI.unescape(request.path)).parent}'>&larr; Parent directory</a><br><br>"
99
- else
100
- html << "<a>Root directory</a><br><br>"
101
- end
102
- html << "<table>\n"
103
- 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>\n"
104
- files = Array.new
105
- Dir.foreach(File.join(settings.public_folder, URI.unescape(request.path)), &files.method(:push))
106
- if files == [".", ".."]
107
- html << "\t<tr>\n\t\t<th>No files.</th>\n\t\t<th>-</th>\n\t\t<th>-</th>\n\t</tr>"
108
- else
109
- files.sort.each do |file|
110
- html << self.wrap(file)
111
- end
112
- end
113
- html << "\n</table>\n"
114
- html << "<br>\n#{options[:readme]}\n" if options[:readme]
115
- html << "</body>\n</html>\n"
116
- html
77
+ ##
78
+ # erb template for page
79
+
80
+ def template
81
+ "<html>
82
+ <head>
83
+ <title>Index of <%= $current_page %></title>
84
+ <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
85
+ <%= $stylesheet %>
86
+ </head>
87
+ <body>
88
+ <h1>Index of <%= $current_page %></h1>
89
+ <%= $back_to_link %>
90
+ <br><br>
91
+
92
+ <table>
93
+ <tr>
94
+ <th>File</th>
95
+ <th>Last modified</th>
96
+ <th>Size</th>
97
+ </tr>
98
+ <%= $files_html %>
99
+ </table>
100
+
101
+ <br>
102
+ <a><%= $readme if $readme %></a>
103
+ </body>
104
+ </html>"
117
105
  end
118
106
 
107
+ ##
108
+ # Get the mtime for a file.
109
+
119
110
  def m_time(file)
120
111
  f = File.join(File.join(settings.public_folder, URI.unescape(request.fullpath)), file)
121
112
  "\t<td>#{File.mtime(f).strftime $last_modified_format}</td>"
122
113
  end
123
114
 
115
+ ##
116
+ # Get the size for a file.
117
+
124
118
  def size(file)
125
119
  f = File.join(File.join(settings.public_folder, URI.unescape(request.fullpath)), file)
126
120
  if File.directory?(f)
@@ -131,6 +125,9 @@ module Sinatra
131
125
  end
132
126
  end
133
127
 
128
+ ##
129
+ # Get the name of a file.
130
+
134
131
  def name(file)
135
132
  file = URI.unescape(file)
136
133
  tfile = file.truncate($filename_truncate_length, '...')
@@ -149,6 +146,9 @@ module Sinatra
149
146
  html
150
147
  end
151
148
 
149
+ ##
150
+ # Generate a single row of data for a file.
151
+
152
152
  def wrap(file)
153
153
  html = ""
154
154
  if $should_list_invisibles == true
@@ -166,8 +166,55 @@ module Sinatra
166
166
  end
167
167
  html
168
168
  end
169
+
170
+ ##
171
+ # Generate the page.
172
+
173
+ def list(o={})
174
+ options = {
175
+ :should_list_invisibles => false,
176
+ :last_modified_format => "%Y-%m-%d %H:%M:%S",
177
+ :filename_truncate_length => 40,
178
+ :stylesheet => "",
179
+ :readme => ""
180
+ }.merge(o)
181
+
182
+ $should_list_invisibles = options[:should_list_invisibles]
183
+ $last_modified_format = options[:last_modified_format]
184
+ $filename_truncate_length = options[:filename_truncate_length]
185
+
186
+ ##
187
+ # Start generating strings to be injected into the erb template
188
+
189
+ $current_page = URI.unescape(request.path)
190
+ $readme = options[:readme] if options[:readme]
191
+ $stylesheet = "<link rel='stylesheet' type='text/css' href='/#{options[:stylesheet].sub(/^[\/]*/,"")}'>" if options[:stylesheet]
192
+
193
+ if URI.unescape(request.path) != "/"
194
+ $back_to_link = "<a href='#{Pathname.new(URI.unescape(request.path)).parent}'>&larr; Parent directory</a>"
195
+ else
196
+ $back_to_link = "<a>Root directory</a>"
197
+ end
198
+
199
+ $files_html = ""
200
+ files = Array.new
201
+ Dir.foreach(File.join(settings.public_folder, URI.unescape(request.path)), &files.method(:push))
202
+ if files == [".", ".."]
203
+ $files_html << "\t<tr>\n\t\t<th>No files.</th>\n\t\t<th>-</th>\n\t\t<th>-</th>\n\t</tr>"
204
+ else
205
+ files.sort.each do |file|
206
+ $files_html << self.wrap(file)
207
+ end
208
+ end
209
+
210
+ ##
211
+ # Generate and return the complete page from the erb template.
212
+
213
+ erb = ERB.new(template)
214
+ erb.result
215
+ end
169
216
 
170
217
  end
171
218
 
172
219
  helpers Directory_listing
173
- end
220
+ end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: directory_listing
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.2.0
4
+ version: 0.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Richard Myers
@@ -10,13 +9,12 @@ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
11
 
13
- date: 2013-07-31 00:00:00 Z
12
+ date: 2013-08-21 00:00:00 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: filesize
17
16
  prerelease: false
18
17
  requirement: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
19
  - - ">="
22
20
  - !ruby/object:Gem::Version
@@ -27,7 +25,6 @@ dependencies:
27
25
  name: truncate
28
26
  prerelease: false
29
27
  requirement: &id002 !ruby/object:Gem::Requirement
30
- none: false
31
28
  requirements:
32
29
  - - ">="
33
30
  - !ruby/object:Gem::Version
@@ -47,29 +44,29 @@ files:
47
44
  homepage: https://rubygems.org/gems/directory_listing
48
45
  licenses:
49
46
  - WTFPL
47
+ metadata: {}
48
+
50
49
  post_install_message:
51
50
  rdoc_options: []
52
51
 
53
52
  require_paths:
54
53
  - lib
55
54
  required_ruby_version: !ruby/object:Gem::Requirement
56
- none: false
57
55
  requirements:
58
- - - ">="
56
+ - &id003
57
+ - ">="
59
58
  - !ruby/object:Gem::Version
60
59
  version: "0"
61
60
  required_rubygems_version: !ruby/object:Gem::Requirement
62
- none: false
63
61
  requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- version: "0"
62
+ - *id003
67
63
  requirements: []
68
64
 
69
65
  rubyforge_project:
70
- rubygems_version: 1.8.23
66
+ rubygems_version: 2.0.7
71
67
  signing_key:
72
- specification_version: 3
68
+ specification_version: 4
73
69
  summary: Easy, CSS-styled, Apache-like directory listings for Sinatra.
74
70
  test_files: []
75
71
 
72
+ has_rdoc: