directory_listing 0.3.4 → 0.4

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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- directory_listing (0.3.2)
4
+ directory_listing (0.3.4)
5
5
  filesize (>= 0.0.2)
6
6
  truncate (>= 0.0.4)
7
7
 
data/Rakefile CHANGED
@@ -5,6 +5,7 @@ require 'yard'
5
5
  ##
6
6
  # Run the test application
7
7
 
8
+ desc "Run the test application on port 4567"
8
9
  task :run do
9
10
  `ruby test/test_directory_listing_app.rb -o 0.0.0.0`
10
11
  end
@@ -22,3 +23,8 @@ end
22
23
  Rake::TestTask.new do |t|
23
24
  t.libs << 'test'
24
25
  end
26
+
27
+ ##
28
+ # Default -> run tests
29
+
30
+ task :default => 'test'
@@ -9,9 +9,7 @@ module Sinatra
9
9
  <%= page.stylesheet %>
10
10
  </head>
11
11
  <body>
12
- <h1>Index of <%= page.current_page %></h1>
13
- <%= page.back_to_link %>
14
- <br><br>
12
+ <h1><%= page.back_to_link %></h1>
15
13
 
16
14
  <table>
17
15
  <tr>
@@ -1,5 +1,8 @@
1
1
  class Page
2
2
 
3
+ ##
4
+ # Class definition for the page to be generated.
5
+
3
6
  attr_accessor :should_list_invisibles,
4
7
  :last_modified_format,
5
8
  :filename_truncate_length,
@@ -18,5 +21,62 @@ class Page
18
21
  :file_sort_link,
19
22
  :mtime_sort_link,
20
23
  :size_sort_link
21
-
22
- end
24
+
25
+ ##
26
+ # Return new parameters for another location with the
27
+ # same sorting parameters as the passed Page object
28
+
29
+ def sorted_url(page)
30
+ params = ""
31
+ if page.request_params["sortby"] && page.request_params["direction"]
32
+ params = "?sortby=" + page.request_params["sortby"] + "&direction=" + page.request_params["direction"]
33
+ end
34
+ end
35
+
36
+ def sorting_info(s_item, s_direction)
37
+
38
+ file_link_dir = mtime_link_dir = sortby_link_dir = "ascending"
39
+ s_item_display = s_direction_display = ""
40
+
41
+ case s_item
42
+ when "file"
43
+ s_item_display = "alphabetically"
44
+ case s_direction
45
+ when "ascending"
46
+ s_direction_display = ""
47
+ file_link_dir = "descending"
48
+ when "descending"
49
+ s_direction_display = "reversed"
50
+ file_link_dir = "ascending"
51
+ end
52
+ when "mtime"
53
+ s_item_display = "by modification date"
54
+ case s_direction
55
+ when "ascending"
56
+ s_direction_display = "oldest to newest"
57
+ mtime_link_dir = "descending"
58
+ when "descending"
59
+ s_direction_display = "newest to oldest"
60
+ mtime_link_dir = "ascending"
61
+ end
62
+ when "size"
63
+ s_item_display = "by size"
64
+ case s_direction
65
+ when "ascending"
66
+ s_direction_display = "smallest to largest"
67
+ sortby_link_dir = "descending"
68
+ when "descending"
69
+ s_direction_display = "largest to smallest"
70
+ sortby_link_dir = "ascending"
71
+ end
72
+ end
73
+
74
+ return "?sortby=file&direction=#{file_link_dir}",
75
+ "?sortby=mtime&direction=#{mtime_link_dir}",
76
+ "?sortby=size&direction=#{sortby_link_dir}",
77
+ s_item_display,
78
+ s_direction_display
79
+
80
+ end
81
+
82
+ end
@@ -1,3 +1,3 @@
1
1
  module Directory_listing
2
- VERSION = '0.3.4'
2
+ VERSION = '0.4'
3
3
  end
@@ -5,95 +5,6 @@ require 'pathname'
5
5
  require 'uri'
6
6
  require 'erb'
7
7
 
