From: short <> Date: Fri, 16 May 2003 03:51:38 +0000 (+0000) Subject: bootstrap X-Git-Tag: bp_modperl~4 X-Git-Url: http://git.jankratochvil.net/?p=MyWeb.git;a=commitdiff_plain;h=fc0f3f8832778053713624c592c8d02df4b046a9 bootstrap --- fc0f3f8832778053713624c592c8d02df4b046a9 diff --git a/Web.pm b/Web.pm new file mode 100644 index 0000000..c0d8c3c --- /dev/null +++ b/Web.pm @@ -0,0 +1,300 @@ +#! /usr/bin/perl +# +# $Id$ +# Common functions for HTML/XHTML output generation +# Copyright (C) 2003 Jan Kratochvil +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; exactly version 2 of June 1991 is required +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +package My::Web; +require 5.6.0; # at least 'use warnings;' but we need some 5.6.0+ modules anyway +use vars qw($VERSION); +$VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; }; +use strict; +use warnings; + +use WebConfig; # for %WebConfig +require CGI; # for &escapeHTML +require Image::Size; # for &imgsize +use File::Basename; # &basename + + +my %Args; + # $Args{"title"} + # $Args{"force_charset"} + +my $cvs_id_html; +sub init ($%) +{ +my($class,%args)=@_; + + %WebConfig=(%WebConfig,%args); # override %WebConfig settings + + undef $WebConfig{"viewcvs"} if $ENV{"SCRIPT_NAME"} && $WebConfig{"viewcvs"} eq $ENV{"SCRIPT_NAME"}; + my @cvs_id_split=split / +/,$::CVS_ID; + if (@cvs_id_split==8) { + $cvs_id_split[2]="" + ."" + .$cvs_id_split[2].""; + $cvs_id_split[1]="".$cvs_id_split[1].""; + $cvs_id_split[5]="".$cvs_id_split[5].""; + } + $cvs_id_html=join " ",@cvs_id_split; +} + +# $args{"ListItem"}=\%...; +sub init_project ($%) +{ +my($class,%args)=@_; + + my $ListItem=$args{"ListItem"}; + my $name=$ListItem->{"name"}; + $name=~s#]*>([^<]*)#$1#g; + init($class, + "title"=>$name, + %args); + heading(); + print "

".$ListItem->{"name"}."

