###########################################################################
#
# 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.
#
###########################################################################
use strict;
no strict 'refs'; # so we can use a var for filehandles (eg STDERR)

package PDFPlug;

use ConvertToPlug;
use unicode;

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

my $arguments =
    [ { 'name' => "process_exp",
	'desc' => "{BasPlug.process_exp}",
	'type' => "regexp",
	'deft' => &get_default_process_exp(),
	'reqd' => "no" },
      { 'name' => "block_exp",
	'desc' => "{BasPlug.block_exp}",
	'type' => "regexp",
	'deft' => &get_default_block_exp() },
      { 'name' => "noimages",
	'desc' => "{PDFPlug.noimages}",
	'type' => "flag" },
      { 'name' => "complex",
	'desc' => "{PDFPlug.complex}",
	'type' => "flag" },
      { 'name' => "nohidden",
	'desc' => "{PDFPlug.nohidden}",
	'type' => "flag" },
      { 'name' => "zoom",
	'desc' => "{PDFPlug.zoom}",
	'deft' => "2",
	'range' => "1,3", # actually the range is 0.5-3 
	'type' => "int" },
      { 'name' => "use_sections",
	'desc' => "{PDFPlug.use_sections}",
	'type' => "flag" } ];

my $options = { 'name'     => "PDFPlug",
		'desc'     => "{PDFPlug.desc}",
		'abstract' => "no",
		'inherits' => "yes",
		'args'     => $arguments };

sub new {
    my ($class) = shift (@_);
    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
    push(@$pluginlist, $class);

    if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
    if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};

    push(@$inputargs,"-title_sub");
    push(@$inputargs,'^(Page\s+\d+)?(\s*1\s+)?');

    my $self = (defined $hashArgOptLists)? new ConvertToPlug($pluginlist,$inputargs,$hashArgOptLists): new ConvertToPlug($pluginlist,$inputargs);


    #if ($self->{"use_sections"}) {
	#$self->{"description_tags"} = 1;
    #}

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

    my $secondary_plugin_options = $self->{'secondary_plugin_options'};

    if (!defined $secondary_plugin_options->{'HTMLPlug'}) {
	$secondary_plugin_options->{'HTMLPlug'} = [];
    }
    if (!defined $secondary_plugin_options->{'TEXTPlug'}) {
	$secondary_plugin_options->{'TEXTPlug'} = [];
    }

    my $html_options = $secondary_plugin_options->{'HTMLPlug'};
    my $text_options = $secondary_plugin_options->{'TEXTPlug'};

    if ($self->{'input_encoding'} eq "auto") {
	# pdftohtml will always produce html files encoded as utf-8
	# => restrict primary PDFPlug and secondary HTML plugin to use 
	# utf8 and extract language.

	$self->{'input_encoding'} = "utf8";
	$self->{'extract_language'} = 1;

	push(@$html_options,"-input_encoding", "utf8");
	push(@$html_options,"-extract_language");
    }

    # Instruct HTMLPlug (when eventually accessed through read_into_doc_obj) 
    # to extract these metadata fields from the HEAD META fields
    push(@$html_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");

    if ($self->{'use_sections'}) {
	$self->{'description_tags'} = 1;
	push(@$html_options,"-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?
    push(@$html_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
    push(@$text_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');

    $self = bless $self, $class;

    $self->load_secondary_plugins($class,$secondary_plugin_options);

    return $self;
}

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 "";
}
    
sub convert_post_process
{
    my $self = shift (@_);
    my ($conv_filename) = @_;

    my $outhandle=$self->{'outhandle'};

    my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);

    # read in file ($text will be in utf8)
    my $text = "";
    $self->read_file ($conv_filename, $encoding, $language, \$text);

    # Calculate number of pages based on <a ...> tags (we have a <a name=1> etc
    # for each page).  Metadata based on this calculation not set until process()
    # 
    # Note: this is done even if we are not breaking to document into pages as it might
    # be useful to give an indication of document length in browser through setting
    # num_pages as metadata.

    my @pages = ($text =~ /\<[Aa] name=\"?\w+\"?>/ig);
    my $num_pages = scalar(@pages);
    $self->{'num_pages'} = $num_pages;

    if ($self->{'use_sections'}
	&& $self->{'converted_to'} eq "HTML") {

	print $outhandle "PDFPlug: Calculating sections...\n";

	# we have "<a name=1></a>" etc for each page
	# it may be <A name=
	my @sections = split('<[Aa] name=', $text);

	my $top_section = "";

	if (scalar (@sections) == 1) { #only one section - no split!
	    print $outhandle "PDFPlug: warning - no sections found\n";
	} else {
	    $top_section .= shift @sections; # keep HTML header etc as top_section
	}

	# handle first section specially for title? Or all use first 100...
	
	my $title = $sections[0];
	$title =~ s/^\"?\w+\"?>//; # 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*$/.../;


	if (scalar (@sections) == 1) { # no sections found
	    $top_section .= $sections[0];
	    @sections=();
	} else {
	    $top_section .= "<!--<Section>\n<Metadata name=\"Title\">$title</Metadata>\n-->\n <!--</Section>-->\n";
	}

	# add metadata per section...
	foreach my $section (@sections) {
	    # section names are not always just digits, may be like "outline"
	    $section =~ s@^\"?(\w+)\"?></a>@@; # leftover from split expression...

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

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

    # turn any high bytes that aren't valid utf-8 into utf-8.
    unicode::ensure_utf8(\$text);

    # Write it out again!
    $self->utf8_write_file (\$text, $conv_filename);
}


# do plugin specific processing of doc_obj for HTML type
sub process {
    my $self = shift (@_);
    my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;

    my $result = $self->process_type("pdf",$base_dir,$file,$doc_obj);

    # fix up the extracted date metadata to be in Greenstone date format,
    # and fix the capitalisation of 'date'
    my $cursection = $doc_obj->get_top_section();

    foreach my $datemeta (@{$doc_obj->get_metadata($cursection, "date")}) {
	$doc_obj->delete_metadata($cursection, "date", $datemeta);

	# We're just interested in the date bit, not the time
	# some pdf creators (eg "Acrobat 5.0 Scan Plug-in for Windows")
	# set a /CreationDate, and set /ModDate to 000000000. pdftohtml
	# extracts the ModDate, so it is 0...
	$datemeta =~ /(\d+)-(\d+)-(\d+)/;
	my ($year, $month, $day) = ($1,$2,$3);
	if (defined($year) && defined($month) && defined($day)) {
	    if ($year == 0) {next}
	    if ($year < 100) {$year += 1900} # just to be safe
	    if ($month =~ /^\d$/) {$month="0$month"} # single digit
	    if ($day =~ /^\d$/) {$day="0$day"} # single digit
	    my $date="$year$month$day";
	    $doc_obj->add_utf8_metadata($cursection, "Date", $date);
	}
    }

    $doc_obj->add_utf8_metadata($cursection, "NumPages", $self->{'num_pages'});

    
    if ($self->{'use_sections'} && $self->{'converted_to'} eq "HTML") {
	# we explicitly make it a paged document, cos greenstone won't get it
	# right if any section has an empty title, or one with letters in it
	$doc_obj->set_utf8_metadata_element ($cursection, "gsdlthistype", "Paged");
    }

    return $result;
}

1;
