]> git.draconx.ca Git - homepage.git/blob - layouts/listing.erb
Add separator rows to the sorted part of clicky tables.
[homepage.git] / layouts / listing.erb
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       displaysize = Dir.children(File.dirname(rep.raw_path)).length - 1
34       size = displaysize - 1000000
35       type = :DIR
36     else
37       size = File.size(rep.raw_path)
38       displaysize = human_filesize(size)
39       type = nil
40     end
41
42     files[f] = {
43       sorttime: if t then t.to_f else 0.0 end,
44       displaytime: if t then t.getutc.strftime "%Y-%m-%d %H:%M\u00a0UTC" end,
45       displaysize: displaysize,
46       size: size,
47       type: type,
48     }
49   end
50 end
51
52 if @items["#{File.dirname(mydir)}/index.lst"]
53   files[".."] = { type: :UP }
54 end
55
56 def render_entry(files, key)
57   f = files[key]
58   return <<~EOF
59     <td>#{if f[:type]
60       "<img alt='#{f[:type]}' width='16' height='16' src='#{case f[:type]
61             when :DIR; "/icons/folder.svg"
62             when :UP;  "/icons/return.svg"
63             else raise "no icon for filetype #{f[:type]}"
64             end}' />"
65     end}</td>
66     <td><a href='#{key}'>#{
67       if key == ".." then "[Parent Directory]" else key end
68     }</a></td>
69     <td>#{f[:displaytime]}</td>
70     <td>#{f[:displaysize]}</td>
71   EOF
72 end
73 %>
74 <table class='filelist' clicky='name'>
75   <thead>
76     <tr>
77       <th />
78       <th clicky='name'>Name</th>
79       <th clicky='date'>Last Modified</th>
80       <th clicky='size'>Size</th>
81     </tr>
82   </thead>
83   <tbody>
84 <%=
85     parentrow = if files[".."] then "#{render_entry(files, "..")}" end
86     files.delete("..")
87     if parentrow then "<tr>#{parentrow}</tr>" end
88 %>
89 <%
90 by_name = files.keys.sort{ |a, b| strverscmp(a, b) }
91 by_name.each do |key|
92   entry = render_entry(files, key)
93 %>
94     <tr><%= entry %></tr>
95 <% end %>
96   </tbody>
97
98   <tbody>
99 <%
100 def meta_cmp(files, key, a, b)
101   av, bv = files[a][key], files[b][key]
102   return av <=> bv if av != bv
103   return strverscmp(a, b)
104 end
105
106 by_date = files.keys.sort { |a, b| meta_cmp(files, :sorttime, a, b) }
107 by_size = files.keys.sort { |a, b| meta_cmp(files, :size, a, b) }
108
109 listnames = [ "namerev", "datefwd", "daterev", "sizefwd", "sizerev" ]
110 lists = [ by_name.reverse, by_date, by_date.reverse, by_size, by_size.reverse ]
111 if parentrow
112 %>
113   <tr class='<%= listnames.join(" ") %>'><%= parentrow %></tr>
114 <%
115 end
116 evenmap = (0..(lists.length-1)).map { false }
117 even = false
118
119 while not (elems = lists.map(&:first)).compact.empty?
120   matches = (0..(lists.length-1)).to_a.keep_if { |x| evenmap[x] == even }
121   if !matches.empty?
122     elems = elems.values_at(*matches).compact
123     mode = elems.group_by{|a| a}.max{|a, b| a[1].length <=> b[1].length}[0]
124     matches = []
125
126     lists.each_index do |i|
127       if evenmap[i] == even and lists[i].first.eql? mode
128         lists[i].shift
129         evenmap[i] ^= true
130         evenmap[i] = nil if lists[i].empty?
131
132         matches << i
133       end
134     end
135 %>
136     <tr class='<%= listnames.values_at(*matches).join(" ") %>'>
137       <%= render_entry(files, mode) %>
138     </tr>
139 <%
140   else
141 %>
142     <tr><td /></tr>
143 <%
144   end
145
146   even ^= true
147 end
148 %>
149   </tbody>
150 </table>