2 # Nick's web site: Generate directory listing.
4 # Copyright © 2021 Nick Bowler
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.
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.
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/>
22 @items.find_all(@item[:pattern]).each do |item|
25 item.reps.each do |rep|
30 next unless "#{d}/" == mydir
33 displaysize = Dir.children(File.dirname(rep.raw_path)).length - 1
34 size = displaysize - 1000000
37 size = File.size(rep.raw_path)
38 displaysize = human_filesize(size)
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 UTC" end,
45 displaysize: displaysize,
52 if @items["#{File.dirname(mydir)}/index.lst"]
53 files[".."] = { type: :UP }
56 def render_entry(files, key)
60 "<img alt='#{f[:type]}' width='16' height='16' src='#{case f[:type]
61 when :DIR; "/images/folder.svg"
62 when :UP; "/images/return.svg"
63 else raise "no icon for filetype #{f[:type]}"
66 <td><a href='#{key}'>#{
67 if key == ".." then "[Parent Directory]" else key end
69 <td>#{f[:displaytime]}</td>
70 <td>#{f[:displaysize]}</td>
75 <script type='x'><![CDATA[]]x><!--]]></script>
76 <input style='display: none' type='radio' name='filelist-sort' id='filelist-name-sort' checked='checked' />
77 <input style='display: none' type='radio' name='filelist-sort' id='filelist-date-sort' />
78 <input style='display: none' type='radio' name='filelist-sort' id='filelist-size-sort' />
79 <input style='display: none' type='checkbox' id='filelist-name-rev' />
80 <input style='display: none' type='checkbox' id='filelist-date-rev' />
81 <input style='display: none' type='checkbox' id='filelist-size-rev' />
82 <script type='x'>--></script>
83 <table class='filelist'>
88 <label for='filelist-name-sort'><span>Name</span></label>
89 <script type='x'><![CDATA[]]x><!--]]></script>
90 <label for='filelist-name-rev' style='display: none'>
92 <img alt='FWD' width='16' height='16' src='/images/down.svg' />
93 <img alt='REV' width='16' height='16' src='/images/up.svg' style='display: none' />
95 <script type='x'>--></script>
98 <label for='filelist-date-sort'><span>Last Modified</span></label>
99 <script type='x'><![CDATA[]]x><!--]]></script>
100 <label for='filelist-date-rev' style='display: none'>
101 <span>Last Modified</span>
102 <img alt='FWD' width='16' height='16' src='/images/down.svg' />
103 <img alt='REV' width='16' height='16' src='/images/up.svg' style='display: none' />
105 <script type='x'>--></script>
108 <label for='filelist-size-sort'><span>Size</span></label>
109 <script type='x'><![CDATA[]]x><!--]]></script>
110 <label for='filelist-size-rev' style='display: none'>
112 <img alt='FWD' width='16' height='16' src='/images/down.svg' />
113 <img alt='REV' width='16' height='16' src='/images/up.svg' style='display: none' />
115 <script type='x'>--></script>
121 parentrow = if files[".."] then "#{render_entry(files, "..")}" end
123 if parentrow then "<tr>#{parentrow}</tr>" end
126 by_name = files.keys.sort{ |a, b| strverscmp(a, b) }
127 by_name.each_index do |i|
128 entry = render_entry(files, by_name[i])
129 if i+1 == by_name.length
130 entry.sub!(%r{(.*)</td>}m,
131 "\\1<script type='x'><![CDATA[]]x><!--]]></script></td>")
134 <tr><%= entry %></tr>
138 <tbody style='display: none'>
140 def meta_cmp(files, key, a, b)
141 av, bv = files[a][key], files[b][key]
142 return av <=> bv if av != bv
143 return strverscmp(a, b)
146 by_date = files.keys.sort { |a, b| meta_cmp(files, :sorttime, a, b) }
147 by_size = files.keys.sort { |a, b| meta_cmp(files, :size, a, b) }
149 listnames = [ "namerev", "date", "daterev", "size", "sizerev" ]
150 lists = [ by_name.reverse, by_date, by_date.reverse, by_size, by_size.reverse ]
153 <tr class='<%= listnames.join(" ") %>'><%= parentrow %></tr>
156 evenmap = (0..(lists.length-1)).map { false }
159 while not (elems = lists.map(&:first)).compact.empty?
160 matches = (0..(lists.length-1)).to_a.keep_if { |x| evenmap[x] == even }
162 elems = elems.values_at(*matches).compact
163 mode = elems.group_by{|a| a}.max{|a, b| a[1].length <=> b[1].length}[0]
166 lists.each_index do |i|
167 if evenmap[i] == even and lists[i].first.eql? mode
170 evenmap[i] = nil if lists[i].empty?
176 <tr class='<%= listnames.values_at(*matches).join(" ") %>'>
177 <%= render_entry(files, mode) %>
189 <tr><td><script type='x'>--></script></tr>