Tuesday, November 21, 2006

Hyperlink "cal" in MediaWiki

I used to provide hyperlink to the dates as a unordered list and users have to do a lot of scrolling in order to locate the actual date.

In UNIX, "cal" provides a calendar output per year with 3 months in a row. The below script is to convert the plain text output of "cal" to MediaWiki hyperlink format. For the Lottery Intelligence case, the dates need to be hyperlinked are Sunday, Wednesday and Saturday.

#! /bin/sh

yr=${1:-`date '+%Y'`}
for i in 1 2 3 4 5 6 7 8 9 10 11 12
do
  cal $i $yr | awk '{printf("%-21s\n",$0)}' |   sed   -e 's/ 1 /01 /' -e 's/ 2 /02 /' -e 's/ 3 /03 /' -e 's/ 4 /04 /'   -e 's/ 5 /05 /' -e 's/ 6 /06 /' -e 's/ 7 /07 /' -e 's/ 8 /08 /'   -e 's/ 9 /09 /' |   nawk -v yr=$yr -v mth=$i 'NR<=2{printf("%s\n",$0)} NR>=3{
    sun=substr($0,1,2)
    if (sun!="  ") {
      sun=sprintf("[[%s-%02d-%s|%s]]",yr,mth,sun,sun)
    }
    mon=substr($0,4,2)
    tue=substr($0,7,2)
    wed=substr($0,10,2)
    if (wed!="  ") {
      wed=sprintf("[[%s-%02d-%s|%s]]",yr,mth,wed,wed)
    }
    thu=substr($0,13,2)
    fri=substr($0,16,2)
    sat=substr($0,19,2)
    if (sat!="  ") {
      sat=sprintf("[[%s-%02d-%s|%s]]",yr,mth,sat,sat)
    }
    printf(" %s %s %s %s %s %s %s\n",sun,mon,tue,wed,thu,fri,sat)
  }' > $i.$yr
done


(paste 1.$yr 2.$yr 3.$yr; echo " "; paste 4.$yr 5.$yr 6.$yr; echo " "; paste 7.$yr 8.$yr 9.$yr; echo " "; paste 10.$yr 11.$yr 12.$yr) | sed -e 's/  /  /g' > $yr


for i in 1 2 3 4 5 6 7 8 9 10 11 12
do
  rm -f $i.$yr
done

Labels: , ,

0 Comments:

Post a Comment

<< Home