Difference between revisions of "Contribute: Plugins/ExportPluginsHTML"

From EPrints Documentation
Jump to: navigation, search
m (User:Tom/Export Plugins/XML moved to User:Tom/Export Plugins/HTML)
m (In More Detail)
Line 103: Line 103:
  
 
= In More Detail =
 
= In More Detail =
 +
== Constructor ==
 
<pre>
 
<pre>
package EPrints::Plugin::Export::MyPlugins::HelloHTML;
+
$self->{suffix} = ".htm";
 
+
$self->{mimetype} = "text/html; charset=utf-8";
@ISA = ("EPrints::Plugin::Export");
+
</pre>
 
 
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;
 
}
 
  
 +
== output_dataobj ==
 +
<pre>
 
sub output_dataobj
 
sub output_dataobj
 
{
 
{
Line 133: Line 119:
 
         return EPrints::XML::to_string($xml);
 
         return EPrints::XML::to_string($xml);
 
}
 
}
 +
</pre>
  
 +
== output_list ==
 +
<pre>
 
sub output_list
 
sub output_list
 
{
 
{
Line 179: Line 168:
 
         return join('', @{$r});
 
         return join('', @{$r});
 
}
 
}
 +
</pre>
  
 +
== xml_dataobj ==
 +
 +
<pre>
 
sub xml_dataobj
 
sub xml_dataobj
 
         my ($plugin, $dataobj) = @_;
 
         my ($plugin, $dataobj) = @_;

Revision as of 19:25, 17 August 2007

Export Plugin Tutorial 3: HTML

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

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

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

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

output_list

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

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

1;

Testing Your Plugin