###########################################################################
#
# PDFPlug.pm -- reasonably with-it pdf plugin
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999-2001 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

package PDFPlug;

use ConvertToPlug;

sub BEGIN {
    @ISA = ('ConvertToPlug');
}

my $arguments = [ { 'name' => "process_exp",
		    'desc' => "A perl regular expression to match against filenames. Matching filenames will be processed by this plugin. Each plugin has its own default process_exp. e.g HTMLPlug defaults to '(?i)\.html?\$' i.e. all documents ending in .htm or .html (case-insensitive).",
		    'type' => "string",
		    'deft' => q^(?i)\.pdf$^,
		    'reqd' => "no" },
		  { 'name' => "block_exp",
		    'desc' => "Files matching this regular expression will be blocked from being passed to any later plugins in the list. This has no real effect other than to prevent lots of warning messages about input files you don't care about. Each plugin might have a default block_exp. e.g. by default HTMLPlug blocks any files with .gif, .jpg, .jpeg, .png or .css file extensions.",
		    'type' => 'string',
		    'deft' =>  q^^ },
		  { 'name' => "noimages",
		    'desc' =>  "Don't attempt to extract images from PDF.",
		    'type' => "flag" },
		  { 'name' => "complex",
		    'desc' =>  "Create more complex output. With this option set the output html will look much more like the original PDF file. For this to function properly you Ghostscript installed (for *nix gs should be on your path while for windows you must have gswin32c.exe on your path).",
		    'type' => "flag" },
		  { 'name' => "zoom",
		    'desc' =>  "The factor by which to zoomthe PDF for output (this is only useful if -complex is set).",
		    'type' => "int" }
		  ];

my $options = { 'name'     => "PDFPlug",
		'desc'     => "Reasonably with-it pdf plugin.",
		'inherits' => "yes",
		'args'     => $arguments };

sub new {
    my $class = shift (@_);

    my ($noimages, $complex, $zoom, $use_sections);
    
    if (!parsargv::parse(\@_, 
			 q^noimages^, \$noimages,
			 q^complex^, \$complex,
			 q^zoom/\d+/2^, \$zoom,
			 q^use_sections/1?/^, \$use_sections,
			 "allow_extra_options")) {

	print STDERR "\nIncorrect options passed to PDFPlug, check your collect.cfg configuration file\n";
	&print_usage();
	die "\n";
    }


    my @args=@_;
    if ($use_sections) {
	push (@args, "-description_tags");
    }

    # following title_sub removes "Page 1" added by pdftohtml, and a leading
    # "1", which is often the page number at the top of the page. Bad Luck
    # if your document title actually starts with "1 " - is there a better way?

    my $self = new ConvertToPlug ($class, @args, "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');

    if ($use_sections) {
	$self->{'use_sections'}=1;
    }

    # 14-05-02 To allow for proper inheritance of arguments - John Thompson
    my $option_list = $self->{'option_list'};
    push( @{$option_list}, $options );

    # these are passed through to gsConvert.pl by ConvertToPlug.pm
    $self->{'convert_options'} = "-pdf_zoom $zoom";
    $self->{'convert_options'} .= " -pdf_complex" if $complex;
    $self->{'convert_options'} .= " -pdf_ignore_images" if $noimages;

    # pdftohtml will always produce html files encoded as utf-8
    if ($self->{'input_encoding'} eq "auto") {
	$self->{'input_encoding'} = "utf8";
	$self->{'extract_language'} = 1;
    }
    
    return bless $self, $class;
}


sub print_usage {
    print STDERR "\n  usage: plugin PDFPlug [options]\n\n";
    print STDERR "  options:\n";
    print STDERR "   -convert_to (html|text) Convert to TEXT or HTML (default html)\n";
    print STDERR "   -use_sections     Create a separate section for each page\n";
    print STDERR "                     of the PDF file.\n";
    print STDERR "   -noimages         Don't attempt to extract images from PDF.\n";
    print STDERR "   -complex          Create more complex output. With this option\n";
    print STDERR "                     set the output html will look much more like\n";
    print STDERR "                     the original PDF file. For this to function\n";
    print STDERR "                     properly you Ghostscript installed (for *nix\n";
    print STDERR "                     gs should be on your path while for windows\n";
    print STDERR "                     you must have gswin32c.exe on your path).\n";
    print STDERR "   -zoom             The factor by which to zoomthe PDF for output\n";
    print STDERR "                     (this is only useful if -complex is set).\n\n";
}



sub get_default_process_exp {
    my $self = shift (@_);

    return q^(?i)\.pdf$^;
}

# so we don't inherit HTMLPlug's block exp...
sub get_default_block_exp {
    return "";
}
    

# do plugin specific processing of doc_obj for HTML type
sub process {
    my $self = shift (@_);
    if ($self->{'use_sections'}
	&& $self->{'converted_to'} eq "HTML") {

	print STDERR "PDFPlug: Calculating sections...\n";
	my $textref=$_[0];

	# we have "<a name=1></a>" etc for each page
	my @sections = split('<a name=', $$textref);

	shift @sections; # don't need HTML header, etc
	# handle first section specially for title? Or all use first 100...
	
	my $title = $sections[0];
	$title =~ s/^\d+>//; # specific for pdftohtml...
	$title =~ s/<\/([^>]+)><\1>//g; # (eg) </b><b> - no space
	$title =~ s/<[^>]*>/ /g;
	$title =~ s/(?:&nbsp;|\xc2\xa0)/ /g; # utf-8 for nbsp...
	$title =~ s/^\s+//s;
	$title =~ s/\s+$//;
	$title =~ s/\s+/ /gs;
	$title =~ s/^$self->{'title_sub'}// if ($self->{'title_sub'});
	$title =~ s/^\s+//s; # in case title_sub introduced any...
	$title = substr ($title, 0, 100);
	$title =~ s/\s\S*$/.../;

	my $top_section = "<!--<Section>\n<Metadata name=\"Title\">$title</Metadata>\n-->\n <!--</Section>-->\n";

	# add metadata per section...
	foreach my $section (@sections) {
	    $section =~ s@^(\d+)></a>@@; # leftover from split expression...

	    $title = $1; # Greenstone does magic if sections are titled digits
	    if (! defined($title) ) {
		print STDERR "no title: $section\n";
	    }
	    my $newsection = "<!-- from PDFPlug -->\n<!-- <Section>\n";
	    $newsection .= "<Metadata name=\"Title\">" . $title
		. "</Metadata>\n--><p>\n";
	    $newsection .= $section;
	    $newsection .= "<!--</Section>-->\n";
	    $section = $newsection;
	}

	$$textref=join('', ($top_section, @sections));
    }

    my $outhandle = $self->{'outhandle'};
    print $outhandle "PDFPlug: passing $_[3] on to $self->{'converted_to'}Plug\n"
	if $self->{'verbosity'} > 1;
    
    return ConvertToPlug::process_type($self,"pdf",@_);
}

1;
