Contribute: Plugins/ExportPluginsHTML

From EPrints Documentation
Revision as of 12:13, 20 August 2007 by Tom (talk | contribs) (xml_dataobj)
Jump to: navigation, search

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 = <<END;
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
                <head>
                        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
                        <title>XHTML Export Plugin</title>
                </head>
        <body>
END

        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

Here we change the file extension to ".htm" and change the MIME type to "text/html". For general XML documents you should change the file extension to ".xml" and the MIME type to "text/xml";

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

output_dataobj

Here the output_dataobj method does very little. It calls the xml_dataobj method to obtain a DOM object which is converted to plain text before being returned. The xml_dataobj method is described later.

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

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

output_list

The only changes to the output_list method are the additions of a header and a footer.

my $header = <<END;
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
                <head>
                        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
                        <title>XHTML Export Plugin</title>
                </head>
        <body>
END

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

xml_dataobj

This method has a similar signature to the output_dataobj method: it takes an implicit reference to the plugin object and a reference to a DataObj, however instead of returning a string it returns a DOM object.

Before we can start creating and manipulating DOM objects we need to get a reference to the Session object from the plugin.

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

We then create elements using the make_element method of our session object. The first argument to the make_element method is the tag name of the element we want to create, for example "a" or "div". The second parameter is a hash of attributes and values associated with the element.

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

Here we add a second-level header. The make_text method is used to create text nodes. The appendChild method is used to add child nodes.

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