# $Header$
#
# FILE:    ResumeToPlainHTML.pm
# AUTHORS: Erik Nygren (nygren@mit.edu)
#
# Converts a resume from XML into plain HTML.
#

require 5.002;

use strict;

require XML::Element;


# Returns a string containing HTML 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 HTML formatted version of <par>...</par>
sub formatPar {
    my ($par) = @_;
    my $s = "";
    foreach ($par->getKids) {
	if (ref $_) {
	    if ($_->getTag eq "cite") {
		$s .= "{\\em ".($_->getText)."}";
	    } else {
		die "unknown tag in formatPar\n";
	    }
	} else {
	    s/^\s*//;
	    if ($_) { $_ .= " "; }
	    $s .= $_ ;
	}
    }
    return $s;
}

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

    print $fh <<_EOFPRELUDE_
\\documentstyle{article}
\\pagestyle{empty}		% no page numbers, thanks.
\\addtolength{\\topmargin}{-5pc}	% repairing LaTeX's huge margins...
\\addtolength{\\textwidth}{.5in}	% same here....
\\setlength{\\parindent}{0mm}	
\\setlength{\\textheight}{9.1in}	% more margin hacking.  I have a large
			    	% resume to fit on one page.


\\begin{document}

\\reversemarginpar		% this puts the margin notes on the
				% left-hand side of the resume.

% This turns off hyphenation...
%\\hyphenpenalty=10000 \\exhyphenpenalty=10000
\\hyphenpenalty=1500 \\exhyphenpenalty=1500

_EOFPRELUDE_
;

    print $fh "\\begin{center}\\Large{\\bf ", $sname, "}\\end{center}\n";


    my $addr = $si->getEl("address");
    my $cityline = $addr->getElText("city").", "
	. $addr->getElText("state"). "  "
	    . $addr->getElText("zipcode");

    print $fh "\\vspace{\\baselineskip}\n\\par\n";
    print $fh $si->getElText("email"), "\\hspace*{\\fill}";
    print $fh $addr->getElText("street"), "\n\\linebreak\n";
    print $fh $si->getElText("phone"), "\\hspace*{\\fill}";
    print $fh $cityline, "\n\\linebreak\n";

    print $fh "\\vspace{-12pt}\n";

    print $fh "\\begin{center}", $si->getElText("homepage"), "\\end{center}\n";

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

        print $fh "\n\\par\n";

        print $fh "\\vspace{.6\\baselineskip}\n\n";

        my $sectname = $sect->getElText("sectionname");
        print $fh "\\hyphenation{ ", $sectname," }\n";
        # special-case kludge (ewww)
        if ($sectname eq "Papers and Publications") {
           $sectname = "Papers and \\par Publications";
        }
	print $fh "\\makebox[0pt]{}\\marginpar{{\\bf "
                    , $sectname, "}}";
        # important that there is no linefeed here...
	my $block;
	for $block ($sect->getEls("block")) {

	    # format the block headers, either:
	    # 
	    # location
	    # position        date
	    #
	    # or
	    #
	    # location        date	    
	    #
	    if ($block->getEl("location") && ($block->getEl("position"))) {
		print $fh "{\\bf "
		    ,$block->getElText("location"),
		    "}\n\\par\n\\nopagebreak\n";
		print $fh "{\\em ", $block->getElText("position") , "}" ;
		print $fh " \\hspace*{\\fill}"
                          ,formatWhen($block->getEl("when"))
		    if $block->getEl("when");
		print $fh "\n\\par\n\\nopagebreak\n";		
	    } elsif ($block->getEl("location")) {
		print $fh "{\\bf " ,$block->getElText("location"), "} ";
		print $fh " \\hspace*{\\fill}",
                    formatWhen($block->getEl("when"))
		    if $block->getEl("when");
		print $fh "\n\\par\n\\nopagebreak\n";		
	    }


	    my $par;
	    foreach $par ($block->getEls("par")) {
		print $fh "" , formatPar($par), "\n";
                print $fh "\\par\n\\vspace{.6\\baselineskip}\n\n";
	    }
	    print $fh "";	    
	}
	print $fh "";
    }

    print $fh "\\end{document}\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);

ResumeToLatex(\*STDOUT, $resume);