8
- # ### directory_listing: easy, CSS-styled, Apache-like directory listings for Sinatra.
9
- #
10
- # ### Install:
11
- #
12
- # For regular use:
13
- #
14
- # ```bash
15
- # (sudo) gem install directory_listing
16
- # ```
17
- #
18
- # Or from source:
19
- #
20
- # ```bash
21
- # rake install
22
- # ```
23
- #
24
- # ### Usage:
25
- #
26
- # ```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:
27
- #
28
- # ```ruby
29
- # require 'sinatra'
30
- # require 'sinatra/directory_listing'
31
- #
32
- # get '*' do |path|
33
- # if File.exist?(File.join(settings.public_folder, path))
34
- # if File.directory?(File.join(settings.public_folder, path))
35
- # list()
36
- # else
37
- # send_file File.join(settings.public_folder, path)
38
- # end
39
- # else
40
- # not_found
41
- # end
42
- # end
43
- #
44
- # not_found do
45
- # 'Try again.'
46
- # end
47
- # ```
48
- #
49
- # ### Options:
50
- #
51
- # Options are passed in a hash:
52
- #
53
- # ```ruby
54
- # list({
55
- # :stylesheet => "stylesheets/styles.css",
56
- # :readme => "<a>Welcome!</a>"
57
- # })
58
- # ```
59
- #
60
- # Available options:
61
- #
62
- # - ```stylesheet``` - a stylesheet that will be added to the <head> of the generated directory listing
63
- # - ```readme``` - an HTML string that will be appended at the footer of the generated directory listing
64
- # - ```should_list_invisibles``` - whether the directory listing should include invisibles (dotfiles) - true or false, defaults to false
65
- # - ```last_modified_format``` - [format](http://www.ruby-doc.org/core-2.0/Time.html) for last modified date - defaults to ```%Y-%m-%d %H:%M:%S```
66
- # - ```filename_truncate_length``` - (integer) length to truncate file names to - defaults to 40
67
- #
68
- # ### Styling:
69
- #
70
- # It's pretty easy to figure out how to style ```directory_listing``` by looking at the source, but here are some gotchas:
71
- #
72
- # - 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```.
73
- # - You can style the "File" column with this CSS:
74
- #
75
- # ```css
76
- # table tr > td:first-child {
77
- # text-align: left;
78
- # }
79
- # ```
80
- #
81
- # - "Last modified" column:
82
- #
83
- # ```css
84
- # table tr > td:first-child + td {
85
- # text-align: left;
86
- # }
87
- # ```
88
- #
89
- # - "Size" column:
90
- #
91
- # ```css
92
- # table tr > td:first-child + td + td {
93
- # text-align: left;
94
- # }
95
- # ```
96
-
97
8
  module Sinatra
98
9
  module Directory_listing
99
10
 
@@ -119,18 +30,13 @@ module Sinatra
119
30
  }.merge(o)
120
31
 
121
32
  ##
122
- # Create a page object.
33
+ # Create a page object and start setting attributes.
123
34
 
124
35
  page = Page.new
125
36
 
126
37
  page.should_list_invisibles = options[:should_list_invisibles]
127
38
  page.last_modified_format = options[:last_modified_format]
128
- page.filename_truncate_length = options[:filename_truncate_length]
129
-
130
- ##
131
- # Get the public folder, request path, and parameters and
132
- # store in globals to be used by the Resource class.
133
-
39
+ page.filename_truncate_length = options[:filename_truncate_length]
134
40
  page.public_folder = settings.public_folder
135
41
  page.request_path = request.path
136
42
  page.request_params = request.params
@@ -145,19 +51,36 @@ module Sinatra
145
51
  end
146
52
 
147
53
  ##
148
- # Generate the "back to" link
54
+ # Generate the navigation links
149
55
  # Append the sorting information if the current directory is sorted.
150
56
 
151
- if URI.unescape(request.path) != "/"
152
- back_link = Pathname.new(request.path).parent
153
- if page.request_params["sortby"] && page.request_params["direction"]
154
- back_link = back_link.to_s + "?sortby=" + page.request_params["sortby"] + "&direction=" + page.request_params["direction"]
155
- end
156
- page.back_to_link = "<a href='#{back_link}'>&larr; Parent directory</a>"
57
+ path_array = page.current_page.split("/").drop(1)
58
+ path_count = path_array.count
59
+ params = page.sorted_url(page)
60
+
61
+ if URI.unescape(request.path) == "/"
62
+ page.back_to_link = "Index of /"
157
63
  else
158
- page.back_to_link = "<a>Root directory</a>"
64
+ page.back_to_link = "Index of <a href=\'/#{params}'>/</a>"
159
65
  end
160
-
66
+
67
+ previous_path = ""
68
+ 0.upto(path_array.count - 1) do |a|
69
+ escaped_path = path_array[a].gsub(" ", "%20").gsub("'", "%27")
70
+ escaped_previous = previous_path.gsub(" ", "%20").gsub("'", "%27")
71
+ if a == path_array.count - 1
72
+ href = ""
73
+ else
74
+ href = "<a href=\'/#{escaped_previous}#{escaped_path}#{params}\'>"
75
+ end
76
+ if a == 0
77
+ page.back_to_link << " #{href}#{path_array[a]}</a>"
78
+ else
79
+ page.back_to_link << " / #{href}#{path_array[a]}</a>"
80
+ end
81
+ previous_path << path_array[a] + "/"
82
+ end
83
+
161
84
  ##
162
85
  # Get an array of files to be listed.
