My::Hash::* reimplementation for separate feature add-on packages (cleanup).
[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         our %list_cache;
214         our @list_cache;
215         if (!@list_cache) {
216                 My::Web->make_file(LIST_FILENAME());
217                 local *F;
218                 open F,LIST_FILENAME() or do {
219                         cluck "Error opening \"".LIST_FILENAME()."\": $!";
220                         return;
221                         };
222                 my @r=split(" ",do { undef $/; <F>; });
223                 close F or cluck "Error closing \"".LIST_FILENAME()."\": $!";
224                 cluck "No projects found?" if !@r;
225                 @list_cache=@r;
226                 %list_cache=map(($_=>1),@list_cache);
227                 }
228         return \%list_cache if !wantarray();
229         return @list_cache;
230 }
231
232 # Returns: hashlist of hashrefs if !$name.
233 sub name_to_hashref($;$)
234 {
235 my($class,$name)=@_;
236
237         cluck if !wantarray() && !$name;
238         # Do not cache the result to get all the items &Wrequire-mapped.
239         return map(($_=>$class->name_to_hashref($_)),$class->list()) if !$name;
240         cluck join(" ","Project name \"$name\" not listed in 'list_cache':",$class->list())
241                         if !$class->list()->{$name};
242         # Do not cache &Wrequire to gets its $Id$ markers / usage map.
243         Wrequire "project::${name}::Index";
244         our %cache;
245         if (!$cache{$name}) {
246                 my $arrayref=eval('\@project::'.$name.'::Index::ListItem');
247                 do { warn "Broken project/$name/Index.pm"; return undef(); } if !@$arrayref;
248                 $cache{$name}=$class->project_arrayref_to_hashref($arrayref);
249                 }
250         return $cache{$name};
251 }
252
253 # $args{"ListItem"}=\%...;
254 sub init($%)
255 {
256 my($class,%args)=@_;
257
258         $args{"__PACKAGE__"}||=caller();
259         $args{"project_name"}||=($args{"__PACKAGE__"}=~/^project::(\w+)::Index$/)[0]
260                         or cluck "Error finding project name of the package: ".$args{"__PACKAGE__"};
261         my $ListItem=$class->name_to_hashref($args{"project_name"});
262         my $W=$class->SUPER::init(
263                         "title"=>My::Web->a_href_inhibit(sub { return $class->title($ListItem); }),
264                         map(("rel_$_"=>'/project/Rel.pm?rel='.$_.'&project='.$args{"project_name"}),qw(prev next)),
265                         "rel_up"=>'/project/',
266 #                       "rel_start"=>"/",       # TODO:homepage
267                         "css_push"=>"/project/Lib.css",
268                         %args,
269                         "heading_novskip"=>1,
270                         );
271         $class->heading();
272         print $class->platforms($ListItem->{"platform"});
273         $class->print_project($ListItem,%args);
274         return $W;
275 }
276
277 our @platforms=(
278                 "unixuser"=>"UNIX",
279                 "unixdevel"=>"UNIX-devel",
280                 "web"=>"Web",
281                 "amiga"=>"Amiga",
282                 "w32"=>"MS-Windows",
283                 "dos"=>"MS-DOS",
284                 "patch"=>"Patches",
285                 );
286
287 sub views ($$)
288 {
289 my($class,$view_selected)=@_;
290
291         my $view=sub ($$)
292                 {
293                 my($current,$href,$content)=@_;
294
295                         return a_href($href,$content) if $current ne $view_selected;
296                         return "<b>".$content."</b> (current)";
297                 };
298
299         return <<"HERE";
300 <h1>Project List of @{[ a_href 'http://www.jankratochvil.net/','Jan Kratochvil' ]}</h1>
301
302 <ul>
303         <li>@{[ &{$view}('Detailed'     ,'/project/','Detailed project listing per platform') ]}</li>
304         <li>@{[ &{$view}('BriefPlatform','/project/List.pm?platform=platform',
305                         'Brief project listing per platform') ]}</li>
306         <li>@{[ &{$view}('BriefUnified' ,'/project/List.pm',
307                         'Unified brief project listing') ]}</li>
308 </ul>
309 @{[ vskip "1ex" ]}
310 HERE
311 }
312
313 sub platforms ($;$%)
314 {
315 my($class,$platform_selected,%args)=@_;
316
317         my $r="";
318         $r.='<table border="0" class="margin-center"><tr>'."\n";
319                 $r.='<td>';
320                         $r.='<table border="1" style="border-collapse: collapse; border-style: solid; border-width: 1px;">'."\n";
321                                 $r.='<tr>'."\n";
322                                         $r.='<td style="padding: 5px; font-weight: bold;">'."\n";
323                                                 $r.='Projects';
324                                         $r.='</td>'."\n";
325                                 $r.='</tr>'."\n";
326                         $r.='</table>';
327                 $r.='</td>';
328                 $r.='<td>';
329                         $r.='<table border="1" style="border-collapse: collapse; border-style: solid;">'."\n";
330                                 $r.='<tr>'."\n";
331                                         my @platforms=@platforms;
332                                         while (@platforms) {
333                                                 my $platform_sym =shift @platforms;
334                                                 my $platform_name=shift @platforms;
335                                                 my $chosen=($platform_selected && $platform_selected eq $platform_sym);
336                                                 $r.='<td style="padding: 5px;">';
337                                                         $r.=a_href((!$platform_selected ? "" : "/project/").'#'.$platform_sym,$platform_name,
338                                                                         "attr"=>($chosen
339                                                                                         ? 'style="text-decoration: underline; font-weight: bold;"'
340                                                                                         : 'style="text-decoration: inherit; /* revoke underline */"'));
341                                                 $r.="</td>\n";
342                                                 }
343                                 $r.='</tr>'."\n";
344                         $r.='</table>'."\n";
345                 $r.='</td>'."\n";
346         $r.='</tr></table>'."\n";
347         if (!$args{"novskip"}) {
348                 $r.="<hr />\n";
349                 $r.=My::Web::vskip "6ex";
350                 }
351         return $r;
352 }
353
354 sub section ($$)
355 {
356 my($class,$name)=@_;
357
358         my $item=$class->name_to_hashref($name);
359         my $title=$class->title($item);
360         my $r="";
361
362         print $class->platforms($item->{"platform"},"novskip"=>1);
363
364         $r.='<table border="1" style="border-collapse: collapse; border-style: solid;" class="margin-center">'."\n";
365                 $r.='<tr><td style="font-size: larger;">'."\n";
366                         $r.=a_href "/project/$name/",$title;
367                 $r.='</td></tr>'."\n";
368         $r.='</table>'."\n";
369         $r.=vskip "1ex";
370         return $r;
371 }
372
373 1;