Difference between revisions of "Contribute: Plugins/ExportPluginsHTML"
(→HelloXML.pm: Initial code) |
|||
Line 1: | Line 1: | ||
− | = Export Plugin Tutorial 3: | + | = Export Plugin Tutorial 3: HTML = |
= HelloHTML.pm = | = HelloHTML.pm = | ||
<pre> | <pre> | ||
− | package EPrints::Plugin::Export::MyPlugins:: | + | package EPrints::Plugin::Export::MyPlugins::HelloHTML; |
@ISA = ("EPrints::Plugin::Export"); | @ISA = ("EPrints::Plugin::Export"); | ||
Line 103: | Line 103: | ||
= In More Detail = | = In More Detail = | ||
+ | <pre> | ||
+ | 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; | ||
+ | |||
+ | </pre> | ||
= Testing Your Plugin = | = Testing Your Plugin = |
Revision as of 16:37, 16 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
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;