163
86
 
@@ -167,7 +90,8 @@ module Sinatra
167
90
  end
168
91
 
169
92
  ##
170
- # If the only thing in the array are invisible files, display a "No files" listing.
93
+ # If the only thing in the array are invisible files,
94
+ # display a "No files" listing.
171
95
 
172
96
  page.files_html = ""
173
97
  if files == [".", ".."]
@@ -188,8 +112,8 @@ module Sinatra
188
112
  end
189
113
 
190
114
  ##
191
- # Get the sortby and direction parameters ("file" and "ascending", by
192
- # default).
115
+ # Get the sortby and direction parameters
116
+ # ("file" and "ascending" by default).
193
117
 
194
118
  page.sort_item = "file"
195
119
  page.sort_direction = "ascending"
@@ -207,45 +131,13 @@ module Sinatra
207
131
  ##
208
132
  # Set display variables and sort links based on sorting variables
209
133
 
210
- file_link_dir = mtime_link_dir = sortby_link_dir = "ascending"
211
-
212
- case page.sort_item
213
- when "file"
214
- page.sort_item_display = "alphabetically"
215
- case page.sort_direction
216
- when "ascending"
217
- page.sort_direction_display = ""
218
- file_link_dir = "descending"
219
- when "descending"
220
- page.sort_direction_display = "reversed"
221
- file_link_dir = "ascending"
222
- end
223
- when "mtime"
224
- page.sort_item_display = "by modification date"
225
- case page.sort_direction
226
- when "ascending"
227
- page.sort_direction_display = "oldest to newest"
228
- mtime_link_dir = "descending"
229
- when "descending"
230
- page.sort_direction_display = "newest to oldest"
231
- mtime_link_dir = "ascending"
232
- end
233
- when "size"
234
- page.sort_item_display = "by size"
235
- case page.sort_direction
236
- when "ascending"
237
- page.sort_direction_display = "smallest to largest"
238
- sortby_link_dir = "descending"
239
- when "descending"
240
- page.sort_direction_display = "largest to smallest"
241
- sortby_link_dir = "ascending"
242
- end
243
- end
244
-
245
- page.file_sort_link = "?sortby=file&direction=#{file_link_dir}"
246
- page.mtime_sort_link = "?sortby=mtime&direction=#{mtime_link_dir}"
247
- page.size_sort_link = "?sortby=size&direction=#{sortby_link_dir}"
248
-
134
+ page.file_sort_link,
135
+ page.mtime_sort_link,
136
+ page.size_sort_link,
137
+ page.sort_item_display,
138
+ page.sort_direction_display =
139
+ page.sorting_info(page.sort_item, page.sort_direction)
140
+
249
141
  ##
250
142
  # Finally, generate the html from the array of Resources.
251
143
 
File without changes
File without changes
File without changes
File without changes
@@ -107,4 +107,12 @@ class DirectoryListingTest < Test::Unit::TestCase
107
107
  files = files_array(last_response.body)
108
108
  assert_equal ["3k.dat", "2k.dat", "1k.dat"], files
109
109
  end
110
+
111
+ ##
112
+ # test navigation bar
113
+
114
+ def test_navigation_bar
115
+ get '/level1/level2/level3/level%204'
116
+ assert last_response.body.include?('<a href=\'/level1/level2\'>level2</a>')
117
+ end
110
118
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: directory_listing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: '0.4'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-10 00:00:00.000000000 Z
12
+ date: 2014-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: filesize
@@ -127,6 +127,10 @@ files:
127
127
  - lib/sinatra/directory_listing/resource.rb
128
128
  - lib/sinatra/directory_listing/version.rb
129
129
  - test/public/1234.txt
130
+ - test/public/level1/level2/level3/level 4/test
131
+ - test/public/level1/level2/level3/test
132
+ - test/public/level1/level2/test
133
+ - test/public/level1/test
130
134
  - test/public/readme/test.txt
131
135
  - test/public/should_list_invisibles/test.txt
132
136
  - test/public/sorting/1k.dat
@@ -156,12 +160,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
160
  version: '0'
157
161
  requirements: []
158
162
  rubyforge_project:
159
- rubygems_version: 1.8.23
163
+ rubygems_version: 1.8.24
160
164
  signing_key:
161
165
  specification_version: 3
162
166
  summary: Easy, CSS-styled, Apache-like directory listings for Sinatra.
163
167
  test_files:
164
168
  - test/public/1234.txt
169
+ - test/public/level1/level2/level3/level 4/test
170
+ - test/public/level1/level2/level3/test
171
+ - test/public/level1/level2/test
172
+ - test/public/level1/test
165
173
  - test/public/readme/test.txt
166
174
  - test/public/should_list_invisibles/test.txt
167
175
  - test/public/sorting/1k.dat