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