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