openpgp: 7E25094870599135A83DD2C6C446E92ECE0D66B2
[www.jankratochvil.net.git] / project / Lib.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 project::Lib;
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 use Carp qw(cluck confess);
28
29 use Exporter;
30 our @EXPORT=qw();
31 Wrequire 'Lib';
32 our @ISA=qw(My::Web Lib Exporter);
33
34
35 my $LIST_ABSPATH="/project/SUBDIRS";
36
37
38 sub print_project
39 {
40 my($class,$ListItem)=@_;
41
42         print "<h1>".$class->title($ListItem)."</h1>\n";
43         do { print $_ if $_; } for ($W->{"project_text_after_title"});
44         print $ListItem->{"description"};
45         print "<hr />\n";
46         print($W->{"before_project_data"}||"");
47         return if $W->{"no_project_data"};
48         my @table=(
49                 {"key"=>"summary","text"=>"Summary"},
50                 {"key"=>"license","text"=>"License","format"=>sub ($) {
51                                 my %known=(
52                                                 "PD"=>"Public Domain",
53                                                 "GPL"=>a_href("http://www.gnu.org/licenses/gpl.html","GNU General Public License"),
54                                                 "LGPL"=>a_href("http://www.gnu.org/licenses/lgpl.html","GNU Lesser General Public License"),
55                                                 "com"=>"Commercial"
56                                                 );
57                                 return $known{$_[0]};
58                                 }},
59                 {"key"=>"maintenance","text"=>"State","format"=>sub ($) {
60                                 my %known=(
61                                                 "active"=>"Ready to use. Project is now actively developed.",
62                                                 "ready"=>"Ready to use. Maintained.",
63                                                 "dead"=>"Dead code, no longer supported.",
64                                                 "merge"=>"Functions belong to existing other project.",
65                                                 "obsolete"=>"Obsoleted.",
66                                                 "update"=>"Package needs updating to recent software.",
67                                                 "accepted"=>"This patch got already integrated by the original package maintainer.",
68                                                 "pending"=>"Patch is ready to be applied to the mainstream.",
69                                                 "ignored"=>"Patch was ignored. It is not applied in the mainstream.",
70                                                 ""=>"",
71                                                 );
72                                 my @r;
73                                 for ($known{($_[0]=~/^([^-]*)-?/)[0] || ""}) {
74                                         push @r,$_ if $_;
75                                         push @r," $'" if $';
76                                         }
77                                 return join(" ",@r);
78                                 }},
79                 {"key"=>"aminet","text"=>a_href('http://www.aminet.net/','Aminet'),"format"=>sub ($) {
80                                 return join(" ",
81                                                 a_href('http://www.aminet.net/'.$_[0].".lha",$_[0].".lha"),
82                                                 "(".a_href('http://www.aminet.net/'.$_[0].".readme","readme").")");
83                                 }},
84                 {"key"=>qr(^download\b),"text"=>sub ($) {
85                                                 $_[0]=~s/^download//;
86                                                 $_[0]=~s/^-/ /;
87                                                 return "Download".$_[0];
88                                                 },
89                                 "format"=>sub ($) {
90                                                 return a_href($_[0],escapeHTML(File::Basename::basename($_[0])),"size"=>2);
91                                                 }},
92                 {"key"=>qr(^link\b),"text"=>sub ($) {
93                                                 $_[0]=~s/^link-//;
94                                                 return $_[0];
95                                                 },
96                                 "format"=>sub ($) {
97                                                 return($_[0]=~/^<a\b/ ? $_[0] : a_href($_[0],escapeHTML($_[0])));
98                                                 }},
99                 {"key"=>qr(^cvs\b),"text"=>sub ($) {
100                                                 $_[0]=~s/^cvs//;
101                                                 $_[0]=~s/^-/ /;
102                                                 return "GIT".$_[0];
103                                                 },
104                                 "format"=>sub ($$) {
105                                                 my($val,$key)=@_;
106                                                 $key=~s/^cvs//;
107                                                 $key=~s/^-/ /;
108                                                 my $branch="";
109                                                 $branch=$1 if $val=~s/:(.*)//;
110                                                 return join("<br />\n\t\t",
111 #pserver:
112 #                                                               escapeHTML("cvs -d ".$W->{"pserver"}.":".$W->{"pserver_path"}." -z3"
113 #                                                                               ." checkout".(!$branch ? "" : " -r $branch -kk")
114 #                                                                               .($val!~m#/# ? "" : " -d ".File::Basename::basename($val))
115 #                                                                               ." $val"),
116                                                                 join(" | \n\t\t",
117                                                                                 map({ a_href($_->[1],$_->[0]); }
118 #                                                                                               ["ViewCVS CVS repository",$W->{"project_viewcvs"}.$val."/".(!$branch ? "" : '?only_with_tag='.$branch)],
119 #                                                                                               ["Download CVS snapshot" ,
120 #                                                                                                               $W->{"project_viewcvs"}.$val."/".File::Basename::basename($val).".tar.gz?tarball=1"
121 #                                                                                                                               .(!$branch ? "" : '&only_with_tag='.$branch)],
122 ##FIXME:                                                                                                ["CVS ChangeLog"         ,"/project/ChangeLog.pm?cvs=$val"]
123                                                                                                 ["GIT repository",$W->{"project_viewcvs"}."?p=$val.git;a=tree".(!$branch ? "" : ";hb=$branch")],
124                                                                                                 ["Download GIT snapshot",$W->{"project_viewcvs"}."?p=$val.git;a=snapshot;sf=tgz;h=".(!$branch ? "HEAD" : "$branch")],
125                                                                                                 ["GIT shortlog",$W->{"project_viewcvs"}."?p=$val.git;a=shortlog".(!$branch ? "" : ";h=refs/heads/$branch")]
126                                                                                                 )));
127                                                 }},
128                 {"key"=>"ownership","text"=>"Ownership"},
129                 {"key"=>"sponsorship","text"=>"Sponsorship"},
130                 {"key"=>"language","text"=>"Programming language","format"=>sub ($) {
131                                 return a_href("http://java.sun.com/",escapeHTML($_[0]))
132                                                 if $_[0]=~/^Java\b/;
133                                 return a_href("http://www.php.net/",escapeHTML($_[0]))
134                                                 if $_[0]=~/^PHP\b/;
135                                 return undef();
136                                 }},
137                 );
138
139 sub tableit_func
140 {
141 my($tableit,$val,$key,$ListItem)=@_;
142
143         my $r="";
144         $r.="<tr>";
145                 if ($tableit->{"text"}) {
146                         $r.="<td>";
147                                 $r.=(!ref $_ ? $_ : &{$_}($key)) for ($tableit->{"text"});
148                         $r.="</td>";
149                         }
150                 if ($tableit->{"format"}) {
151                         do { $val=$_ if defined $_; } for (&{$tableit->{"format"}}($val,$key));
152                         }
153                 return join("",map("<tr><td>".$_->[0]."</td><td>".$_->[1]."</td></tr>\n",@$val))
154                                 if ref $val;
155                 $r.="<td>$val</td>";
156         $r.="</tr>\n";
157 }
158
159         my %used_key;
160         print '<table border="0" class="print_project">'."\n";
161                 for my $tableit (@table) {
162                         if (!ref $tableit->{"key"}) {
163                                 print tableit_func($tableit,$ListItem->{$tableit->{"key"}},$tableit->{"key"},$ListItem)
164                                                 if $ListItem->{$tableit->{"key"}} && !$used_key{$tableit->{"key"}}++;
165                                 }
166                         else {
167                                 for my $key (@{$ListItem->{"keys_array"}}) {
168                                         my $keyregex=$tableit->{"key"};
169                                         next if $key!~/$keyregex/;
170                                         print tableit_func($tableit,$ListItem->{$key},$key,$ListItem);
171                                         }
172                                 }
173                         }
174         print "</table>\n";
175         print vskip;
176 }
177
178 # Returns: hashref if !wantarray(), list if wantarray().
179 sub list($)
180 {
181 my($self)=@_;
182
183         return $self->list_abspath($LIST_ABSPATH);
184 }
185
186 # $args{"ListItem"}=\%...;
187 sub init($%)
188 {
189 my($class,%args)=@_;
190
191         $args{"__PACKAGE__"}||=caller();
192         $args{"project_name"}||=($args{"__PACKAGE__"}=~/^project::(\w+)::Index$/)[0]
193                         or cluck "Error finding project name of the package: ".$args{"__PACKAGE__"};
194         my $ListItem=$class->name_to_hashref($args{"project_name"});
195         my $W=$class->SUPER::init(
196                         "title"=>My::Web->a_href_inhibit(sub { return $class->title($ListItem); }),
197                         map(("rel_$_"=>'/project/Rel.pm?rel='.$_.'&project='.$args{"project_name"}),qw(prev next)),
198                         "rel_up"=>'/project/',
199 #                       "rel_start"=>"/",       # TODO:homepage
200                         "css_push"=>"/project/Lib.css",
201                         %args,
202                         "heading_novskip"=>1,
203                         );
204         do { &{$_}() if $_; } for $W->{__PACKAGE__."-init-hook"};
205         $class->heading();
206         print $class->platforms($ListItem->{"platform"});
207         $class->print_project($ListItem,%args);
208         return $W;
209 }
210
211 our @platforms=(
212                 "unixuser"=>"UNIX",
213                 "unixdevel"=>"UNIX-devel",
214                 "web"=>"Web",
215                 "amiga"=>"Amiga",
216                 "w32"=>"MS-Windows",
217                 "dos"=>"MS-DOS",
218                 "patch"=>"Patches",
219                 );
220
221 sub views ($$)
222 {
223 my($class,$view_selected)=@_;
224
225         my $view=sub ($$)
226                 {
227                 my($current,$href,$content)=@_;
228
229                         return a_href($href,$content) if $current ne $view_selected;
230                         return "<b>".$content."</b> (current)";
231                 };
232
233         return <<"HERE";
234 <h1>Project List of @{[ a_href 'http://www.jankratochvil.net/','Jan Kratochvil' ]}</h1>
235
236 <ul>
237         <li>@{[ &{$view}('Detailed'     ,'/project/','Detailed project listing per platform') ]}</li>
238         <li>@{[ &{$view}('BriefPlatform','/project/List.pm?platform=platform',
239                         'Brief project listing per platform') ]}</li>
240         <li>@{[ &{$view}('BriefUnified' ,'/project/List.pm',
241                         'Unified brief project listing') ]}</li>
242 </ul>
243 @{[ vskip "1ex" ]}
244 HERE
245 }
246
247 sub platforms ($;$%)
248 {
249 my($class,$platform_selected,%args)=@_;
250
251         my $r="";
252         $r.='<table border="0" class="margin-center"><tr>'."\n";
253                 $r.='<td>';
254                         $r.='<table border="1" style="border-collapse: collapse; border-style: solid; border-width: 1px;">'."\n";
255                                 $r.='<tr>'."\n";
256                                         $r.='<td style="padding: 5px; font-weight: bold;">'."\n";
257                                                 $r.='Projects';
258                                         $r.='</td>'."\n";
259                                 $r.='</tr>'."\n";
260                         $r.='</table>';
261                 $r.='</td>';
262                 $r.='<td>';
263                         $r.='<table border="1" style="border-collapse: collapse; border-style: solid;">'."\n";
264                                 $r.='<tr>'."\n";
265                                         my @platforms=@platforms;
266                                         while (@platforms) {
267                                                 my $platform_sym =shift @platforms;
268                                                 my $platform_name=shift @platforms;
269                                                 my $chosen=($platform_selected && $platform_selected eq $platform_sym);
270                                                 $r.='<td style="padding: 5px;">';
271                                                         $r.=a_href((!$platform_selected ? "" : "/project/").'#'.$platform_sym,$platform_name,
272                                                                         "attr"=>($chosen
273                                                                                         ? 'style="text-decoration: underline; font-weight: bold;"'
274                                                                                         : 'style="text-decoration: inherit; /* revoke underline */"'));
275                                                 $r.="</td>\n";
276                                                 }
277                                 $r.='</tr>'."\n";
278                         $r.='</table>'."\n";
279                 $r.='</td>'."\n";
280         $r.='</tr></table>'."\n";
281         if (!$args{"novskip"}) {
282                 Wrequire 'WebConfig';
283                 $r.=WebConfig->vskip_hr();
284                 }
285         return $r;
286 }
287
288 sub section($$)
289 {
290 my($class,$name)=@_;
291
292         my $item=$class->name_to_hashref($name);
293         return ""
294                         .$class->platforms($item->{"platform"},"novskip"=>1)
295                         .$class->SUPER::section($name);
296 }
297
298 1;