Difference between revisions of "Contribute: Plugins/ExportPluginsHTML"
(→Testing Your Plugin) |
m (→HelloHTML.pm: Edit for consistency.) |
||
Line 8: | Line 8: | ||
package EPrints::Plugin::Export::MyPlugins::HelloHTML; | package EPrints::Plugin::Export::MyPlugins::HelloHTML; | ||
− | @ISA = ( | + | @ISA = ('EPrints::Plugin::Export'); |
use strict; | use strict; | ||
Line 18: | Line 18: | ||
my $self = $class->SUPER::new(%opts); | my $self = $class->SUPER::new(%opts); | ||
− | $self->{name} = | + | $self->{name} = 'Hello, HTML!'; |
$self->{accept} = [ 'dataobj/eprint', 'list/eprint' ]; | $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ]; | ||
− | $self->{visible} = | + | $self->{visible} = 'all'; |
− | $self->{suffix} = | + | $self->{suffix} = '.htm'; |
− | $self->{mimetype} = | + | $self->{mimetype} = 'text/html; charset=utf-8'; |
return $self; | return $self; | ||
Line 53: | Line 53: | ||
END | END | ||
− | if (defined $opts{ | + | if (defined $opts{fh}) |
{ | { | ||
− | print {$opts{ | + | print {$opts{fh}} $header; |
} | } | ||
else | else | ||
Line 62: | Line 62: | ||
} | } | ||
− | foreach my $dataobj ($opts{ | + | foreach my $dataobj ($opts{list}->get_records) |
{ | { | ||
my $part = $plugin->output_dataobj($dataobj, %opts); | my $part = $plugin->output_dataobj($dataobj, %opts); | ||
− | if (defined $opts{ | + | if (defined $opts{fh}) |
{ | { | ||
print {$opts{fh}} $part; | print {$opts{fh}} $part; | ||
Line 74: | Line 74: | ||
} | } | ||
} | } | ||
− | my $footer = | + | my $footer = '</body></html>'; |
− | if (defined $opts{ | + | if (defined $opts{fh}) |
{ | { | ||
print {$opts{fh}} $footer; | print {$opts{fh}} $footer; | ||
Line 84: | Line 84: | ||
} | } | ||
− | if (defined $opts{ | + | if (defined $opts{fh}) |
{ | { | ||
return undef; | return undef; | ||
Line 96: | Line 96: | ||
my $session = $plugin->{session}; | my $session = $plugin->{session}; | ||
− | my $div = $session->make_element( | + | my $div = $session->make_element('div'); |
− | my $title = $session->make_element( | + | my $title = $session->make_element('h2'); |
− | $title->appendChild($session->make_text($dataobj->get_value( | + | $title->appendChild($session->make_text($dataobj->get_value('title'))); |
$div->appendChild($title); | $div->appendChild($title); | ||
− | my $abstract = $session->make_element( | + | my $abstract = $session->make_element('p'); |
− | $abstract->appendChild($session->make_text($dataobj->get_value( | + | $abstract->appendChild($session->make_text($dataobj->get_value('abstract'))); |
$div->appendChild($abstract); | $div->appendChild($abstract); | ||
Revision as of 12:08, 27 September 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 = <<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);
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 in the previous tutorial.