1 |
wakaba |
1.2 |
#!/usr/local/bin/perl |
2 |
|
|
|
3 |
wakaba |
1.4 |
use strict; |
4 |
wakaba |
1.2 |
|
5 |
wakaba |
1.4 |
=head1 NAME |
6 |
wakaba |
1.2 |
|
7 |
wakaba |
1.4 |
piclist - Making List of Pictures in a Directory |
8 |
wakaba |
1.2 |
|
9 |
wakaba |
1.4 |
=cut |
10 |
wakaba |
1.2 |
|
11 |
wakaba |
1.4 |
unless ($main::ENV{PATH_TRANSLATED}) { |
12 |
|
|
die "BAD PATH_TRANSLATED"; |
13 |
|
|
} |
14 |
wakaba |
1.2 |
|
15 |
wakaba |
1.4 |
my %Opt; |
16 |
wakaba |
1.2 |
|
17 |
wakaba |
1.4 |
my $dir = $main::ENV{PATH_TRANSLATED}; |
18 |
|
|
if ($dir =~ s#/LIST$##) { |
19 |
|
|
for (split /[&;]/, $ENV{QUERY_STRING}) { |
20 |
|
|
my ($name, $val) = split /=/, $_, 2; |
21 |
|
|
$Opt{$name} = defined $val ? $val : 1; |
22 |
|
|
} |
23 |
|
|
} else { |
24 |
|
|
die "BAD PATH_TRANSLATED: $ENV{PATH_TRANSLATED}"; |
25 |
|
|
} |
26 |
wakaba |
1.2 |
|
27 |
|
|
|
28 |
wakaba |
1.4 |
sub escape ($) { |
29 |
|
|
my $s = shift; |
30 |
|
|
$s =~ s/&/&/g; |
31 |
|
|
$s =~ s/</</g; |
32 |
|
|
$s =~ s/>/>/g; |
33 |
|
|
$s =~ s/"/"/g; |
34 |
wakaba |
1.5 |
$s =~ s/'/'/g; |
35 |
wakaba |
1.4 |
$s; |
36 |
|
|
} |
37 |
wakaba |
1.2 |
|
38 |
wakaba |
1.4 |
sub rfc3339date ($) { |
39 |
|
|
my @gt = gmtime shift; |
40 |
|
|
sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ', |
41 |
|
|
$gt[5] + 1900, $gt[4] + 1, @gt[3, 2, 1, 0]; |
42 |
wakaba |
1.2 |
} |
43 |
|
|
|
44 |
wakaba |
1.4 |
sub filesize ($) { |
45 |
|
|
my $size = 0 + shift; |
46 |
|
|
if ($size > 2048) { |
47 |
|
|
$size /= 1024; |
48 |
|
|
if ($size > 2048) { |
49 |
|
|
$size /= 1024; |
50 |
|
|
sprintf '%.1f メガオクテット', $size; |
51 |
|
|
} else { |
52 |
|
|
sprintf '%.1f キロオクテット', $size; |
53 |
|
|
} |
54 |
|
|
} else { |
55 |
|
|
$size . ' オクテット'; |
56 |
|
|
} |
57 |
|
|
} |
58 |
wakaba |
1.2 |
|
59 |
wakaba |
1.4 |
opendir DIR, $dir or die "$dir: $!"; |
60 |
|
|
my @all_files = sort grep {not /^\./ and /^[A-Za-z0-9._-]+$/} |
61 |
|
|
(readdir DIR)[0..1000]; |
62 |
wakaba |
1.2 |
close DIR; |
63 |
wakaba |
1.4 |
my @files = grep {/\.(?:jpe?g|png|ico|gif|mng|xbm|JPE?G)(?:\.gz)?$/} @all_files; |
64 |
|
|
my @dirs = grep {$_ ne 'CVS' and -d $dir.'/'.$_} @all_files; |
65 |
wakaba |
1.2 |
|
66 |
wakaba |
1.4 |
sub has_file ($) { |
67 |
|
|
my $name = shift; |
68 |
|
|
my $namelen = 1 + length $name; |
69 |
|
|
for (@all_files) { |
70 |
|
|
if ($name.'.' eq substr $_, 0, $namelen) { |
71 |
|
|
return 1; |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
return 0; |
75 |
wakaba |
1.2 |
} |
76 |
|
|
|
77 |
wakaba |
1.3 |
my $title = '画像一覧'; |
78 |
wakaba |
1.4 |
my $dirpath = escape $ENV{REQUEST_URI}; |
79 |
|
|
$dirpath =~ s/\?.*$//; |
80 |
|
|
$dirpath =~ s#/LIST$##; |
81 |
|
|
$dirpath ||= '/'; |
82 |
wakaba |
1.2 |
|
83 |
|
|
print STDOUT "Content-Type: text/html; charset=euc-jp\n\n"; |
84 |
|
|
|
85 |
wakaba |
1.4 |
my $linkelement = '<link rel="stylesheet" href="/s/image-list" media="all" />'; |
86 |
wakaba |
1.2 |
|
87 |
|
|
$| = ''; |
88 |
|
|
print <<EOH; |
89 |
wakaba |
1.4 |
<!DOCTYPE html SYSTEM> |
90 |
|
|
<html lang="ja"> |
91 |
wakaba |
1.2 |
<head> |
92 |
wakaba |
1.5 |
<title>@{[$Opt{cframe} ? '' : qq<$dirpath の>]}${title}</title> |
93 |
wakaba |
1.2 |
${linkelement} |
94 |
|
|
</head> |
95 |
|
|
EOH |
96 |
|
|
|
97 |
wakaba |
1.5 |
my $LISTq = q<>; |
98 |
|
|
$LISTq .= q<;detail> if $Opt{detail}; |
99 |
|
|
$LISTq = substr $LISTq, 1; |
100 |
|
|
$LISTq = 'LIST' . ($LISTq ? '?' . $LISTq : ''); |
101 |
|
|
|
102 |
|
|
if ($Opt{cframe}) { |
103 |
|
|
my $LISTqt = ($LISTq eq 'LIST' ? $LISTq . '?' : $LISTq . ';') . 'target=view'; |
104 |
|
|
print qq{<frameset cols="25%,*"> |
105 |
|
|
<frame src="$LISTqt" name="list"> |
106 |
|
|
<frame src="./" name="view"> |
107 |
|
|
<noframes>}; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
my $viewtarget = ''; |
111 |
|
|
my $listtarget = ''; |
112 |
|
|
my $parenttarget = ''; |
113 |
|
|
if ($Opt{target} =~ /^([a-z]+)$/) { |
114 |
|
|
$viewtarget = qq{ target="$1"}; |
115 |
|
|
$listtarget = q{ target="_self"}; |
116 |
|
|
$LISTq .= $LISTq eq 'LIST' ? qq{?target=$1} : qq{;target=$1}; |
117 |
|
|
$parenttarget = q{ target="_parent"}; |
118 |
|
|
} |
119 |
|
|
|
120 |
wakaba |
1.4 |
if ($Opt{detail}) { |
121 |
wakaba |
1.5 |
print qq{<body@{[$Opt{target}?' class="has-target"':'']}> |
122 |
|
|
<h1>${title}</h1> |
123 |
|
|
<div class="pictures detail">}; |
124 |
wakaba |
1.4 |
|
125 |
|
|
for my $file_name (@files) { |
126 |
|
|
my $efile = escape $file_name; |
127 |
|
|
my $uri = $efile; |
128 |
|
|
$uri =~ s/\..+//g; |
129 |
|
|
my @cls = split /\./, lc $file_name; |
130 |
|
|
shift @cls; |
131 |
|
|
print q{<div class="image-with-desc">}; |
132 |
wakaba |
1.5 |
print qq{<a href="$uri"$viewtarget>}; |
133 |
wakaba |
1.4 |
print qq{<img src="$uri" alt="" class="@{[join ' ', @cls, 's']}"></a>}; |
134 |
|
|
print qq{<dl><dt>URI</dt><dd>}; |
135 |
wakaba |
1.5 |
print qq{<code class="uri"><<a href="$uri"$viewtarget>$uri</a>></code></dd>}; |
136 |
wakaba |
1.4 |
print qq{<dt>ファイル名</dt><dd>}; |
137 |
wakaba |
1.5 |
print qq{<code class="file"><a href="$efile"$viewtarget>$efile</a></code></dd>}; |
138 |
wakaba |
1.4 |
print qq{<dt>日付</dt><dd>}; |
139 |
|
|
print rfc3339date ([stat $dir.'/'.$file_name]->[9]); |
140 |
|
|
print qq{</dd>}; |
141 |
|
|
print qq{</dl>}; |
142 |
|
|
print q{</div>}; |
143 |
|
|
} |
144 |
wakaba |
1.2 |
|
145 |
wakaba |
1.4 |
my @videos = grep {/\.(?:avi|mpe?g|mp3|wav|mid|swf)(?:\.gz)?$/i} @all_files; |
146 |
|
|
for my $file_name (@videos) { |
147 |
|
|
my $efile = escape $file_name; |
148 |
|
|
my $uri = $efile; |
149 |
|
|
$uri =~ s/\..+//g; |
150 |
|
|
print q{<div class="image-with-desc">}; |
151 |
wakaba |
1.5 |
print qq{<a href="$uri"$viewtarget><img src="/~wakaba/archive/2005/movie-1" alt=""></a>}; |
152 |
wakaba |
1.4 |
print qq{<dl><dt>URI</dt><dd>}; |
153 |
wakaba |
1.5 |
print qq{<code class="uri"><<a href="$uri"$viewtarget>$uri</a>></code></dd>}; |
154 |
wakaba |
1.4 |
print qq{<dt>ファイル名</dt><dd>}; |
155 |
wakaba |
1.5 |
print qq{<code class="file"><a href="$efile"$viewtarget>$efile</a></code></dd>}; |
156 |
wakaba |
1.4 |
print qq{<dt>日付</dt><dd>}; |
157 |
|
|
print rfc3339date ([stat $dir.'/'.$file_name]->[9]); |
158 |
|
|
print qq{</dd>}; |
159 |
|
|
print qq{<dt>大きさ</dt><dd>}; |
160 |
|
|
print filesize ([stat $dir.'/'.$file_name]->[7]); |
161 |
|
|
print qq{</dd>}; |
162 |
|
|
print qq{</dl>}; |
163 |
|
|
print q{</div>}; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
for my $dir_name (@dirs) { |
167 |
|
|
my $edir = escape $dir_name; |
168 |
|
|
print q{<div class="dir dir-with-desc">}; |
169 |
|
|
if (-f $dir . '/' . $dir_name . '/favicon.png' or |
170 |
|
|
-f $dir . '/' . $dir_name . '/favicon.ico') { |
171 |
|
|
print qq{<img src="$edir/favicon" alt="" class="mini-icon">}; |
172 |
|
|
} else { |
173 |
|
|
print qq{<img src="/icons/folder" alt="" class="mini-icon">}; |
174 |
|
|
} |
175 |
wakaba |
1.5 |
print qq{<code class="file"><a href="$edir/$LISTq"$listtarget>$edir/</a></code>}; |
176 |
wakaba |
1.4 |
print q{</div>}; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
for (['cover', '表紙', 'start'], |
180 |
|
|
['introduction', 'はじめに', 'start'], |
181 |
|
|
['intro', 'はじめに', 'start'], |
182 |
|
|
['README', 'はじめに'], |
183 |
|
|
['contents', '目次', 'contents'], |
184 |
|
|
['list', '一覧', 'contents'], |
185 |
|
|
['description', '説明'], |
186 |
|
|
['index', '索引', 'index'], |
187 |
|
|
['latest', '最新版'], |
188 |
|
|
['current', '現行版']) { |
189 |
|
|
if (has_file $_->[0]) { |
190 |
|
|
print q{<div class="file file-with-desc">}; |
191 |
|
|
print q{<img src="/icons/layout" alt="" class="mini-icon">}; |
192 |
wakaba |
1.5 |
print qq{<a href="$_->[0]" rel="$_->[2]"$viewtarget>$_->[1]</a>}; |
193 |
wakaba |
1.4 |
print q{</div>}; |
194 |
|
|
} |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
print q{<div class="dir-up dir-with-desc">}; |
198 |
|
|
print q{<img src="/icons/forward" alt="" class="mini-icon">}; |
199 |
wakaba |
1.5 |
print qq{<a href="../$LISTq" rel="up"$listtarget>上の階層</a>}; |
200 |
wakaba |
1.4 |
print q{</div>}; |
201 |
|
|
|
202 |
|
|
print q{</div>}; |
203 |
|
|
|
204 |
wakaba |
1.5 |
} else { ## Normal Listing Mode |
205 |
|
|
print qq{<body@{[$Opt{target}?' class="has-target"':'']}> |
206 |
|
|
<h1>${title}</h1> |
207 |
|
|
<div class="pictures">}; |
208 |
wakaba |
1.4 |
my $imgsattr = ' class="s"'; |
209 |
|
|
|
210 |
|
|
for my $file_name (@files) { |
211 |
|
|
my $uri = escape $file_name; |
212 |
|
|
$uri =~ s/\..+$//g; |
213 |
wakaba |
1.5 |
print '<a href="'.$uri.'"'.$viewtarget.'>'; |
214 |
wakaba |
1.4 |
print '<img src="'.$uri.'" alt="'.$uri.'"'.$imgsattr.' />'; |
215 |
wakaba |
1.2 |
print "</a>\n"; |
216 |
|
|
} |
217 |
wakaba |
1.4 |
|
218 |
|
|
print q{</div>}; |
219 |
|
|
|
220 |
|
|
print q{<ul>}; |
221 |
|
|
for my $dir_name (@dirs) { |
222 |
|
|
my $edir = escape $dir_name; |
223 |
|
|
print q{<li class="dir">}; |
224 |
wakaba |
1.5 |
print qq{<code class="file"><a href="$edir/$LISTq"$listtarget>$edir/</a></code>}; |
225 |
wakaba |
1.4 |
print q{</li>}; |
226 |
|
|
} |
227 |
wakaba |
1.5 |
print qq{<li class="dir-up"><a href="../$LISTq" rel="up"$listtarget>上の階層</a></li></ul>}; |
228 |
wakaba |
1.4 |
} |
229 |
|
|
|
230 |
|
|
my $cvslink = ''; |
231 |
|
|
if (-d $dir . '/CVS') { |
232 |
|
|
if (-f $dir . '/CVS/Root') { |
233 |
|
|
open my $root, '<', $dir . '/CVS/Root'; |
234 |
|
|
if (<$root> =~ m#^(/home/cvs|/home/wakaba/pub/cvs)$#) { |
235 |
|
|
my $rpath = $1; |
236 |
|
|
if (-f $dir . '/CVS/Repository') { |
237 |
|
|
open my $repo, '<', $dir . '/CVS/Repository'; |
238 |
|
|
my $reppath = escape <$repo>; |
239 |
|
|
$reppath =~ tr/\x0A\x0D//d; |
240 |
|
|
if ($reppath) { |
241 |
|
|
$cvslink = qq{ <a href="/gate/cvs/$reppath/@{[ |
242 |
|
|
{q[/home/cvs] => '', |
243 |
|
|
q[/home/wakaba/pub/cvs] => '?cvsroot=Wakaba'}->{$rpath} |
244 |
wakaba |
1.5 |
]}" rel="history"$parenttarget>この階層の履歴</a>}; |
245 |
wakaba |
1.4 |
} |
246 |
|
|
} |
247 |
|
|
} |
248 |
|
|
} |
249 |
wakaba |
1.2 |
} |
250 |
|
|
|
251 |
|
|
print <<EOH; |
252 |
wakaba |
1.4 |
|
253 |
|
|
<div class="footer"> |
254 |
|
|
<div class="navigation"> |
255 |
wakaba |
1.5 |
[<a href="/" rel="home"$parenttarget>/</a>] |
256 |
|
|
[<a href="." rel="contents"$parenttarget>この階層</a>$cvslink] |
257 |
|
|
[画像一覧 (<a href="LIST" rel="alternate"$parenttarget>簡易</a>, |
258 |
|
|
<a href="LIST?cframe" rel="alternate"$parenttarget>簡易・横分割</a>, |
259 |
|
|
<a href="LIST?detail" rel="alternate"$parenttarget>詳細</a>, |
260 |
|
|
<a href="LIST?detail;cframe" rel="alternate"$parenttarget>詳細・横分割</a>)] |
261 |
wakaba |
1.4 |
</div> |
262 |
wakaba |
1.2 |
</div> |
263 |
|
|
</body> |
264 |
|
|
EOH |
265 |
|
|
|
266 |
wakaba |
1.5 |
print q{</noframes></frameset>} if $Opt{cframe}; |
267 |
|
|
|
268 |
|
|
print q{</html>}; |
269 |
|
|
|
270 |
wakaba |
1.2 |
1; |
271 |
|
|
|
272 |
wakaba |
1.4 |
__END__ |
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
|
|
=head1 CHANGES |
277 |
|
|
|
278 |
wakaba |
1.5 |
2005-02-26 Wakaba <w@suika.fam.cx> |
279 |
|
|
|
280 |
|
|
- Frame mode implemented. |
281 |
|
|
|
282 |
wakaba |
1.4 |
2005-02-25 Wakaba <w@suika.fam.cx> |
283 |
|
|
|
284 |
|
|
- Use external style sheet. |
285 |
wakaba |
1.5 |
- Detail mode implemented. |
286 |
wakaba |
1.4 |
|
287 |
|
|
2001-06-25 Wakaba <wakaba@61.201.226.127> |
288 |
|
|
|
289 |
|
|
- In default, images are sized by stylesheet. When ?realsize=1, |
290 |
|
|
images are not specified its size. |
291 |
|
|
- Images are linked to itself. |
292 |
|
|
|
293 |
|
|
2001-05-17 Wakaba |
294 |
|
|
|
295 |
|
|
- New File. |
296 |
|
|
|
297 |
|
|
=head1 LICENSE |
298 |
|
|
|
299 |
|
|
Public Domain. |
300 |
|
|
|
301 |
|
|
=cut |
302 |
|
|
|
303 |
wakaba |
1.5 |
# $Date: 2005/02/25 17:08:30 $ |