+RHCE
[www.jankratochvil.net.git] / project / List.pm
1 # $Id$
2 # List of projects Perl template.
3 # Copyright (C) 2003-2005 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::List;
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 Wrequire 'project::Lib';
28 use Carp qw(confess cluck);
29
30
31 sub handler
32 {
33 My::Web->init(
34                 "title"=>'Project List',
35                 "args_check"=>{
36                                 "platform"=>'^(?:platform)?$',
37                                 },
38 #               "rel_up"=>"/",  # TODO:homepage
39 #               "rel_start"=>"/",       # TODO:homepage
40                 # Not used if no full listing is done:
41                 #"footer_ids"=>0,
42                 );
43 My::Web->heading();
44
45
46 print(project::Lib->views(($W->{"args"}{"platform"} ? "BriefPlatform" : "BriefUnified")));
47 print(project::Lib->platforms(undef(),"novskip"=>1)) if $W->{"args"}{"platform"};
48
49 my %item=project::Lib->name_to_hashref();
50 my %item_unused=%item;
51
52 # $col{"name"}{"show"}=1
53 # $col{"name"}{"format"}=sub { "<".$_[0].">"; }
54 # $col{"name"}{""}="Project name";
55 # @col_order
56
57 my @row_order=qw(-priority);
58 my @col_order=qw(name summary license maintenance language);
59 my %col;
60
61 sub format_url ($) { return (!$_[0] ? "" : '<a href="'.$_[0].'">X</a>'); }
62
63 $col{"name"}{"format"}=sub {
64                 local $_=$_[0];
65                 s#<a\s[^>]*>([^<]*)</a>#$1#g;
66                 return "<a href=\"".$_[1]."/\">"
67                                 .(!$My::Web::W->{"args"}{"W"} ? "" : $item{$_[1]}{"priority"}.":")
68                                 .$_."</a>";
69                 };
70
71 $col{"license"}{"format"}=sub {
72                 my %known=(
73                                 "PD"=>"<a href=\"http://www.gnu.org/philosophy/categories.html#PublicDomainSoftware\">PD</a>",
74                                 "GPL"=>"<a href=\"http://www.gnu.org/licenses/gpl.html\">GPL</a>",
75                                 "LGPL"=>"<a href=\"http://www.gnu.org/licenses/lgpl.html\">LGPL</a>",
76                                 );
77                 return $known{$_[0]} if $known{$_[0]};
78                 return $_[0];
79                 };
80
81 $col{"maintenance"}{"format"}=sub {
82                 return ($_[0]=~/^([^-]*)/)[0];
83                 };
84
85 $col{"online-demo"}{"format"}=\&format_url;
86 $col{"download"}{"format"}=\&format_url;
87 $col{"name"}{""}="Project Name";
88 $col{"summary"}{""}="Abstract";
89 $col{"license"}{""}="Copying";
90 $col{"maintenance"}{""}="State";
91 $col{"language"}{""}="Language";
92
93 # Fill in cols not contained in @col_order
94 if (0) {
95         my %col_order=map(($_=>1),@col_order);
96         push @col_order,map({ ($col_order{$_} ? () : $_); } keys(%{{ map(($_=>1),map((keys(%{$item{$_}})),keys(%item)))}}));
97         }
98
99 my $print_one_platform=sub ($)
100 {
101 my($platform)=@_;
102
103         print '<table border="1">'."\n";
104                 print '<tr>';
105                         for my $col (@col_order) {
106                                 next if defined $col{$col}{"show"} && !$col{$col}{"show"};
107                                 print '<th>'.($col{$col}{""} || "[$col]").'</th>';
108                                 }
109                 print '</tr>'."\n";
110                 my @rows_ordered=sort {
111                                 for my $order_by (@row_order,"name") {
112                                         my $order_by=$order_by;
113                                         my $minus=($order_by=~s/^(-)//)[0];
114                                         # FIXME: 'cmp' or '<=>'?
115                                         # Currently it complains on "name" not <=>able if two projects have the same 'priority'.
116                                         my $r=($item{$a}{$order_by} <=> $item{$b}{$order_by});
117                                         $r=-$r if $minus;
118                                         return $r if $r;
119                                         }
120                                 return 0;
121                                 } map(($platform && $item{$_}{"platform"} ne $platform ? () : ($_)),keys(%item));
122                 for my $row (@rows_ordered) {
123                         print '<tr>';
124                         for my $col (@col_order) {
125                                 next if defined $col{$col}{"show"} && !$col{$col}{"show"};
126                                 delete $item_unused{$row};
127                                 print '<td>';
128                                 if (!$col{$col}{"format"}) {
129                                         print(($item{$row}{$col} || ""));
130                                         }
131                                 else {
132                                         print(&{$col{$col}{"format"}}($item{$row}{$col},$row));
133                                         }
134                                 print '</td>';
135                                 }
136                         print '</tr>'."\n";
137                         if ($W->{"args"}{"description_opt"}) {
138                                 print '<tr><td style="border: none;"></td>';
139                                 print '<td colspan="'.(scalar(@col_order)-1).'"><blockquote>'.$item{$row}{"description"}.'</blockquote></td>';
140                                 print '</tr>'."\n";
141                                 print '<tr><td style="border: none;" colspan="'.scalar(@col_order).'">&nbsp;</td></tr>'."\n";
142                                 }
143                         }
144         print '</table>'."\n";
145 };
146
147
148 if (!$W->{"args"}{"platform"}) {
149         &{$print_one_platform}(undef());
150         }
151 else {
152         my @platforms=@project::Lib::platforms;
153         while (@platforms) {
154                 my $platform_sym =shift @platforms;
155                 my $platform_name=shift @platforms;
156                 print '<h2 id="'.$platform_sym.'">'.$platform_name.'</h2>'."\n";
157                 &{$print_one_platform}($platform_sym);
158                 }
159         }
160 cluck "INVALID 'platform': $_" for keys(%item_unused);
161
162
163 exit;
164 }
165 1;