# $Header$
#
# FILE:    ResumeToText.pm
# AUTHORS: Erik Nygren (nygren@mit.edu)
#
# Converts a resume from XML into ascii text.
#

require 5.002;

use strict;

require XML::Element;


use vars qw/ $pagewidth /;
$pagewidth = 71;




# Returns a string containing formatted version of <when>...</when>
sub formatWhen {
    my ($when) = @_;
    if ($when->getEl("when.from")) {
	return $when->getElText("when.from") . "-" 
	    . $when->getElText("when.to");
    } else {
	return $when->getText();
    }
}

# Returns a string containing formatted version of <par>...</par>
sub formatPar {
    my ($par) = @_;
    my $s = "";
    foreach ($par->getKids) {
	if (ref $_) {
	    if ($_->getTag eq "cite") {
		$s .= ($_->getText);         # do nothing to italic text
	    } else {
		die "unknown tag in formatPar\n";
	    }
	} else {
	    $s .= $_;
	}
    }
    $s =~ s/\s+/ /g;
    $s =~ s/(\S\S\S)\. /\1.  /g;   # put two spaces between sentences
    $s =~ s/^\s*//g;
    $s =~ s/\s*$//g;
    return $s;
}


# Formats two strings onto a line such that one is right justified
# and the other is left-justified...
sub formatLeftRight {
    my ($ls, $rs) = @_;

    my $s = $ls;
    $s .= " "x( $pagewidth - length($rs) - length($ls) );
    $s .= $rs . "\n" ;
    return $s;
}

# Formats a section header line (underlined text)
sub formatSectHead {
    my ($in) = @_;
    my $s = $in;
    $s .= "\n" ;
    $s .= "-"x(length($in));
    $s .= "\n";
    return $s;
}






# Takes in a file handle and a resume XML Element and 
# writes ASCII text out to the file...
sub ResumeToText {
    my ($fh, $resume) = @_;
    my $si = $resume->getEl("subjectinfo");
    my @sects = $resume->getEls("section");

    my $addr = $si->getEl("address");
    my $fmname = $si->getEl("name")->getText();
    my $fmemail = $si->getElText("email");
    my $fmhomepage = $si->getElText("homepage");
    my $fmphone = $si->getElText("phone");
    my $fmstreet = $addr->getElText("street");
    my $fmcitystatezip = $addr->getElText("city") . ", " 
	. $addr->getElText("state"), "  "
	    . $addr->getElText("zipcode");

format FMHEADER = 


@||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
           $fmname

@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     $fmemail, $fmstreet
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     $fmphone, $fmcitystatezip

@||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
           $fmhomepage

.

    *{$fh}->format_name("FMHEADER");
    write $fh;

    my $sect;
    for $sect (@sects) {

	print $fh "\n", formatSectHead($sect->getElText("sectionname"));

	my $block;
	for $block ($sect->getEls("block")) {

	    if ($block->getEl("location") && ($block->getEl("position"))) {
		print $fh "\n", $block->getElText("location") , "\n";
		print $fh formatLeftRight($block->getElText("position")
					  , formatWhen($block->getEl("when")));
	    } elsif ($block->getEl("location")) {
		print $fh "\n", formatLeftRight($block->getElText("location")
					  , formatWhen($block->getEl("when")));
	    }

	    my $par;
	    foreach $par ($block->getEls("par")) {

		my $fmpar = formatPar($par);

		format FMPAR = 
    
~~  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                $fmpar
.
                *{$fh}->format_name("FMPAR");
                write $fh;                
	    }
            print $fh "\n";

	}

    }
    print $fh "\n\n\n";

}


use vars qw/ $infn $outfn $ptree $resume /;

$infn = $ARGV[0];
$outfn = $ARGV[1];

$ptree = XML::Element->parseFromFile($infn);

$resume = $ptree->getEl("resume");
#$resume->printDebug;
die "resume not found in file\n" unless ($resume);

ResumeToText(\*STDOUT, $resume);
