Difference between revisions of "Contribute: Plugins/ExportPluginsHTML"

From EPrints Documentation
Jump to: navigation, search
m (Code.pm)
(HelloXML.pm: Initial code)
Line 1: Line 1:
 
=  Export Plugin Tutorial 3: (X)HTML and XML =
 
=  Export Plugin Tutorial 3: (X)HTML and XML =
  
= HelloXML.pm =
+
= HelloHTML.pm =
  
 
<pre>
 
<pre>
 +
package EPrints::Plugin::Export::MyPlugins::HelloXML;
 +
 +
@ISA = ("EPrints::Plugin::Export");
 +
 +
use strict;
 +
 +
sub new
 +
{
 +
        my ($class, %opts) = @_;
 +
 +
        my $self = $class->SUPER::new(%opts);
 +
 +
        $self->{name} = "Hello, HTML!";
 +
        $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];
 +
        $self->{visible} = "all";
 +
        $self->{suffix} = ".htm";
 +
        $self->{mimetype} = "text/html; charset=utf-8";
 +
 +
        return $self;
 +
}
 +
 +
sub output_dataobj
 +
{
 +
        my ($plugin, $dataobj) = @_;
 +
 +
        my $xml = $plugin->xml_dataobj($dataobj);
 +
 +
        return EPrints::XML::to_string($xml);
 +
}
 +
 +
sub output_list
 +
{
 +
        my ($plugin, %opts) = @_;
 +
 +
        my $r = [];
 +
 +
        my $header = "<html><head><title></title></head><body>";
 +
 +
        if (defined $opts{"fh"})
 +
        {
 +
                print {$opts{"fh"}} $header;
 +
        }
 +
        else
 +
        {
 +
                push @{$r}, $header;
 +
        }
 +
 +
        foreach my $dataobj ($opts{"list"}->get_records)
 +
        {
 +
                my $part = $plugin->output_dataobj($dataobj, %opts);
 +
                if (defined $opts{"fh"})
 +
                {
 +
                        print {$opts{fh}} $part;
 +
                }
 +
                else
 +
                {
 +
                        push @{$r}, $part;
 +
                }
 +
        }
 +
        my $footer = "</body></html>";
 +
        if (defined $opts{"fh"})
 +
        {
 +
                print {$opts{fh}} $footer;
 +
        }
 +
        else
 +
        {
 +
                push @{$r}, $footer;
 +
        }
 +
 +
        if (defined $opts{"fh"})
 +
        {
 +
                return undef;
 +
        }
 +
        return join('', @{$r});
 +
}
 +
 +
sub xml_dataobj
 +
        my ($plugin, $dataobj) = @_;
 +
 +
        my $session = $plugin->{session};
 +
 +
        my $div = $session->make_element("div");
 +
 +
        my $title = $session->make_element("h2");
 +
        $title->appendChild($session->make_text($dataobj->get_value("title")));
 +
        $div->appendChild($title);
 +
 +
        my $abstract = $session->make_element("p");
 +
        $abstract->appendChild($session->make_text($dataobj->get_value("abstract")));
 +
        $div->appendChild($abstract);
 +
 +
        return $div;
 +
}
 +
 +
1;
 +
 
</pre>
 
</pre>
  

Revision as of 16:36, 16 August 2007

Export Plugin Tutorial 3: (X)HTML and XML

HelloHTML.pm

package EPrints::Plugin::Export::MyPlugins::HelloXML;

@ISA = ("EPrints::Plugin::Export");

use strict;

sub new
{
        my ($class, %opts) = @_;

        my $self = $class->SUPER::new(%opts);

        $self->{name} = "Hello, HTML!";
        $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];
        $self->{visible} = "all";
        $self->{suffix} = ".htm";
        $self->{mimetype} = "text/html; charset=utf-8";

        return $self;
}

sub output_dataobj
{
        my ($plugin, $dataobj) = @_;

        my $xml = $plugin->xml_dataobj($dataobj);

        return EPrints::XML::to_string($xml);
}

sub output_list
{
        my ($plugin, %opts) = @_;

        my $r = [];

        my $header = "<html><head><title></title></head><body>";

        if (defined $opts{"fh"})
        {
                print {$opts{"fh"}} $header;
        }
        else
        {
                push @{$r}, $header;
        }

        foreach my $dataobj ($opts{"list"}->get_records)
        {
                my $part = $plugin->output_dataobj($dataobj, %opts);
                if (defined $opts{"fh"})
                {
                        print {$opts{fh}} $part;
                }
                else
                {
                        push @{$r}, $part;
                }
        }
        my $footer = "</body></html>";
        if (defined $opts{"fh"})
        {
                print {$opts{fh}} $footer;
        }
        else
        {
                push @{$r}, $footer;
        }

        if (defined $opts{"fh"})
        {
                return undef;
        }
        return join('', @{$r});
}

sub xml_dataobj
        my ($plugin, $dataobj) = @_;

        my $session = $plugin->{session};

        my $div = $session->make_element("div");

        my $title = $session->make_element("h2");
        $title->appendChild($session->make_text($dataobj->get_value("title")));
        $div->appendChild($title);

        my $abstract = $session->make_element("p");
        $abstract->appendChild($session->make_text($dataobj->get_value("abstract")));
        $div->appendChild($abstract);

        return $div;
}

1;

In More Detail

Testing Your Plugin