cal2html_k93

- Translates the calendar output from the Unix "cal" command into HTML




#!/usr/bin/ksh93
################################################################
function usagemsg_cal2html_k93 {
  print -u 2 "
Program: cal2html_k93

The cal2html_k93 Korn Shell command/function transforms the standard
text calendar generated by the Unix "cal" command into an HTML
document for a single month or an entire year

Usage: ${1##*/} [-l [-u URLPRE]]  [-m MONTH] [-y YEAR] [-Y]
                    [-v] [-V] [-?]

    Where:
           -l        = Generate an HTML link for each day of the month.
           -u URLPRE = Generate an HTML link for each day of the month
                       and associate a URL prefix with each link.
           -m MONTH  = Month number to generate calendar (1-12).
           -y YEAR   = 4 digit year to generate calendar.
           -Y        = Generate calendar for an entire year.
           -v        = Verbose mode.
           -V        = Very Verbose Mode.

Author: Dana French (dfrench@mtxia.com)    Copyright 2004

\"AutoContent\" enabled"
return 0
}
################################################################
#### 
#### Description:
#### 
#### The cal2html_k93 Korn Shell command/function transforms
#### the standard text calendar generated by the Unix "cal"
#### command into an HTML document for a single month or an
#### entire year
#### 
#### Assumptions:
#### 
#### Dependencies:
#### 
#### Products:
#### 
#### Configured Usage:
#### 
#### Details:
#### 
################################################################
function cal2html_k93 {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset URLPRE=""
  typeset DATESTAMP=$( date +"%Y%m%d" )
  typeset MKLINK="${FALSE}"
  typeset YEAR="${DATESTAMP:0:4}"
  typeset YEAROPT="${FALSE}"
  typeset MON="${DATESTAMP:4:2}"
  typeset MONOPT="${FALSE}"
  typeset FULLYEAR="${FALSE}"

  while getopts ":lu:m#y#YvV" OPTION
  do
      case "${OPTION}" in
          'l') MKLINK="${TRUE}";;
          'u') URLPRE="${OPTARG}"
               MKLINK="${TRUE}";;
          'm') MON="${OPTARG}"
               MONOPT="${TRUE}";;
          'y') YEAR="${OPTARG}"
               YEAROPT="${TRUE}";;
          'Y') FULLYEAR="${TRUE}";;
          'v') VERBOSE="${TRUE}";;
          'V') VERYVERB="${TRUE}";;
          '?') usagemsg_cal2html_k93 "${0}" && return 1;;
          ':') print -u 2 "ERROR: Required option argument not specified"
               usagemsg_cal2html_k93 "${0}" && return 2;;
      esac
  done
   
  shift $(( ${OPTIND} - 1 ))
  
  (( VERYVERB == TRUE )) && set -x
  
  trap 'usagemsg_cal2html_k93 ${0}' EXIT

  if (( FULLYEAR == TRUE )) && (( MONOPT == TRUE ))
  then
      print -u 2 "ERROR: The full year option '-Y' is mutually exclusive with
the month option '-m'.  You may specify month or full year, but not both."
      return 3
  fi

  trap '-' EXIT
  
################################################################

  if (( FULLYEAR == TRUE ))
  then
      yearHtmlCal_k93 "${MON}" "${YEAR}" "${MKLINK}" "${URLPRE}"
  else
      monthHtmlCal_k93 "${MON}" "${YEAR}" "${MKLINK}" "${URLPRE}"
  fi

  return 0
}
################################################################
function monthHtmlCal_k93 {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset MON="${1:?ERROR: ${0}: Month not specified}"
  typeset YEAR="${2:?ERROR: ${0}: Year not specified}"
  typeset MKLINK="${3}"
  typeset URLPRE="${4}"
  typeset LCNT
  typeset LINE
  typeset -Z2 MON2="${MON}"

  print "<TABLE Border=\"1\">"
  LCNT="0"

#### Generate a calendar for the specified month and
#### year and read each line of the calendar.

  cal ${MON} ${YEAR} | while IFS="" read LINE
  do

#### Keep track of the line number being processed
#### because the first two lines of the calendar have a
#### different format than the rest of the lines, and must be
#### processed differently.

    (( ++LCNT ))

#### If it is the first line of the calendar, then it
#### contains the month name and year, and should span the
#### entire width of the calendar.

    (( LCNT == 1 )) &&
      print "<TR><TH Colspan=\"7\" Align=\"center\" Valign=\"middle\">${LINE}</TH></TR>" &&
      continue

#### If it is the second line of the calendar, then it
#### contains the day of the week identifiers, and should be
#### displayed as column headers.

    (( LCNT == 2 )) &&
      print "<TR><TH>Su</TH><TH>Mo</TH><TH>Tu</TH><TH>We</TH><TH>Th</TH><TH>Fr</TH><TH>Sa</TH></TR>" &&
      continue

#### All the rest of the lines of the calendar contain day
#### numbers for each day of the month.

    if (( LCNT >= 3 ))
    then
      LINE="${LINE//   /<TD> </TD>}"

#### If the command line option to generate HTML links was
#### specified, then insert the HTML code to create a link
#### for each day of the month.  The link will minimally
#### consist of "YYYYMMDD.html"

      if (( MKLINK == TRUE ))
      then
        LINE="${LINE//([[:digit:]][[:digit:]])/<TD Align=\"right\"><A Href=\"${URLPRE}${YEAR}${MON2}\1.html\">\1</A></TD>}"
        LINE="${LINE// ([[:digit:]])/<TD Align=\"right\"><A Href=\"${URLPRE}${YEAR}${MON2}0\1.html\">\1</A></TD>}"
      else
        LINE="${LINE//([[:digit:]][[:digit:]])/<TD Align=\"right\">\1</TD>}"
        LINE="${LINE// ([[:digit:]])/<TD Align=\"right\">\1</TD>}"
      fi
      print "<TR>${LINE}</TR>"
    fi
  done
  print "</TABLE>"
  return 0
}
################################################################
function yearHtmlCal_k93 {
  typeset MON="${1:?ERROR: ${0}: Month not specified}"
  typeset YEAR="${2:?ERROR: ${0}: Year not specified}"
  typeset MKLINK="${3}"
  typeset URLPRE="${4}"
  typeset CNT
  typeset COL
  typeset ROW

  print "<TABLE>"
  CNT=0
  for ROW in 1 2 3 4
  do
    print "<TR>"
    for COL in 1 2 3
    do
      (( ++CNT ))
      print "<TD Align=\"center\" Valign=\"top\">"
      monthHtmlCal_k93 "${CNT}" "${YEAR}" "${MKLINK}" "${URLPRE}"
      print "</TD>"
    done
    print "</TR>"
  done
  print "</TABLE>"
  return 0
}
################################################################

cal2html_k93 "${@}"