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