Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Something simpler than HTML::Calendar::Simple

by oakbox (Chaplain)
on Jun 23, 2003 at 14:27 UTC ( [id://268176]=perlcraft: print w/replies, xml ) Need Help??

   1: #!/usr/bin/perl
   2: 
   3: # I wanted something VERY SIMPLE for generating an
   4: # HTML calendar.  I didn't want to wade through the interface
   5: # for  HTML::Calendar::Simple, or worry about Entities.
   6: 
   7: # This script looks at the time, backtracks to the first of
   8: # the month, and then prints out an HTML calendar for this 
   9: # month.  You can manipulate the month being printed by fiddling
  10: # with the $now variable and you can put information into the 
  11: # calendar easily where commented.
  12: 
  13: # Oakbox Productions - Richard Still (oakbox)
  14: 
  15: use strict;
  16: 
  17: my $message;  # variable to hold output
  18: 
  19: my $now = time;
  20: 
  21: my @wday = localtime($now);
  22: 
  23: my %dayrev = (  "0" => "Sun",
  24: 	        "1" => "Mon",
  25: 		"2" => "Tue",
  26: 		"3" => "Wed",
  27: 		"4" => "Thu",
  28: 		"5" => "Fri",
  29: 		"6" => "Sat");
  30: 
  31: my %monrev = (  "0" => "Jan",
  32: 	        "1" => "Feb",
  33: 		"2" => "Mar",
  34: 		"3" => "Apr",
  35: 		"4" => "May",
  36: 		"5" => "Jun",
  37: 		"6" => "Jul",
  38: 		"7" => "Aug",
  39: 		"8" => "Sep",
  40: 		"9" => "Oct",
  41: 		"10" => "Nov",
  42: 		"11" => "Dec");
  43: 
  44: 
  45: use Time::Local;
  46: 
  47: $message.=qq(<span class="big"> $monrev{$wday[4]} </span>
  48:         <br> <table border="1" cellspacing="0" cellpadding"3" width="100%">
  49:                <tr bgcolor="#679cd3" class="big">
  50:                  <td align="center"> $dayrev{0} </td>
  51:                  <td align="center"> $dayrev{1} </td>
  52:                  <td align="center"> $dayrev{2} </td>
  53:                  <td align="center"> $dayrev{3} </td>
  54:                  <td align="center"> $dayrev{4} </td>
  55:                  <td align="center"> $dayrev{5} </td>
  56:                  <td align="center"> $dayrev{6} </td>
  57:                </tr>);
  58: 
  59: # I have to move the start date a little bit to get Sunday
  60: # over to the first position
  61: 
  62: my $fday = timelocal(0,0,0,1,$wday[4],$wday[5]);
  63: my @ltime = localtime($fday);
  64: if($ltime[6] ne "0"){
  65:     $message.=qq(<tr>);
  66: 
  67:   foreach my $cl (0...($ltime[6] - 1)){
  68:     $message.=qq(<td> &nbsp; </td> );
  69:   }
  70: }else{
  71: 
  72:     $message.=qq(<tr>);
  73: 
  74: }
  75: 
  76: my $endm;
  77: 
  78: foreach my $daycount (1...31){
  79:    my $thisday;
  80:    eval {   $thisday = timelocal(0,0,0,$daycount,$wday[4],$wday[5]);  }; 
  81:    if( $@ ){ next; }
  82:    my @ltime = localtime($thisday);
  83:    $endm = $ltime[6]; # signal to next section about what day we ended on
  84: 
  85:    my $color = qq();
  86: 
  87: ## This is where you want to put stuff INTO your calendar
  88: ## but that's optional :)
  89: 
  90:    $message.=qq(<td $color> $daycount<p> &nbsp; </td>\n);
  91: 
  92:    if($ltime[6] eq "6"){ $message.=qq(</tr><tr>\n); }
  93: }
  94: 
  95: # close up the table by filling in any missing days
  96: 
  97: if($endm ne "6"){
  98:   foreach my $cl (($endm+1)...6){
  99:     $message.=qq(<td> &nbsp; </td> );
 100:   }
 101: }
 102:     $message.=qq(</tr></table>);
 103: 
 104: 
 105: # little html out template
 106: 
 107:  my $html_frame=qq(<html>
 108: 			<head>
 109: <style type="text/css">
 110: <!--
 111: td, body, p {  font-family: Arial, Helvetica, sans-serif; font-size: 12px}
 112: .big {  font-family: Arial, Helvetica, sans-serif; font-size: 14px}
 113: -->
 114: </style>
 115: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 116: </head>
 117: 
 118: <body bgcolor="#FFFFFF" text="#000000">
 119: <p>$message</p>
 120: </body>
 121: </html>);
 122: 	
 123: print "Content-type: text/html\n\n";
 124: print "$html_frame";
 125: 
 126: exit;

Replies are listed 'Best First'.
(jeffa) Re: Something simpler than HTML::Calendar::Simple
by jeffa (Bishop) on Jun 24, 2003 at 01:46 UTC
    Step one in making your own HTML calendar:
    1. use Calendar::Simple;
    Here is a slimmer version of what i posted a while ago at YAHC (Yet Another HTML Calendar).
    use POSIX; use CGI qw(:standard); use Calendar::Simple; use DBIx::XHTML_Table; print header, start_html('simple calendar'); print DBIx::XHTML_Table ->new([calendar],[qw(Su Mo Tu We Th Fr Sa)]) ->modify(table => {border=>1}) ->modify(caption => strftime("%B %Y",localtime)) ->output ; print end_html;
    But using my own module is probably cheating. I guess my point is that even though you don't like a certain wheel, you shouldn't dismiss it's core wheel as well. Calendar::Simple simply rocks. davorg++ yet again. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Something simpler than HTML::Calendar::Simple
by jmcnamara (Monsignor) on Jun 23, 2003 at 15:39 UTC

    Here is something even simpler. Possibly too simple:
    #!/usr/bin/perl -wl use strict; # Get the month using cal my @cal = `cal @ARGV`; my $date = shift @cal; $date =~ s/^\s+//; $date =~ s/\s+$//; shift @cal; # Ignore short day names pop @cal; # Ignore blank row unshift @cal, 'Sun Mon Tue Wed Thu Fri Sat'; # Add days # Some simple Html formatting print '<html>'; print '<head>'; print '<center>'; print '<table border="1">'; print "\t<tr>\n\t\t<td colspan=\"7\">" . "<center>$date</center></td>\t\n</tr>"; # Print each day as a <td> element for (@cal) { print "\t<tr>"; my @days = split ' '; # Left or right pad short weeks if (@days < 7) { if ($days[0] == 1) { unshift @days, ('&nbsp;') x (7 -@days); } else { push @days, ('&nbsp;') x (7 -@days); } } print "\t\t<td>$_</td>" for @days; print "\t</tr>"; } print '</table>'; print '</center>'; print '</head>'; print '</html>'; __END__

    It uses the Unix cal utility to generate the months and it takes month and year as optional arguments. Here is some sample output, add formatting to taste:

        perl html_cal.pl

    June 2003
    Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30          

        perl html_cal.pl 5 2003

    May 2003
    Sun Mon Tue Wed Thu Fri Sat
            1 2 3
    4 5 6 7 8 9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29 30 31

    --
    John.

Re: Something simpler than HTML::Calendar::Simple
by Juerd (Abbot) on Jun 27, 2003 at 17:38 UTC

    my %monrev = ( "0" => "Jan", "1" => "Feb", "2" => "Mar", "3" => "Apr", "4" => "May", "5" => "Jun", "6" => "Jul", "7" => "Aug", "8" => "Sep", "9" => "Oct", "10" => "Nov", "11" => "Dec");

    Why a hash?

    my @monrev = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
    Is there something obvious that I don't see?

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      That would be me slapping my head and making a "Doh!" sound.

      It seems that I've gotten locked into a few idioms in my Perl and just keep hammering at them.

      oakbox

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-25 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found