Greetings fellow monks.
I try to keep my CV vaguely up to date, and available in a few formats. I've always had problems keeping the various formats in sync, though.
I found that there was already
a project addressing the issue, so all I had to do was write a perl replacement for the java thingy that runs the XML through XSLT to generate
my various CVs.
Of course, the problem now is that writing a CV is rather less fun than writing the code that writes it ;-)
I hope some of you find it helpful!
#!/usr/bin/perl -w
# xmlcv.pl: perl thing for transforming xml versions of a curriculum v
+itae
# into various formats, using the xml resume library from
# http://xmlresume.sourceforge.net/
#
# Also supports use Gary O'Sullivans openoffice patch:
# http://sourceforge.net/tracker/index.php?func=detail&aid=981727&grou
+p_id=29512&atid=396337
#
# v.0.1
#
# Copyright (C)2005 Charlie Harvey
#
# Licensed GPL, see: http://www.gnu.org/copyleft/gpl.html
use strict;
use XML::LibXSLT;
use XML::LibXML;
# Tweak these settings!
# ---------------------
my @TARGETS = ();
# If you need pdf versions of your cv/resume, you'll want to install F
+OP
# from:
# http://xmlgraphics.apache.org/fop/
# $PATH_TO_FOP is the filepath to the fop.sh script.
my $PATH_TO_FOP ='/usr/bin/fop';
# $PATH_TO_RESUME is the path to the xmlresume xsl directory on your
# machine
my $PATH_TO_XML_RESUME = './lib' ;
# $PATH_TO_OO_TEMPLATE is the path to your template OpenOffice sxw fil
+e, if you're
# genereating an sxw of your cv.
my $PATH_TO_OO_TEMPLATE = "$PATH_TO_XML_RESUME/charlie.sxw";
# $PATH_TO_OO_OUTPUT is the name you want your sxw output file to be c
+alled if you're
# creating an sxw. You need to set the output file name below to conte
+nt.xml
my $PATH_TO_OO_OUTPUT = "./charlie_harvey_cv.sxw";
# %FORMATS is a hash of output filenames,
# keyed on xsl filenames (from the $PATH_TO_RESUME/xsl/output director
+y)
# Nb:
# - To create pdfs make sure your output filename ends in .fo, with no
+ whitespace
# afterwards, the output will replace this with .pdf
# - To create openoffice files, make sure your output filename is cont
+ent.xml, the
# resulting file is called cv.sxw, and can be found in the directory y
+ou ran xmlcv.pl
# in. It's based on $PATH_TO_RESUME/cv.sxw, so you'll need a blank oo
+doc there.
my %FORMATS = ( 'uk-html.xsl'=>'charlie_harvey_cv.html', # p
+lain html
'uk-a4.xsl'=>'charlie_harvey_cv_uk_a4.fo', # pdf (a4)
'us-letter.xsl'=>'charlie_harvey_cv_us_letter.fo', # p
+df (letter)
'uk-text.xsl'=>'charlie_harvey_cv.txt', # g
+ood ol' ASCII
'uk-openoffice.xsl' => 'content.xml'); # OOo sxw
# ====== STOP TWEAKING! ======= #
my $USAGE = "Usage: $0 path_to_cv_or_resume";
if (!$ARGV[0]) {die ("$USAGE\n");}
my ($cv) = shift;
my $xslt = XML::LibXSLT->new();
my $parser = XML::LibXML->new();
# remove tags with contexts not in @TARGETS
sub reparse($){
my $doc = shift;
return unless defined $doc;
my @nodelist = $doc->documentElement()->findnodes('*[@targets]');
foreach my $node (@nodelist) {
my @attr = $node->attributes;
foreach (@attr) {
my $count = target_find(split /,/, $_->getValue());
unless ($count > 0) {
$node->unbindNode();
}
}
}
return $doc;
}
# find how many of our targets are contained in the targets attribute
sub target_find(@) {
my @targs = @_;
my $count = 0;
foreach my $targ (@targs){
$count += grep (/$targ/, @TARGETS);
}
return $count;
}
foreach (keys %FORMATS) {
print "Formatting $FORMATS{$_} using stylesheet $_\n";
my $xslfile = "$PATH_TO_XML_RESUME/xsl/output/$_";
my $xmlfile = "$cv";
my $source = $parser->parse_file($xmlfile);
my $doc = reparse($source);
my $style_doc = $parser->parse_file($xslfile);
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($doc);
open (OUT, ">$FORMATS{$_}") || die ("File error processing $cv: $!\n
+");
print OUT $stylesheet->output_string($results);
close OUT;
if ($FORMATS{$_}=~/\.fo$/) {
my $pdf_name = $FORMATS{$_};
$pdf_name =~ s/\.fo$/\.pdf/;
print "Creating $pdf_name from $FORMATS{$_}\n";
system ("$PATH_TO_FOP $FORMATS{$_} $pdf_name")==0 || die("FOP erro
+r encountered\n");
print "PDF named $pdf_name created from $FORMATS{$_}\n"
}
if ($FORMATS{$_}=~/content\.xml/) {
system ("cp $PATH_TO_OO_TEMPLATE $PATH_TO_OO_OUTPUT")==0 || die ("
+OpenOffice template file issue\n");
system ("zip -u $PATH_TO_OO_OUTPUT $FORMATS{$_}")==0 || die ("Open
+Office zip issue\n");
print "OpenOffice file named $PATH_TO_OO_OUTPUT based on $PATH_TO_
+OO_TEMPLATE created using $FORMATS{$_}\n"
}
}
print "Done\n";