img
[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 print_project ($)
34 {
35 my($class,$ListItem)=@_;
36
37         print "<h1>".$ListItem->{"name"}."</h1>\n";
38         print $ListItem->{"description"};
39         print "<hr />\n";
40         my @table=(
41                 {"key"=>qr(^download\b),"text"=>sub ($) {
42                                                 $_[0]=~s/^download//;
43                                                 $_[0]=~s/^-/ /;
44                                                 return "Download".$_[0];
45                                                 },
46                                 "format"=>sub ($) {
47                                                 my $r;
48                                                 if ($_[0]=~m#^[a-z]+://#) {
49                                                         $r=a_href($_[0],CGI::escapeHTML($_[0]));
50                                                         }
51                                                 else {
52                                                         $r=a_href($_[0],CGI::escapeHTML(File::Basename::basename($_[0])));
53                                                         my $size=(stat $_[0])[7];
54                                                         die "Cannot stat \"".$_[0]."\": $!" if !defined $size;
55                                                                  if ($size>=1024*1024) { $size=int($size/(1024*1024))." MB"; }
56                                                         elsif ($size>=1024     ) { $size=int($size/(1024     ))." KB"; }
57                                                         else                     { $size=int($size            )." B"; }
58                                                         $r.=" ($size)";
59                                                         }
60                                                 return $r;
61                                                 }},
62                 {"key"=>qr(^link\b),"text"=>sub ($) {
63                                                 $_[0]=~s/^link-//;
64                                                 return $_[0];
65                                                 },
66                                 "format"=>sub ($) {
67                                                 return a_href($_[0],CGI::escapeHTML($_[0]));
68                                                 }},
69                 {"key"=>qr(^cvs\b),"text"=>sub ($) {
70                                                 $_[0]=~s/^cvs//;
71                                                 $_[0]=~s/^-/ /;
72                                                 return "CVS".$_[0];
73                                                 },
74                                 "format"=>sub ($$) {
75                                                 my($val,$key)=@_;
76                                                 $key=~s/^cvs//;
77                                                 $key=~s/^-/ /;
78                                                 my $branch="";
79                                                 $branch="only_with_tag=$1" if $val=~s/:(.*)//;
80                                                 return join("<br />\n\t\t",map({ a_href($_->[1],$_->[0]); }
81                                                                 ["ViewCVS CVS repository",$W->{"project_viewcvs"}.$val."/".(!$branch ? '' : '?'.$branch)],
82                                                                 ["Download CVS snapshot" ,
83                                                                                 $W->{"project_viewcvs"}.$val."/".File::Basename::basename($val).".tar.gz?tarball=1"
84                                                                                                 .(!$branch ? '' : '&'.$branch)],
85                                                                 ["CVS ChangeLog"         ,$W->{"top_dir"}."/project/ChangeLog.txt.pl?cvs=$val"]));
86                                                 }},
87                 {"key"=>"summary","text"=>"Summary"},
88                 {"key"=>"ownership","text"=>"Ownership"},
89                 {"key"=>"license","text"=>"License","format"=>sub ($) {
90                                 my %known=(
91                                                 "PD"=>"Public Domain",
92                                                 "GPL"=>a_href("http://www.gnu.org/licenses/gpl.html","GNU General Public License"),
93                                                 "LGPL"=>a_href("http://www.gnu.org/licenses/lgpl.html","GNU Lesser General Public License"),
94                                                 "com"=>"Commercial"
95                                                 );
96                                 return $known{$_[0]};
97                                 }},
98                 {"key"=>"maintenance","text"=>"State","format"=>sub ($) {
99                                 my %known=(
100                                                 "active"=>"Ready to use and the package is being actively developed.",
101                                                 "ready"=>"Ready to use although no longer being actively developed.",
102                                                 "dead"=>"Dead code, no longer supported.",
103                                                 "merge"=>"Functions belong to existing other project.",
104                                                 "obsolete"=>"Obsoleted.",
105                                                 "update"=>"Package needs updating to recent software.",
106                                                 "accepted"=>"This patch got already integrated by the original package maintainer.",
107                                                 ""=>"",
108                                                 );
109                                 my @r;
110                                 for ($known{($_[0]=~/^([^-]*)-?/)[0] || ""}) {
111                                         push @r,$_ if $_;
112                                         push @r," $'" if $';
113                                         }
114                                 return join(" ",@r);
115                                 }},
116                 {"key"=>"sponsorship","text"=>"Sponsoring Company"},
117                 {"key"=>"language","text"=>"Programming language","format"=>sub ($) {
118                                 return a_href("http://java.sun.com/",CGI::escapeHTML($_[0]))
119                                                 if $_[0]=~/^Java\b/;
120                                 return a_href("http://www.php.net/",CGI::escapeHTML($_[0]))
121                                                 if $_[0]=~/^PHP\b/;
122                                 return undef();
123                                 }},
124                 );
125
126 sub tableit_func
127 {
128 my($tableit,$val,$key,$ListItem)=@_;
129
130         delete $ListItem->{$key};
131         my $r="";
132         $r.="<tr>";
133                 if ($tableit->{"text"}) {
134                         $r.="<td>";
135                                 $r.=(!ref $_ ? $_ : &{$_}($key)) for ($tableit->{"text"});
136                         $r.="</td>";
137                         }
138                 if ($tableit->{"format"}) {
139                         do { $val=$_ if defined $_; } for (&{$tableit->{"format"}}($val,$key));
140                         }
141                 return join("",map("<tr><td>".$_->[0]."</td><td>".$_->[1]."</td></tr>\n",@$val))
142                                 if ref $val;
143                 $r.="<td>$val</td>";
144         $r.="</tr>\n";
145 }
146
147         print '<table border="0" class="print_project">'."\n";
148                 for my $tableit (@table) {
149                         if (!ref $tableit->{"key"}) {
150                                 print tableit_func($tableit,$ListItem->{$tableit->{"key"}},$tableit->{"key"},$ListItem)
151                                                 if $ListItem->{$tableit->{"key"}};
152                                 }
153                         else {
154                                 for my $key (@{$ListItem->{"keys_array"}}) {
155                                         my $keyregex=$tableit->{"key"};
156                                         next if $key!~/$keyregex/;
157                                         print tableit_func($tableit,$ListItem->{$key},$key,$ListItem);
158                                         }
159                                 }
160                         }
161         print "</table>\n";
162         print vskip;
163 }
164
165 sub project_arr_to_hash (@)
166 {
167 my(@arr)=@_;
168
169         return (
170                         @arr,
171                         "keys_array"=>[ My::Web::arr_keys(@arr) ],
172                         );
173 }
174
175 # $args{"ListItem"}=\%...;
176 sub init_project ($%)
177 {
178 my($class,%args)=@_;
179
180         my $ListItem={ project_arr_to_hash(@{$args{"ListItem"}}) };
181         my $name=$ListItem->{"name"};
182         $name=~s#<a\s[^>]*>([^<]*)</a>#$1#g;
183         my $W=$class->init(
184                         "title"=>$name,
185                         %args,
186                         "head_css"=>($args{"head_css"} || "")."
187 table.print_project td { vertical-align: top; }
188 ",
189                         );
190         $class->heading();
191         $class->print_project({ %$ListItem });
192         return $W;
193 }
194
195 1;