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