\n"; + print $ListItem->{"description"}; + print "
\n"; + my @table=( + {"key"=>qr(^download\b.*),"text"=>sub ($) { + $_[0]=~s/^download//; + $_[0]=~s/^-/ /; + return "Download".$_[0]; + }, + "format"=>sub ($) { + my $r; + if ($_[0]=~m#^[a-z]+://#) { + $r="".CGI::escapeHTML($_[0]).""; + } + else { + $r="".CGI::escapeHTML(basename($_[0])).""; + my $size=(stat $_[0])[7]; + die "Cannot stat \"".$_[0]."\": $!" if !defined $size; + if ($size>=1024*1024) { $size=int($size/(1024*1024))." MB"; } + elsif ($size>=1024 ) { $size=int($size/(1024 ))." KB"; } + else { $size=int($size )." B"; } + $r.=" ($size)"; + } + return $r; + }}, + {"key"=>qr(^link\b.*),"text"=>sub ($) { + $_[0]=~s/^link-//; + return $_[0]; + }, + "format"=>sub ($) { + return "".CGI::escapeHTML($_[0]).""; + }}, + {"key"=>"summary","text"=>"Summary"}, + {"key"=>"ownership","text"=>"Ownership"}, + {"key"=>"license","text"=>"License","format"=>sub ($) { + my %known=( + "PD"=>"Public Domain", + "GPL"=>"GNU General Public License", + "LGPL"=>"GNU Lesser General Public License", + ); + return $known{$_[0]}; + }}, + {"key"=>"maintenance","text"=>"Currently maintained?","format"=>sub ($) { + my %known=( + "finished"=>"Project is finished. No serious bugs known. No new features planned.", + "dead"=>"Project became dead code. Some updates may be needed. It is no longer used.", + ); + return $known{$_[0]}; + }}, + {"key"=>"language","text"=>"Programming language","format"=>sub ($) { + return "".CGI::escapeHTML($_[0])."" + if $_[0]=~/^Java\b/; + return "".CGI::escapeHTML($_[0])."" + if $_[0]=~/^PHP\b/; + return undef(); + }}, + ); + print ''."\n"; + +sub tableit_func +{ +my($tableit,$val,$key)=@_; + + print ""; + if ($tableit->{"format"}) { + my $format=$tableit->{"format"}; + my $valn=&$format($val); + $val=$valn if defined $valn; + } + print "\n"; +} + + for my $tableit (@table) { + if (!ref $tableit->{"key"}) { + tableit_func($tableit,$ListItem->{$tableit->{"key"}},$tableit->{"key"}) if $ListItem->{$tableit->{"key"}}; + } + else { + for my $key (keys(%$ListItem)) { + my $keyregex=$tableit->{"key"}; + next if $key!~/$keyregex/; + tableit_func($tableit,$ListItem->{$key},$key); + } + } + } + print "
"; + if (!ref $tableit->{"text"}) { + print $tableit->{"text"}; + } + else { + my $textfunc=$tableit->{"text"}; + print &$textfunc($key); + } + print ":$val
\n"; +} + +sub fatal (;$) +{ +my($msg)=@_; + + $msg="UNKNOWN" if !$msg; + +# heading(false/*title*/,false/*indexme*/); // notitle is always safe, don't index the error message + print("\n

 
 


FATAL ERROR: $msg!

\n" + ."

You can report this problem's details to" + ." admin of this website.

\n"); +# footer(); +} + +my $footer_passed; +sub footer (;$) +{ +my($delimit)=@_; + + $delimit=1 if !defined $delimit; + + exit(1) if $footer_passed++; # deadlock prevention: + + print "

 

\n" if $delimit; + print "
\n

$cvs_id_html

\n"; + print "\n"; + exit(0); +} + +my $heading_done; + +my %headers; +my %headers_lc; # maps lc($headers_key)=>$headers_key +sub header (%) +{ +my(%pairs)=@_; + + while (my($key,$val)=each(%pairs)) { + do { warn "Headers already sent"; next; } if $heading_done; + for ($headers_lc{lc $key} || ()) { + delete $headers{$_}; + } + $headers_lc{lc $key}=$key; + $headers{$key}=$val; + } +} + +sub img_size ($$) +{ +my($width,$height)=@_; + + return((1 #$have_style TODO:dyn + ? "style=\"border:0;width:${width}px;height:${height}px\"" : "border=\"0\"") + ." width=\"$width\" height=\"$height\""); +} + +sub img ($$;$) +{ +my($file,$alt,$attrs)=@_; + + (my $file_det=$file)=~s/[.]mng$/.gif/; + my($width,$height)=Image::Size::imgsize($file_det); + $alt=CGI::escapeHTML($alt); + return("\"$alt\""); +} + + +sub heading (;$$) +{ +my($class,$showtitle,$indexme)=@_; + + $showtitle=1 if !defined $showtitle; + $indexme=1 if !defined $indexme; + + # $ENV{"CLIENT_CHARSET"} ignored (mod_czech support dropped!) + my $client_charset=$Args{"force_charset"} || "us-ascii"; + header("Content-type"=>"text/html; charset=$client_charset"); + header("Content-Style-Type"=>"text/css"); + + if ($ENV{"SERVER_SOFTWARE"}) { + while (my($key,$val)=each(%headers)) { + print "$key: $val\n"; + } + print "\n"; + } + + return if $heading_done++; + + if (1) { # || !$msie_major || $msie_major>=4) # TODO:dyn + print ''."\n"; + } + print ''."\n"; + print ''."\n"; + print ''.CGI::escapeHTML($WebConfig{"title_prefix"}) + .join("",map({ ': '.CGI::escapeHTML($_); } ($WebConfig{"title"} || ()))) + .''."\n"; + + if (1) { # || $have_css) # TODO:dyn + print <<'HERE'; +\n"; + } + + print ''."\n"; + print $_ for ($WebConfig{"head"} || ()); + print "\n"; +# if ($showtitle) +# print("

" +# ."Energie & Peníze") +# ."

\n"); +} + +1;