Difference between revisions of "Contribute: Plugins/ExportPluginsHTML"
m (→output_list) |
m (→xml_dataobj) |
||
Line 143: | Line 143: | ||
return $div; | return $div; | ||
} | } | ||
− | |||
− | |||
− | |||
</pre> | </pre> | ||
= Testing Your Plugin = | = Testing Your Plugin = |
Revision as of 19:28, 17 August 2007
Contents
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
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; }