2 # Common functions for HTML/XHTML output generation
3 # Copyright (C) 2003-2005 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
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
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.
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
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; };
27 use Carp qw(cluck confess);
31 our @ISA=qw(My::Web Exporter);
34 # Returns: hashref if !wantarray(), list if wantarray().
35 # This cache is "headers_in" hits safe - only local files reading.
38 my($self,$list_abspath)=@_;
40 Wrequire __PACKAGE__; # inheritance
41 # Do not: path_abs_disk("/project/SUBDIRS");
42 # as we would need $W->{"r"} for the possibly relative path resolving.
43 my $list_filename=My::Web::dir_top_abs_disk().$list_abspath;
45 # %{$list_cache{$list_abspath}{"hash"}}
46 # @{$list_cache{$list_abspath}{"array"}}
49 if (!$list_cache{$list_abspath}) {
50 My::Web->make_file($list_filename);
52 open F,$list_filename or do {
53 cluck "Error opening \"$list_filename\": $!";
56 my @r=split(" ",do { undef $/; <F>; });
57 close F or cluck "Error closing \"$list_filename\": $!";
58 cluck "No projects found?" if !@r;
59 $list_cache{$list_abspath}={
61 "hash" =>{ map(($_=>1),@r) },
64 return $list_cache{$list_abspath}{"hash"} if !wantarray();
65 return @{$list_cache{$list_abspath}{"array"}};
68 sub _hashlikearray_get_keys(@)
70 my(@hashlikearray)=@_;
73 while (@hashlikearray) {
74 push @r,shift @hashlikearray; # key
75 shift @hashlikearray; # val
80 # $args{"override"}={"platform"=>"product"};
81 sub _project_arrayref_to_hashref($$)
83 my($self,$arrayref,%args)=@_;
86 return My::Hash->new({
88 map((!$_ ? () : %$_),$args{"override"}),
89 "keys_array"=>[ _hashlikearray_get_keys(@$arrayref) ],
90 },"My::Hash::Sub","My::Hash::Readonly");
93 # Returns: hashlist of hashrefs if !$name.
94 # 'abstract': requires: &$self::list
95 sub name_to_hashref($;$%)
97 my($class,$name,%args)=@_;
99 Wrequire __PACKAGE__; # inheritance
100 my $project_product=($class=~/^(project|product)::/)[0] or cluck;
101 cluck if !wantarray() && !$name;
102 # Do not cache the result to get all the items &Wrequire-mapped.
103 return map(($_=>$class->name_to_hashref($_,%args)),$class->list()) if !$name;
104 cluck join(" ","Project name $project_product::\"$name\" not listed in 'list_cache':",$class->list())
105 if !$class->list()->{$name};
106 # Never cache anything to be stable for "headers_in" hits.
107 Wrequire "${project_product}::${name}::Index";
108 my $arrayref=eval('\@'.$project_product.'::'.$name.'::Index::ListItem');
109 do { warn "Broken $project_product/$name/Index.pm"; return undef(); } if !@$arrayref;
110 return $class->_project_arrayref_to_hashref($arrayref,%args);
115 my($class,$hashref)=@_;
117 cluck if !$hashref->{"name"} || !$hashref->{"summary"};
118 return $hashref->{"name"}.": ".$hashref->{"summary"},
125 my $item=$class->name_to_hashref($name);
126 my $title=$class->title($item);
127 my $project_product=(caller()=~/^(project|product)::/)[0] or cluck;
129 $r.='<table border="1" style="border-collapse: collapse; border-style: solid;" class="margin-center">'."\n";
130 $r.='<tr><td style="font-size: larger;">'."\n";
131 $r.=a_href "/$project_product/$name/",$title;
132 $r.='</td></tr>'."\n";