Finally merged the branch 'apache20'(+'apache2') back to the main trunk.
[www.jankratochvil.net.git] / project / Lib.pm
1 # $Id$
2 # Common functions for HTML/XHTML output generation
3 # Copyright (C) 2003 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; exactly version 2 of June 1991 is required
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19 package project::Lib;
20 require 5.6.0;  # at least 'use warnings;' but we need some 5.6.0+ modules anyway
21 our $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
22 our $CVS_ID=q$Id$;
23 use strict;
24 use warnings;
25
26 use My::Web;
27 use Carp qw(cluck confess);
28
29 use Exporter;
30 our @EXPORT=qw();
31 our @ISA=qw(My::Web Exporter);
32
33
34 sub LIST_FILENAME()
35 {
36         # Do not: path_abs_disk("/project/SUBDIRS");
37         # as we would need $W->{"r"} for the possibly relative path resolving.
38         return My::Web::dir_top_abs_disk()."/project/SUBDIRS";
39 }
40
41
42 sub print_project
43 {
44 my($class,$ListItem)=@_;
45
46         print "<h1>".$class->title($ListItem)."</h1>\n";
47         do { print $_ if $_; } for ($W->{"project_text_after_title"});
48         print $ListItem->{"description"};
49         print "<hr />\n";
50         print($W->{"before_project_data"}||"");
51         return if $W->{"no_project_data"};
52         my @table=(
53                 {"key"=>"summary","text"=>"Summary"},
54                 {"key"=>"license","text"=>"License","format"=>sub ($) {
55                                 my %known=(
56                                                 "PD"=>"Public Domain",
57                                                 "GPL"=>a_href("http://www.gnu.org/licenses/gpl.html","GNU General Public License"),
58                                                 "LGPL"=>a_href("http://www.gnu.org/licenses/lgpl.html","GNU Lesser General Public License"),
59                                                 "com"=>"Commercial"
60                                                 );
61                                 return $known{$_[0]};
62                                 }},
63                 {"key"=>"maintenance","text"=>"State","format"=>sub ($) {
64                                 my %known=(
65                                                 "active"=>"Ready to use. Project is now actively developed.",
66                                                 "ready"=>"Ready to use. Maintained.",
67                                                 "dead"=>"Dead code, no longer supported.",
68                                                 "merge"=>"Functions belong to existing other project.",
69                                                 "obsolete"=>"Obsoleted.",
70                                                 "update"=>"Package needs updating to recent software.",
71                                                 "accepted"=>"This patch got already integrated by the original package maintainer.",
72                                                 "pending"=>"Patch is ready to be applied to the mainstream.",
73                                                 "ignored"=>"Patch was ignored. It is not applied in the mainstream.",
74                                                 ""=>"",
75                                                 );
76                                 my @r;
77                                 for ($known{($_[0]=~/^([^-]*)-?/)[0] || ""}) {
78                                         push @r,$_ if $_;
79                                         push @r," $'" if $';
80                                         }
81                                 return join(" ",@r);
82                                 }},
83                 {"key"=>"aminet","text"=>a_href('http://www.aminet.net/','Aminet'),"format"=>sub ($) {
84                                 return join(" ",
85                                                 a_href('http://www.aminet.net/'.$_[0].".lha",$_[0].".lha"),
86                                                 "(".a_href('http://www.aminet.net/'.$_[0].".readme","readme").")");
87                                 }},
88                 {"key"=>qr(^download\b),"text"=>sub ($) {
89                                                 $_[0]=~s/^download//;
90                                                 $_[0]=~s/^-/ /;
91                                                 return "Download".$_[0];
92                                                 },
93                                 "format"=>sub ($) {
94                                                 return a_href($_[0],escapeHTML(File::Basename::basename($_[0])),"size"=>2);
95                                                 }},
96                 {"key"=>qr(^link\b),"text"=>sub ($) {
97                                                 $_[0]=~s/^link-//;
98                                                 return $_[0];
99                                                 },
100                                 "format"=>sub ($) {
101                                                 return($_[0]=~/^<a\b/ ? $_[0] : a_href($_[0],escapeHTML($_[0])));
102                                                 }},
103                 {"key"=>qr(^cvs\b),"text"=>sub ($) {
104                                                 $_[0]=~s/^cvs//;
105                                                 $_[0]=~s/^-/ /;
106                                                 return "CVS".$_[0];
107                                                 },
108                                 "format"=>sub ($$) {
109                                                 my($val,$key)=@_;
110                                                 $key=~s/^cvs//;
111                                                 $key=~s/^-/ /;
112                                                 my $branch="";
113                                                 $branch=$1 if $val=~s/:(.*)//;
114                                                 return join("<br />\n\t\t",
115                                                                 escapeHTML("cvs -d ".$W->{"pserver"}.":".$W->{"pserver_path"}." -z3"
116                                                                                 ." checkout".(!$branch ? "" : " -r $branch -kk")
117                                                                                 .($val!~m#/# ? "" : " -d ".File::Basename::basename($val))
118                                                                                 ." $val"),
119                                                                 join(" | \n\t\t",
120                                                                                 map({ a_href($_->[1],$_->[0]); }
121                                                                                                 ["ViewCVS CVS repository",$W->{"project_viewcvs"}.$val."/".(!$branch ? "" : '?only_with_tag='.$branch)],
122                                                                                                 ["Download CVS snapshot" ,
123                                                                                                                 $W->{"project_viewcvs"}.$val."/".File::Basename::basename($val).".tar.gz?tarball=1"
124                                                                                                                                 .(!$branch ? "" : '&only_with_tag='.$branch)],
125                                                                                                 ["CVS ChangeLog"         ,"/project/ChangeLog.pm?cvs=$val"])));
126                                                 }},
127                 {"key"=>"ownership","text"=>"Ownership"},
128                 {"key"=>"sponsorship","text"=>"Sponsorship"},
129                 {"key"=>"language","text"=>"Programming language","format"=>sub ($) {
130                                 return a_href("http://java.sun.com/",escapeHTML($_[0]))
131                                                 if $_[0]=~/^Java\b/;
132                                 return a_href("http://www.php.net/",escapeHTML($_[0]))
133                                                 if $_[0]=~/^PHP\b/;
134                                 return undef();
135                                 }},
136                 );
137
138 sub tableit_func
139 {
140 my($tableit,$val,$key,$ListItem)=@_;
141
142         my $r="";
143         $r.="<tr>";
144                 if ($tableit->{"text"}) {
145                         $r.="<td>";
146                                 $r.=(!ref $_ ? $_ : &{$_}($key)) for ($tableit->{"text"});
147                         $r.="</td>";
148                         }
149                 if ($tableit->{"format"}) {
150                         do { $val=$_ if defined $_; } for (&{$tableit->{"format"}}($val,$key));
151                         }
152                 return join("",map("<tr><td>".$_->[0]."</td><td>".$_->[1]."</td></tr>\n",@$val))
153                                 if ref $val;
154                 $r.="<td>$val</td>";
155         $r.="</tr>\n";
156 }
157
158         my %used_key;
159         print '<table border="0" class="print_project">'."\n";
160                 for my $tableit (@table) {
161                         if (!ref $tableit->{"key"}) {
162                                 print tableit_func($tableit,$ListItem->{$tableit->{"key"}},$tableit->{"key"},$ListItem)
163                                                 if $ListItem->{$tableit->{"key"}} && !$used_key{$tableit->{"key"}}++;
164                                 }
165                         else {
166                                 for my $key (@{$ListItem->{"keys_array"}}) {
167                                         my $keyregex=$tableit->{"key"};
168                                         next if $key!~/$keyregex/;
169                                         print tableit_func($tableit,$ListItem->{$key},$key,$ListItem);
170                                         }
171                                 }
172                         }
173         print "</table>\n";
174         print vskip;
175 }
176
177 sub hashlikearray_get_keys(@)
178 {
179 my(@hashlikearray)=@_;
180
181         my @r;
182         while (@hashlikearray) {
183                 push @r,shift @hashlikearray;   # key
184                 shift @hashlikearray;   # val
185                 }
186         return @r;
187 }
188
189 sub project_arrayref_to_hashref($$)
190 {
191 my($self,$arrayref)=@_;
192
193         Wrequire 'My::Hash';
194         return My::Hash->new({
195                 @$arrayref,
196                 "keys_array"=>[ hashlikearray_get_keys(@$arrayref) ],
197                 },"My::Hash::Sub","My::Hash::Readonly");
198 }
199
200 sub title ($$)
201 {
202 my($class,$hashref)=@_;
203
204         cluck if !$hashref->{"name"} || !$hashref->{"summary"};
205         return $hashref->{"name"}.": ".$hashref->{"summary"},
206 }
207
208 # Returns: hashref if !wantarray(), list if wantarray().
209 sub list($)
210 {
211 my($self)=@_;
212
213         # This cache is "headers_in" hits safe - only local files reading.
214         our %list_cache;
215         our @list_cache;
216         if (!@list_cache) {
217                 My::Web->make_file(LIST_FILENAME());
218                 local *F;
219                 open F,LIST_FILENAME() or do {
220                         cluck "Error opening \"".LIST_FILENAME()."\": $!";
221                         return;
222                         };
223                 my @r=split(" ",do { undef $/; <F>; });
224                 close F or cluck "Error closing \"".LIST_FILENAME()."\": $!";
225                 cluck "No projects found?" if !@r;
226                 @list_cache=@r;
227                 %list_cache=map(($_=>1),@list_cache);
228                 }
229         return \%list_cache if !wantarray();
230         return @list_cache;
231 }
232
233 # Returns: hashlist of hashrefs if !$name.
234 sub name_to_hashref($;$)
235 {
236 my($class,$name)=@_;
237
238         cluck if !wantarray() && !$name;
239         # Do not cache the result to get all the items &Wrequire-mapped.
240         return map(($_=>$class->name_to_hashref($_)),$class->list()) if !$name;
241         cluck join(" ","Project name \"$name\" not listed in 'list_cache':",$class->list())
242                         if !$class->list()->{$name};
243         # Never cache anything to be stable for "headers_in" hits.
244         Wrequire "project::${name}::Index";
245         my $arrayref=eval('\@project::'.$name.'::Index::ListItem');
246         do { warn "Broken project/$name/Index.pm"; return undef(); } if !@$arrayref;
247         return $class->project_arrayref_to_hashref($arrayref);
248 }
249
250 # $args{"ListItem"}=\%...;
251 sub init($%)
252 {
253 my($class,%args)=@_;
254
255         $args{"__PACKAGE__"}||=caller();
256         $args{"project_name"}||=($args{"__PACKAGE__"}=~/^project::(\w+)::Index$/)[0]
257                         or cluck "Error finding project name of the package: ".$args{"__PACKAGE__"};
258         my $ListItem=$class->name_to_hashref($args{"project_name"});
259         my $W=$class->SUPER::init(
260                         "title"=>My::Web->a_href_inhibit(sub { return $class->title($ListItem); }),
261                         map(("rel_$_"=>'/project/Rel.pm?rel='.$_.'&project='.$args{"project_name"}),qw(prev next)),
262                         "rel_up"=>'/project/',
263 #                       "rel_start"=>"/",       # TODO:homepage
264                         "css_push"=>"/project/Lib.css",
265                         %args,
266                         "heading_novskip"=>1,
267                         );
268         $class->heading();
269         print $class->platforms($ListItem->{"platform"});
270         $class->print_project($ListItem,%args);
271         return $W;
272 }
273
274 our @platforms=(
275                 "unixuser"=>"UNIX",
276                 "unixdevel"=>"UNIX-devel",
277                 "web"=>"Web",
278                 "amiga"=>"Amiga",
279                 "w32"=>"MS-Windows",
280                 "dos"=>"MS-DOS",
281                 "patch"=>"Patches",
282                 );
283
284 sub views ($$)
285 {
286 my($class,$view_selected)=@_;
287
288         my $view=sub ($$)
289                 {
290                 my($current,$href,$content)=@_;
291
292                         return a_href($href,$content) if $current ne $view_selected;
293                         return "<b>".$content."</b> (current)";
294                 };
295
296         return <<"HERE";
297 <h1>Project List of @{[ a_href 'http://www.jankratochvil.net/','Jan Kratochvil' ]}</h1>
298
299 <ul>
300         <li>@{[ &{$view}('Detailed'     ,'/project/','Detailed project listing per platform') ]}</li>
301         <li>@{[ &{$view}('BriefPlatform','/project/List.pm?platform=platform',
302                         'Brief project listing per platform') ]}</li>
303         <li>@{[ &{$view}('BriefUnified' ,'/project/List.pm',
304                         'Unified brief project listing') ]}</li>
305 </ul>
306 @{[ vskip "1ex" ]}
307 HERE
308 }
309
310 sub platforms ($;$%)
311 {
312 my($class,$platform_selected,%args)=@_;
313
314         my $r="";
315         $r.='<table border="0" class="margin-center"><tr>'."\n";
316                 $r.='<td>';
317                         $r.='<table border="1" style="border-collapse: collapse; border-style: solid; border-width: 1px;">'."\n";
318                                 $r.='<tr>'."\n";
319                                         $r.='<td style="padding: 5px; font-weight: bold;">'."\n";
320                                                 $r.='Projects';
321                                         $r.='</td>'."\n";
322                                 $r.='</tr>'."\n";
323                         $r.='</table>';
324                 $r.='</td>';
325                 $r.='<td>';
326                         $r.='<table border="1" style="border-collapse: collapse; border-style: solid;">'."\n";
327                                 $r.='<tr>'."\n";
328                                         my @platforms=@platforms;
329                                         while (@platforms) {
330                                                 my $platform_sym =shift @platforms;
331                                                 my $platform_name=shift @platforms;
332                                                 my $chosen=($platform_selected && $platform_selected eq $platform_sym);
333                                                 $r.='<td style="padding: 5px;">';
334                                                         $r.=a_href((!$platform_selected ? "" : "/project/").'#'.$platform_sym,$platform_name,
335                                                                         "attr"=>($chosen
336                                                                                         ? 'style="text-decoration: underline; font-weight: bold;"'
337                                                                                         : 'style="text-decoration: inherit; /* revoke underline */"'));
338                                                 $r.="</td>\n";
339                                                 }
340                                 $r.='</tr>'."\n";
341                         $r.='</table>'."\n";
342                 $r.='</td>'."\n";
343         $r.='</tr></table>'."\n";
344         if (!$args{"novskip"}) {
345                 $r.="<hr />\n";
346                 $r.=My::Web::vskip "6ex";
347                 }
348         return $r;
349 }
350
351 sub section ($$)
352 {
353 my($class,$name)=@_;
354
355         my $item=$class->name_to_hashref($name);
356         my $title=$class->title($item);
357         my $r="";
358
359         print $class->platforms($item->{"platform"},"novskip"=>1);
360
361         $r.='<table border="1" style="border-collapse: collapse; border-style: solid;" class="margin-center">'."\n";
362                 $r.='<tr><td style="font-size: larger;">'."\n";
363                         $r.=a_href "/project/$name/",$title;
364                 $r.='</td></tr>'."\n";
365         $r.='</table>'."\n";
366         $r.=vskip "1ex";
367         return $r;
368 }
369
370 1;