Difference between revisions of "Contribute: Plugins/ExportPluginsHTML"

From EPrints Documentation
Jump to: navigation, search
m (xml_dataobj)
m (Export Plugin Tutorial 3: HTML)
Line 1: Line 1:
 
=  Export Plugin Tutorial 3: HTML =
 
=  Export Plugin Tutorial 3: HTML =
 +
 +
In this tutorial we'll look at creating an export plugin with slightly more complex output than unformatted plain text. Although the plugin below produces XHTML the same principles apply to producing any XML document.
  
 
= HelloHTML.pm =
 
= HelloHTML.pm =

Revision as of 10:31, 20 August 2007

Export Plugin Tutorial 3: HTML

In this tutorial we'll look at creating an export plugin with slightly more complex output than unformatted plain text. Although the plugin below produces XHTML the same principles apply to producing any XML document.

HelloHTML.pm

package EPrints::Plugin::Export::MyPlugins::HelloHTML;

@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

Constructor

$self->{suffix} = ".htm";
$self->{mimetype} = "text/html; charset=utf-8";

output_dataobj

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

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

output_list

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

my $footer = "</body></html>";

xml_dataobj

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;
}

Testing Your Plugin