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