No pserver.
[www.jankratochvil.net.git] / Lib.pm
1 # $Id$
2 # Common functions for HTML/XHTML output generation
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 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 our @ISA=qw(My::Web Exporter);
32
33
34 # Returns: hashref if !wantarray(), list if wantarray().
35 # This cache is "headers_in" hits safe - only local files reading.
36 sub list_abspath($)
37 {
38 my($self,$list_abspath)=@_;
39
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;
44
45         # %{$list_cache{$list_abspath}{"hash"}}
46         # @{$list_cache{$list_abspath}{"array"}}
47         our %list_cache;
48
49         if (!$list_cache{$list_abspath}) {
50                 My::Web->make_file($list_filename);
51                 local *F;
52                 open F,$list_filename or do {
53                         cluck "Error opening \"$list_filename\": $!";
54                         return;
55                         };
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}={
60                         "array"=>\@r,
61                         "hash" =>{ map(($_=>1),@r) },
62                         };
63                 }
64         return   $list_cache{$list_abspath}{"hash"} if !wantarray();
65         return @{$list_cache{$list_abspath}{"array"}};
66 }
67
68 sub _hashlikearray_get_keys(@)
69 {
70 my(@hashlikearray)=@_;
71
72         my @r;
73         while (@hashlikearray) {
74                 push @r,shift @hashlikearray;   # key
75                 shift @hashlikearray;   # val
76                 }
77         return @r;
78 }
79
80 # $args{"override"}={"platform"=>"product"};
81 sub _project_arrayref_to_hashref($$)
82 {
83 my($self,$arrayref,%args)=@_;
84
85         Wrequire 'My::Hash';
86         return My::Hash->new({
87                 @$arrayref,
88                 map((!$_ ? () : %$_),$args{"override"}),
89                 "keys_array"=>[ _hashlikearray_get_keys(@$arrayref) ],
90                 },"My::Hash::Sub","My::Hash::Readonly");
91 }
92
93 # Returns: hashlist of hashrefs if !$name.
94 # 'abstract': requires: &$self::list
95 sub name_to_hashref($;$%)
96 {
97 my($class,$name,%args)=@_;
98
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);
111 }
112
113 sub title($$)
114 {
115 my($class,$hashref)=@_;
116
117         cluck if !$hashref->{"name"} || !$hashref->{"summary"};
118         return $hashref->{"name"}.": ".$hashref->{"summary"},
119 }
120
121 sub section($$)
122 {
123 my($class,$name)=@_;
124
125         my $item=$class->name_to_hashref($name);
126         my $title=$class->title($item);
127         my $project_product=(caller()=~/^(project|product)::/)[0] or cluck;
128         my $r="";
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";
133         $r.='</table>'."\n";
134         $r.="<hr />\n";
135         $r.=vskip "1ex";
136         return $r;
137 }
138
139 1;