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