modperl branch collapsed back to MAIN trunk, man!
[MyWeb.git] / Project.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 My::Project;
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
28 use Exporter;
29 our @EXPORT=qw();
30 our @ISA=qw(My::Web Exporter);
31
32
33 sub ENTRIES     { return top_dir_disk()."/project/CVS/Entries"; }
34 sub ENTRIES_LOG { return top_dir_disk()."/project/CVS/Entries.Log"; }
35
36
37 sub print_project
38 {
39 my($class,$ListItem,%args)=@_;
40
41         print "<h1>".$W->{"title"}."</h1>\n";
42         print $ListItem->{"description"};
43         print "<hr />\n";
44         print $args{"before_project_data"} || "";
45         return if $args{"no_project_data"};
46         my @table=(
47                 {"key"=>"summary","text"=>"Summary"},
48                 {"key"=>"license","text"=>"License","format"=>sub ($) {
49                                 my %known=(
50                                                 "PD"=>"Public Domain",
51                                                 "GPL"=>a_href("http://www.gnu.org/licenses/gpl.html","GNU General Public License"),
52                                                 "LGPL"=>a_href("http://www.gnu.org/licenses/lgpl.html","GNU Lesser General Public License"),
53                                                 "com"=>"Commercial"
54                                                 );
55                                 return $known{$_[0]};
56                                 }},
57                 {"key"=>"maintenance","text"=>"State","format"=>sub ($) {
58                                 my %known=(
59                                                 "active"=>"Ready to use. Project is now actively developed.",
60                                                 "ready"=>"Ready to use. Maintained.",
61                                                 "dead"=>"Dead code, no longer supported.",
62                                                 "merge"=>"Functions belong to existing other project.",
63                                                 "obsolete"=>"Obsoleted.",
64                                                 "update"=>"Package needs updating to recent software.",
65                                                 "accepted"=>"This patch got already integrated by the original package maintainer.",
66                                                 "pending"=>"Patch is ready to be applied to the mainstream.",
67                                                 "ignored"=>"Patch was ignored. It is not applied in the mainstream.",
68                                                 ""=>"",
69                                                 );
70                                 my @r;
71                                 for ($known{($_[0]=~/^([^-]*)-?/)[0] || ""}) {
72                                         push @r,$_ if $_;
73                                         push @r," $'" if $';
74                                         }
75                                 return join(" ",@r);
76                                 }},
77                 {"key"=>"aminet","text"=>a_href('http://www.aminet.net/','Aminet'),"format"=>sub ($) {
78                                 return join(" ",
79                                                 a_href('http://www.aminet.net/'.$_[0].".lha",$_[0].".lha"),
80                                                 "(".a_href('http://www.aminet.net/'.$_[0].".readme","readme").")");
81                                 }},
82                 {"key"=>qr(^download\b),"text"=>sub ($) {
83                                                 $_[0]=~s/^download//;
84                                                 $_[0]=~s/^-/ /;
85                                                 return "Download".$_[0];
86                                                 },
87                                 "format"=>sub ($) {
88                                                 return a_href($_[0],CGI::escapeHTML(File::Basename::basename($_[0])));
89                                                 }},
90                 {"key"=>qr(^link\b),"text"=>sub ($) {
91                                                 $_[0]=~s/^link-//;
92                                                 return $_[0];
93                                                 },
94                                 "format"=>sub ($) {
95                                                 return($_[0]=~/^<a\b/ ? $_[0] : a_href($_[0],CGI::escapeHTML($_[0])));
96                                                 }},
97                 {"key"=>qr(^cvs\b),"text"=>sub ($) {
98                                                 $_[0]=~s/^cvs//;
99                                                 $_[0]=~s/^-/ /;
100                                                 return "CVS".$_[0];
101                                                 },
102                                 "format"=>sub ($$) {
103                                                 my($val,$key)=@_;
104                                                 $key=~s/^cvs//;
105                                                 $key=~s/^-/ /;
106                                                 my $branch="";
107                                                 $branch=$1 if $val=~s/:(.*)//;
108                                                 return join("<br />\n\t\t",
109                                                                 CGI::escapeHTML("cvs -d ".$W->{"pserver"}.":".$W->{"pserver_path"}." -z3"
110                                                                                 ." checkout".(!$branch ? "" : " -r $branch -kk")
111                                                                                 .($val!~m#/# ? "" : " -d ".File::Basename::basename($val))
112                                                                                 ." $val"),
113                                                                 join(" | \n\t\t",
114                                                                                 map({ a_href($_->[1],$_->[0]); }
115                                                                                                 ["ViewCVS CVS repository",$W->{"project_viewcvs"}.$val."/".(!$branch ? "" : '?only_with_tag='.$branch)],
116                                                                                                 ["Download CVS snapshot" ,
117                                                                                                                 $W->{"project_viewcvs"}.$val."/".File::Basename::basename($val).".tar.gz?tarball=1"
118                                                                                                                                 .(!$branch ? "" : '&only_with_tag='.$branch)],
119                                                                                                 ["CVS ChangeLog"         ,"/project/ChangeLog.txt.pl?cvs=$val"])));
120                                                 }},
121                 {"key"=>"ownership","text"=>"Ownership"},
122                 {"key"=>"sponsorship","text"=>"Sponsorship"},
123                 {"key"=>"language","text"=>"Programming language","format"=>sub ($) {
124                                 return a_href("http://java.sun.com/",CGI::escapeHTML($_[0]))
125                                                 if $_[0]=~/^Java\b/;
126                                 return a_href("http://www.php.net/",CGI::escapeHTML($_[0]))
127                                                 if $_[0]=~/^PHP\b/;
128                                 return undef();
129                                 }},
130                 );
131
132 sub tableit_func
133 {
134 my($tableit,$val,$key,$ListItem)=@_;
135
136         delete $ListItem->{$key};
137         my $r="";
138         $r.="<tr>";
139                 if ($tableit->{"text"}) {
140                         $r.="<td>";
141                                 $r.=(!ref $_ ? $_ : &{$_}($key)) for ($tableit->{"text"});
142                         $r.="</td>";
143                         }
144                 if ($tableit->{"format"}) {
145                         do { $val=$_ if defined $_; } for (&{$tableit->{"format"}}($val,$key));
146                         }
147                 return join("",map("<tr><td>".$_->[0]."</td><td>".$_->[1]."</td></tr>\n",@$val))
148                                 if ref $val;
149                 $r.="<td>$val</td>";
150         $r.="</tr>\n";
151 }
152
153         print '<table border="0" class="print_project">'."\n";
154                 for my $tableit (@table) {
155                         if (!ref $tableit->{"key"}) {
156                                 print tableit_func($tableit,$ListItem->{$tableit->{"key"}},$tableit->{"key"},$ListItem)
157                                                 if $ListItem->{$tableit->{"key"}};
158                                 }
159                         else {
160                                 for my $key (@{$ListItem->{"keys_array"}}) {
161                                         my $keyregex=$tableit->{"key"};
162                                         next if $key!~/$keyregex/;
163                                         print tableit_func($tableit,$ListItem->{$key},$key,$ListItem);
164                                         }
165                                 }
166                         }
167         print "</table>\n";
168         print vskip;
169 }
170
171 sub project_arr_to_hash (@)
172 {
173 my(@arr)=@_;
174
175         return (
176                         @arr,
177                         "keys_array"=>[ My::Web::arr_keys(@arr) ],
178                         );
179 }
180
181 sub title ($$)
182 {
183 my($class,$hashref)=@_;
184
185         return $hashref->{"name"}.": ".$hashref->{"summary"},
186 }
187
188 # $args{"ListItem"}=\%...;
189 sub init_project ($%)
190 {
191 my($class,%args)=@_;
192
193         my $ListItem={ project_arr_to_hash(@{$args{"ListItem"}}) };
194         my $W=$class->init(
195                         "title"=>$class->title($ListItem),
196                         map(("rel_$_"=>top_dir('/project/Rel.pl?rel='.$_.'&project='.($args{"__PACKAGE__"}=~/^.*::([^:]+)::[^:]+$/)[0])),
197                                         qw(prev next)),
198                         "rel_up"=>top_dir('/project/'),
199                         "rel_start"=>top_dir(),
200                         %args,
201                         "head_css"=>($args{"head_css"} || "")."
202 table.print_project td { vertical-align: top; }
203 ",
204                         "WebConfig::heading_novskip"=>1,
205                         );
206         $class->heading();
207         print $class->platforms($ListItem->{"platform"});
208         $class->print_project({ %$ListItem },%args);
209         return $W;
210 }
211
212 sub one_item_list_read($$)
213 {
214 my($class,$name)=@_;
215
216         Wrequire "project::${name}::ListItem";
217         my $item=eval('\@project::'.$name.'::ListItem::ListItem');
218         do { warn "Broken project/$name/ListItem.pm"; next; } if !defined $item;
219         return @$item;
220 }
221
222 sub item_hash_read ()
223 {
224 # FIXME: $class
225         my %dirs;
226         for my $ENTRIES (ENTRIES(),ENTRIES_LOG()) {
227                 local *E;
228                 next if !open E,$ENTRIES;
229                 while (<E>) {
230                         chomp;
231                         do { $dirs{$1}=1; next; } if m#^(?:A )?D/([^/]*)/#;
232                         next if m#^/([^/]*)/# ;
233                         next if /^D$/;
234                         warn "File $ENTRIES contains invalid line \"$_\": $!";
235                         }
236                 close E;
237                 }
238
239         my %item;
240         for my $dir (keys(%dirs)) {
241                 Wrequire "project::${dir}::ListItem";
242                 my $item=eval('\@project::'.$dir.'::ListItem::ListItem');
243                 do { warn "Broken project/$dir/ListItem.pm"; next; } if !defined $item;
244                 $item{$dir}={ My::Project::project_arr_to_hash(My::Project->one_item_list_read($dir)) };
245                 }
246         return %item;
247 }
248
249 our @platforms=(
250                 "unixuser"=>"UNIX",
251                 "unixdevel"=>"UNIX-devel",
252                 "web"=>"Web",
253                 "amiga"=>"Amiga",
254                 "w32"=>"MS-Windows",
255                 "dos"=>"MS-DOS",
256                 "patch"=>"Patches",
257                 );
258
259 sub views ($$)
260 {
261 my($class,$view_selected)=@_;
262
263         my $view=sub ($$)
264                 {
265                 my($current,$href,$content)=@_;
266
267                         return a_href($href,$content) if $current ne $view_selected;
268                         return "<b>".$content."</b> (current)";
269                 };
270
271         return <<"HERE";
272 <h1>Project List of @{[ a_href 'http://www.jankratochvil.net/','Jan Kratochvil' ]}</h1>
273
274 <ul>
275         <li>@{[ &{$view}('Detailed'     ,'/project/','Detailed project listing per platform') ]}</li>
276         <li>@{[ &{$view}('BriefPlatform','/project/List.html.pl?platform=platform',
277                         'Brief project listing per platform') ]}</li>
278         <li>@{[ &{$view}('BriefUnified' ,'/project/List.html.pl',
279                         'Unified brief project listing') ]}</li>
280 </ul>
281 @{[ vskip "1ex" ]}
282 HERE
283 }
284
285 sub platforms ($;$%)
286 {
287 my($class,$platform_selected,%args)=@_;
288
289         my $r="";
290         $r.='<table border="0" align="center"><tr>'."\n";
291                 $r.='<td>Projects:&nbsp;&nbsp;</td>';
292                 $r.='<td>';
293                         $r.='<table border="1" align="center" style="border-collapse: collapse; border-style: solid;">'."\n";
294                                 $r.='<tr>'."\n";
295                                         my @platforms=@platforms;
296                                         while (@platforms) {
297                                                 my $platform_sym =shift @platforms;
298                                                 my $platform_name=shift @platforms;
299                                                 my $chosen=($platform_selected && $platform_selected eq $platform_sym);
300                                                 $r.='<td style="padding: 5px;">';
301                                                         $r.=a_href((!$platform_selected ? "" : "/project/").'#'.$platform_sym,$platform_name,
302                                                                         "attr"=>($chosen
303                                                                                         ? 'style="text-decoration: underline; font-weight: bold;"'
304                                                                                         : 'style="text-decoration: inherit; /* revoke underline */"'));
305                                                 $r.="</td>\n";
306                                                 }
307                                 $r.='</tr>'."\n";
308                         $r.='</table>'."\n";
309                 $r.='</td>'."\n";
310         $r.='</tr></table>'."\n";
311         $r.=vskip "1ex" if !$args{"novskip"};
312         $r;
313 }
314
315 sub section ($$)
316 {
317 my($class,$name)=@_;
318
319         my %item=( $class->one_item_list_read($name) );
320         my $title=$class->title(\%item);
321         my $r="";
322
323         print $class->platforms($item{"platform"},"novskip"=>1);
324
325         $r.='<table border="0" align="center"><tr>'."\n";
326                 $r.='<td>';
327                         $r.='<table border="1" align="center" style="border-collapse: collapse; border-style: solid;">'."\n";
328                                 $r.='<tr><td style="font-size: larger;">'."\n";
329                                         $r.=a_href "/project/$name/",$title;
330                                 $r.='</td></tr>'."\n";
331                         $r.='</table>'."\n";
332                 $r.='</td>'."\n";
333         $r.='</tr></table>'."\n";
334         $r.=vskip "1ex";
335         $r;
336 }
337
338 1;