#!/usr/bin/perl -w
use strict;
use FindBin;
use lib "$FindBin::RealBin/../lib";
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;
use LaTeXML;
use LaTeXML::Post;

#**********************************************************************
# Parse command line.

my $identity = "latexmlpost (LaTeXML version $LaTeXML::VERSION)";
my($source,$destination)=(".","");
my($help,$showversion)=(0,0);
my $format=undef;
my($mathml,$openmath,$keepXMath);
my $verbosity=0;
my $stylesheet;
GetOptions("destination=s"=>\$destination,
	   "source=s"     =>\$source,
	   "verbose+"     =>\$verbosity,
	   "stylesheet:s" =>\$stylesheet,
	   "format=s"     =>\$format,
	   "VERSION"      =>\$showversion,
	   "mathml"       =>\$mathml,
	   "openmath"     =>\$openmath,
	   "keepXMath"    =>\$keepXMath,
	   "help|?"       =>\$help,
	  ) or pod2usage(-message => $identity, -exitval=>1, -verbose=>0, -output=>\*STDERR);
pod2usage(-message=>$identity, -exitval=>1, -verbose=>2, -output=>\*STDOUT) if $help;
if($showversion){ print STDERR "$identity\n"; exit(1); }
pod2usage(-message=>"$identity\nMissing input xmlfile", 
	  -exitval=>1, -verbose=>0,-output=>\*STDERR) unless @ARGV;
my $xmlfile = shift(@ARGV);

#**********************************************************************
# Do the processing.
binmode(STDERR,":utf8");
binmode(STDOUT,":utf8");
print STDERR "$identity\n" if $verbosity > 0;

my $doc = LaTeXML::Post->new()->process($xmlfile, 
					sourceDirectory=> $source,
					destination    => $destination,
					format         => $format,
					mathml         => $mathml,
					openmath       => $openmath,
					keepXMath      => $keepXMath,
					stylesheet     => $stylesheet,
					verbosity      => $verbosity,
					toString       => !$destination);

print $doc unless $destination;

#**********************************************************************
__END__

=head1 NAME

C<latexmlpost> postprocesses an xml file generated by latexml for common tasks,
such ad convert math to images and processing graphics inclusions for the web.

=head1 SYNOPSIS

latexmlpost [options] xmlfile

  Options:
   --format=html|xhtml|xml    requests the output format.
   --stylesheet=xslfile       requests the XSL transform of the document using the given
                              xslfile as stylesheet.
   --mathml                   requests conversion of the math to Presentation MathML
   --openmath                 requests conversion of the math to OpenMath
   --keepXMath                preserves the intermediate XMath representation
   --verbose                  prints progress messages during processing.
   --VERSION                  show version number.
   --source=sourcedir         specifies directory of source TeX file.
   --destination=outputfile   specifies output file (and directory).
   --help                     shows help message.

=head1 OPTIONS AND ARGUMENTS

latexmlpost postprocesses an xml file, C<xmlfile>, generated by latexml 
to perform common tasks, such as convert math to images and processing
graphics inclusions for the web.  If C<-> is given for C<xmlfile>, the
xml data is read from standard input.

=over 4

=item B<--format>=I<html|xhtml|xml>

Specifies the output format for post processing. 
html format converts the material to html and the mathematics to png images.
xhtml format converts to xhtml and uses presentation MathML (after attempting
to parse the mathematics) for representing the math.  In both cases, any
graphics will be converted to web-friendly formats and/or copied to the
destination directory.  By default, the output is left in LaTeXML's xml,
but the math is parsed and converted to presentation MathML.
For html and xhtml, a default stylesheet is provided, but see
the B<--stylesheet> option.

=item B<--stylesheet>=I<stylesheet>

Specifies an XSLT stylesheet for transforming the xml.

=item B<--mathml>

Requests that the mathematical material be parsed, and converted to
Presentation MathML.

=item B<--openmath>

Requests that the mathematical material be parsed, and converted to
OpenMath.  This is currently a very naive conversion.

=item B<--keepXMath>

By default, when either --mathml

=item B<--source>=I<source>

Specifies the directory where the original latex source is located.
Unless latexmlpost is run from that directory, it may be necessary
to specify this option in order to find graphics and style files.

=item B<--destination=>I<destination>

Specifies the destination file and directory.  The directory is needed for
mathimages and graphics processing.


=item B<--stylesheet>=I<xslfile>

Requests the XSL transformation of the document using the given xslfile as stylesheet.
If the stylesheet is omitted, a `standard' one will be used (C<dtd/LaTeXML.xsl>).

=item B<--VERSION>

Shows the version number of the LaTeXML package..

=item B<--help>

Shows this help message.

=back

=head1 SEE ALSO

L<latexml>, L<LaTeXML>

=cut
#**********************************************************************

