package CommentaryPlugin;

use ReadTextFile;
use niupepautil;
use doc;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa
no strict 'subs';

sub BEGIN {
    @CommentaryPlugin::ISA = ('ReadTextFile');
}

my $arguments =
    [
     { 'name' => "process_exp",
        'desc' => "{BasePlugin.process_exp}",
        'type' => "regexp",
        'deft' => &get_default_process_exp(),
        'reqd' => "no" } ,

 ];

my $options = { 'name'     => "CommentaryPlugin",
		'desc'     => "Simple plugin for processing commentaries in niupepa collection",
		'abstract' => "no",
		'inherits' => "yes",
		'args'     => $arguments };

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

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

    my $self = new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists);

    return bless $self, $class;
}

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

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

# do plugin specific processing of doc_obj
sub process {
    my $self = shift (@_);
    my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
    my $outhandle = $self->{'outhandle'};
    
    my ($filename_full_path, $filename_no_path) =  &util::get_full_filenames($base_dir, $file); 
    # filename is like 01.commentary, OID is 01commentary
    my $OID = $filename_no_path;
    $OID =~ s/\.//;
    $doc_obj->set_OID ($OID);
    print STDERR "set oid $OID\n";
    my ($full_dir) = $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
    # set metadata from meta.txt
    &niupepautil::set_main_metadata($doc_obj, $full_dir, $self->{'verbosity'});

    my $cursection = $doc_obj->get_top_section();
    # Title is commentary
    $doc_obj->set_utf8_metadata_element ($cursection, 'Title', "_commentary_");
    #$doc_obj->set_utf8_metadata_element ($cursection, "doctype", "Commentary");

    # just add the html text as is to the section
    
    $doc_obj->add_utf8_text($cursection, $$textref);

    return 1;
}
