#!/usr/local/bin/perl -w # # phone2html # # Build HTML pages for each of the phone lists. # use strict; use vars qw(%FieldNames @FieldOrder $Home %Lists $PerLine %PhoneBook); $Home = (getpwuid($<))[7]; $PerLine = 2; # Entries per line %FieldNames = ( "Address1" => "Address", "Phone" => "Phone", "WorkPhone" => "Work Phone", "CellPhone" => "Cell Phone", "Fax" => "Fax", "Pager" => "Pager", "E-mail" => "E-mail", "WorksAt" => "Works At", "Anniversary" => "Anniversary", "Birthday" => "Birthday", "Comment1" => "Comment", ); @FieldOrder = ( "ResidenceName", "Address1", "Address2", "WorksAt", "Phone", "WorkPhone", "CellPhone", "Fax", "Pager", "E-mail", "Anniversary", "Birthday", "Comment1", "Comment2", ); ### ### Main program ### ### Read the phone book in. After it is completely read, ### produce alphabetized pages for each list specified. ### Produce an index page for them all. ### my($field, %info, $list, $name, $td, $title); ReadPhoneBook(); mkdir("$Home/public_html/phone", 0755); open(INDEX, ">$Home/public_html/phone/index.wc") || die "Cannot open $Home/public_html/phone/index.wc: $!"; print INDEX "# Do not edit this file\n"; print INDEX "#include defs.wh\n"; print INDEX "#define _PAGE-TITLE_ Index of phone lists\n"; print INDEX "#define _TITLE-HEADER-TEXT_ Index of phone lists\n"; print INDEX "#include head.wh\n"; print INDEX "#include index.head.wh\n"; for $list (sort(keys %Lists)) { $list = lc($list); print INDEX "
  • $list\n"; open(LIST, ">$Home/public_html/phone/$list.wc") || die "Cannot open $Home/public_html/phone/$list.wc: $!"; print LIST "# Do not edit this file\n"; print LIST "#include defs.wh\n"; print LIST "#define _PAGE-TITLE_ Phone list $list\n"; print LIST "#define _TITLE-HEADER-TEXT_ Phone list $list\n"; print LIST "#include head2.wh\n"; print LIST "#include list.head.wh\n"; $td = 0; print LIST qq|\n|; for $name (sort keys(%PhoneBook)) { %info = map { split(/=/, $_, 2) } split(/\n/, $PhoneBook{$name}); next unless ($info{'Lists'} =~ /\b$list\b/i); $info{'ResidenceName'} = $info{'Name'} if (!defined($info{'ResidenceName'}) || $info{'ResidenceName'} eq ""); print LIST "" if ($td == 0); print LIST qq|\n"; if (++$td >= $PerLine) { print LIST "\n"; $td = 0; } } if ($td > 0) { while (++$td <= $PerLine) { print LIST "\n"; } print LIST "\n"; } print LIST "
    \n|; print LIST "\n"; for $field (@FieldOrder) { next unless defined($info{$field}); $title = $FieldNames{$field} || "
    "; $title .= ":" if ($title ne "
    "); print LIST qq|| . qq|\n|; } print LIST "
    $title$info{$field}
    \n"; print LIST "

    \n"; print LIST "#include list.tail.wh\n"; print LIST "#include tail2.wh\n"; close LIST; } print INDEX "#include index.tail.wh\n"; print INDEX "#include tail.wh\n"; close INDEX; chdir("$Home/public_html/phone"); system("webc *.wc"); ### ### ReadPhoneBook ### ### Read the phone book into the PhoneBook hash. ### sub ReadPhoneBook { my(%info, $item, $line, @lists, $name, $value); open(PHONE, "<$Home/Notes/phone.dat") || die "Cannot open $Home/Notes/phone.dat: $!"; $info{'Lists'} = "NORMAL"; for ($line = 1; ; $line++) { chomp; next if (/^\s*#/ || /^\s*$/); if (/^%%/) { next if (scalar(keys(%info)) == 1); $value = ""; for $name (sort(keys %info)) { $value .= "$name=$info{$name}\n"; } $name = $info{'SortAs'} || $info{'Name'} || die "No name at $line"; $PhoneBook{$name} = $value; undef %info; $info{'Lists'} = "NORMAL"; } else { ($name, $value) = split(/\s*=\s*/, $_, 2); next unless defined($value); $info{$name} = $value; if ($name eq "Lists") { @lists = split(/\s+/, $value); for $item (@lists) { $Lists{$item} = 1; } } } } close PHONE; }