Difference between revisions of "Contribute: Plugins/ExportPluginsHTML"
m (→output_dataobj) |
(→output_list) |
||
Line 123: | Line 123: | ||
== output_list == | == output_list == | ||
+ | The only changes to the output_list method are the additions of a header and a footer. | ||
+ | |||
<pre> | <pre> | ||
my $header = "<html><head><title></title></head><body>"; | my $header = "<html><head><title></title></head><body>"; |
Revision as of 10:40, 20 August 2007
Contents
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
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 = "<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; }