]> git.draconx.ca Git - homepage.git/blob - layouts/listing.xhtml
Implement version-aware sorting of file listings.
[homepage.git] / layouts / listing.xhtml
1 <%
2 # Nick's web site: Generate directory listing.
3 #
4 # Copyright © 2021 Nick Bowler
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <https://www.gnu.org/licenses/>
18
19 mydir = rep_uri
20
21 files = {}
22 @items.find_all(@item[:pattern]).each do |item|
23   t = item[:updated_at]
24
25   item.reps.each do |rep|
26     next if rep == @rep
27
28     p = rep_uri(rep)
29     d, f = File.split(p)
30     next unless "#{d}/" == mydir
31
32     if p =~ %r{/$}
33       sz = Dir.children(File.dirname(rep.raw_path)).length - 1
34       type = "DIR"
35     else
36       sz = human_filesize(File.size(rep.raw_path))
37       type = nil
38     end
39
40     files[f] = {
41       mtime: if t then t.getutc.strftime "%Y-%m-%d %H:%M UTC" end,
42       size: sz,
43       type: type,
44     }
45   end
46 end
47
48 if @items["#{File.dirname(mydir)}/index.lst"]
49   files[".."] = { type: "UP" }
50 end
51 %>
52 <table class='filelist'>
53   <thead>
54     <tr><th /><th>Name</th><th>Last Modified</th><th>Size</th></tr>
55   </thead>
56   <tbody>
57 <% files.keys.sort{ |a, b| strverscmp(a, b) }.each do |key| %>
58     <tr>
59       <td>
60 <% if files[key][:type] %>
61         <div>
62           <img src='<%=
63             case files[key][:type]
64             when "DIR"; "/images/folder.svg"
65             when "UP";  "/images/return.svg"
66             else raise "no icon for filetype #{files[key][:type]}"
67             end %>' alt='<%= files[key][:type] %>' width='16' height='16' />
68         </div>
69 <% end %>
70       </td>
71       <td><a href='<%= key %>'><%=
72         if key == ".." then "[Parent Directory]" else key end
73       %></a></td>
74       <td><%= files[key][:mtime] %></td>
75       <td><%= files[key][:size] %></td>
76     </tr>
77 <% end %>
78   </tbody>
79 </table>