modperl bootstrap
[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"=>"summary","text"=>"Summary"},
70                 {"key"=>"ownership","text"=>"Ownership"},
71                 {"key"=>"license","text"=>"License","format"=>sub ($) {
72                                 my %known=(
73                                                 "PD"=>"Public Domain",
74                                                 "GPL"=>a_href("http://www.gnu.org/licenses/gpl.html","GNU General Public License"),
75                                                 "LGPL"=>a_href("http://www.gnu.org/licenses/lgpl.html","GNU Lesser General Public License"),
76                                                 );
77                                 return $known{$_[0]};
78                                 }},
79                 {"key"=>"maintenance","text"=>"Currently maintained?","format"=>sub ($) {
80                                 my %known=(
81                                                 "finished"=>"Project is finished. Possible bug reports welcome although project not actively developed.",
82                                                 "dead"=>"Project became dead code, some updates would be required. It is no longer used, project is not supported.",
83                                                 "obsolete"=>"Obsolete as some other existing package superseded this one.",
84                                                 "merge"=>"Functions of this package should be merged to some other one.",
85                                                 "update"=>"Package needs updating to be fully usable, patches welcome.",
86                                                 "accepted"=>"This patch was accepted by the original package author. It has no longer any separate meaning.",
87                                                 );
88                                 return $known{$_[0]};
89                                 }},
90                 {"key"=>"reason","text"=>"Reason"},
91                 {"key"=>"sponsorship","text"=>"Sponsoring Company"},
92                 {"key"=>"language","text"=>"Programming language","format"=>sub ($) {
93                                 return a_href("http://java.sun.com/",CGI::escapeHTML($_[0]))
94                                                 if $_[0]=~/^Java\b/;
95                                 return a_href("http://www.php.net/",CGI::escapeHTML($_[0]))
96                                                 if $_[0]=~/^PHP\b/;
97                                 return undef();
98                                 }},
99                 );
100         print '<table border="0">'."\n";
101
102 sub tableit_func
103 {
104 my($tableit,$val,$key,$ListItem)=@_;
105
106         print "<tr><td>";
107         if (!ref $tableit->{"text"}) {
108                 print $tableit->{"text"};
109                 }
110         else {
111                 my $textfunc=$tableit->{"text"};
112                 my $key=$key;
113                 print &$textfunc($key);
114                 }
115         print ":</td>";
116         if ($tableit->{"format"}) {
117                 my $format=$tableit->{"format"};
118                 my $valn=&$format($val);
119                 $val=$valn if defined $valn;
120                 }
121         print "<td>$val</td></tr>\n";
122         delete $ListItem->{$key};
123 }
124
125         for my $tableit (@table) {
126                 if (!ref $tableit->{"key"}) {
127                         tableit_func($tableit,$ListItem->{$tableit->{"key"}},$tableit->{"key"},$ListItem) if $ListItem->{$tableit->{"key"}};
128                         }
129                 else {
130                         for my $key (@{$ListItem->{"keys_array"}}) {
131                                 my $keyregex=$tableit->{"key"};
132                                 next if $key!~/$keyregex/;
133                                 tableit_func($tableit,$ListItem->{$key},$key,$ListItem);
134                                 }
135                         }
136                 }
137         print "</table>\n";
138         print vskip;
139 }
140
141 sub project_arr_to_hash (@)
142 {
143 my(@arr)=@_;
144
145         return (
146                         @arr,
147                         "keys_array"=>[ My::Web::arr_keys(@arr) ],
148                         );
149 }
150
151 # $args{"ListItem"}=\%...;
152 sub init_project ($%)
153 {
154 my($class,%args)=@_;
155
156         my $ListItem={ project_arr_to_hash(@{$args{"ListItem"}}) };
157         my $name=$ListItem->{"name"};
158         $name=~s#<a\s[^>]*>([^<]*)</a>#$1#g;
159         my $W=$class->init(
160                         "title"=>$name,
161                         %args);
162         $class->heading();
163         $class->print_project({ %$ListItem });
164         return $W;
165 }
166
167 1;