Module: Sinatra::Directory_listing
- Defined in:
- lib/sinatra/directory_listing.rb,
lib/sinatra/directory_listing/layout.rb
Constant Summary collapse
- LAYOUT =
"<html>\n<head>\n <title>Index of <%= page.current_page %>, sorted <%= page.sort_item_display %> <%= page.sort_direction_display %></title>\n <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>\n <%= page.stylesheet %>\n</head>\n<body>\n <div class=\"nav\">\n<h1><%= page.nav_bar %></h1>\n </div>\n\n <table>\n<tr>\n <th><a href='<%= page.file_sort_link %>'>File</a></th>\n <th><a href='<%= page.mtime_sort_link %>'>Last modified</a></th>\n <th><a href='<%= page.size_sort_link %>'>Size</a></th>\n</tr>\n<%= page.files_html %>\n </table>\n\n <br>\n <a><%= page.readme %></a>\n</body>\n</html>\n"
Instance Method Summary collapse
-
#list(o = {}) ⇒ Object
Generate the page.
Instance Method Details
#list(o = {}) ⇒ Object
Generate the page.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sinatra/directory_listing.rb', line 19 def list(o={}) ## # Set default options. = { :should_list_invisibles => false, :last_modified_format => "%Y-%m-%d %H:%M:%S", :filename_truncate_length => 40, :stylesheet => "", :readme => "" }.merge(o) ## # Create a page object and start setting attributes. page = Page.new page.should_list_invisibles = [:should_list_invisibles] page.last_modified_format = [:last_modified_format] page.filename_truncate_length = [:filename_truncate_length] page.public_folder = settings.public_folder page.request_path = request.path page.request_params = request.params page.current_page = URI.unescape(request.path) ## # Set the readme and stylesheet page.readme = [:readme] if [:readme] if [:stylesheet] page.stylesheet = "<link rel='stylesheet' type='text/css' href='/#{options[:stylesheet].sub(/^[\/]*/,"")}'>" end ## # Get an array of files to be listed. files = Array.new Dir.foreach(File.join(settings.public_folder, URI.unescape(request.path))) do |file| files.push(file) end ## # If the only thing in the array are invisible files, # display a "No files" listing. page.files_html = "" if files == [".", ".."] page.files_html << " <tr> <th>No files.</th> <th>-</th> <th>-</th> </tr>" else ## # Otherwise, create an array of Resources: resources = Array.new Dir.foreach(File.join(settings.public_folder, URI.unescape(request.path))) do |resource| resources.push(Resource.new(resource, page)) end ## # Get the sortby and direction parameters # ("file" and "ascending" by default). page.sort_item = "file" page.sort_direction = "ascending" page.sort_item = page.request_params["sortby"] if page.request_params["sortby"] page.sort_direction = page.request_params["direction"] if page.request_params["direction"] ## # Sort the resources. # The second and third arguments are what to sort by ("file", "mtime", # or "size"), and whether to sort in order ("ascending") or reverse # ("descending"). sorted_resources = Resource.sort(resources, page.sort_item, page.sort_direction) ## # Set display variables and sort links based on sorting variables page.file_sort_link, page.mtime_sort_link, page.size_sort_link, page.sort_item_display, page.sort_direction_display = page.sorting_info(page.sort_item, page.sort_direction) ## # Finally, generate the html from the array of Resources. sorted_resources.each do |resource| page.files_html << resource.wrap end end ## # Generate and return the complete page from the erb template. erb = ERB.new(LAYOUT) erb.result(binding) end |