Difference between revisions of "Contribute: Plugins/ExportPluginsHTML"

From EPrints Documentation
Jump to: navigation, search
m (In More Detail)
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
 +
[[Category:Contribute]]
 +
[[Category:Plugins]]
 
=  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.
 
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 =
+
= HTML.pm =
 +
The code in the section below should be placed in a file called HTML.pm in the directory created previously, and MyPlugins should be changed to the name of that directory.
  
 
<pre>
 
<pre>
package EPrints::Plugin::Export::MyPlugins::HelloHTML;
+
package EPrints::Plugin::Export::MyPlugins::HTML;
  
 
@ISA = ('EPrints::Plugin::Export');
 
@ISA = ('EPrints::Plugin::Export');
Line 18: Line 21:
 
         my $self = $class->SUPER::new(%opts);
 
         my $self = $class->SUPER::new(%opts);
  
         $self->{name} = 'Hello, HTML!';
+
         $self->{name} = 'Hello, HTML';
 
         $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];
 
         $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];
 
         $self->{visible} = 'all';
 
         $self->{visible} = 'all';
Line 92: Line 95:
  
 
sub xml_dataobj
 
sub xml_dataobj
 +
{
 
         my ($plugin, $dataobj) = @_;
 
         my ($plugin, $dataobj) = @_;
  
Line 115: Line 119:
 
= In More Detail =
 
= In More Detail =
 
== Constructor ==
 
== 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';
+
Here we change the file extension to '.htm' and change the [http://en.wikipedia.org/wiki/MIME MIME] type to "text/html". For general XML documents you should change the file extension to '.xml' and the [http://en.wikipedia.org/wiki/MIME MIME] type to 'text/xml';
  
 
<pre>
 
<pre>
Line 123: Line 127:
  
 
== output_dataobj ==
 
== 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.
+
Here the output_dataobj method does very little. It calls the xml_dataobj method to obtain a [http://en.wikipedia.org/wiki/Document_Object_Model DOM] tree which is converted to plain text before being returned. The xml_dataobj method is described later.
  
 
<pre>
 
<pre>
Line 150: Line 154:
  
 
== xml_dataobj ==
 
== 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.
+
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 [http://en.wikipedia.org/wiki/Document_Object_Model DOM] tree.
  
Before we can start creating and manipulating DOM objects we need to get a reference to the Session object from the plugin.
+
Before we can start creating and manipulating [http://en.wikipedia.org/wiki/Document_Object_Model DOM] objects we need to get a reference to the Session object from the plugin.
  
 
<pre>
 
<pre>
Line 179: Line 183:
 
</pre>
 
</pre>
  
Finally we return a DOM object representing a 'div' element containing our header and paragraph.
+
Finally we return a [http://en.wikipedia.org/wiki/Document_Object_Model DOM] object representing a 'div' element containing our header and paragraph.
  
 
<pre>
 
<pre>
Line 186: Line 190:
  
 
= Testing Your Plugin =
 
= Testing Your Plugin =
Restart your webserver and test the plugin as in [[User:Tom/Export_Plugins/Hello_Lists| the previous tutorial]].
+
Restart your webserver and test the plugin as [[Contribute:_Plugins/ExportPluginsHello| before]].
  
 
== Sample Output ==
 
== Sample Output ==
 
[[Image:Exphtml.png]]
 
[[Image:Exphtml.png]]

Latest revision as of 13:49, 8 February 2010

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.

HTML.pm

The code in the section below should be placed in a file called HTML.pm in the directory created previously, and MyPlugins should be changed to the name of that directory.

package EPrints::Plugin::Export::MyPlugins::HTML;

@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 tree 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 tree.

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

A similar pattern is followed to add a paragraph containing the eprint's abstract.

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

Finally we return a DOM object representing a 'div' element containing our header and paragraph.

return $div;

Testing Your Plugin

Restart your webserver and test the plugin as before.

Sample Output

Exphtml.png