Skip to content

Commit

Permalink
changed interface to plugin options
Browse files Browse the repository at this point in the history
  • Loading branch information
borisdaeppen committed Aug 16, 2012
1 parent c0e75df commit 0704392
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 97 deletions.
28 changes: 11 additions & 17 deletions lib/EBook/MOBI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ sub add_content {
my $pagemode = $args{pagemode} || 0;
my $head0_mode = $args{head0_mode} || 0;
my $driver = $args{driver} || $self->{default_driver};
my $driver_opt = $args{driver_options} || 0;

# we load a plugin to convert the input to mobi format
my $parser;
Expand All @@ -133,10 +134,12 @@ sub add_content {
die "Problems with plugin $driver at $require_name: $@" if $@;

# pass some settings
$parser->debug_on($self->{ref_to_debug_sub})
if ($self->{ref_to_debug_sub});
$parser->pagemode($pagemode);
$parser->head0_mode($head0_mode);
if ($self->{ref_to_debug_sub}) {
$parser->debug_on($self->{ref_to_debug_sub});
}
if ($driver_opt) {
$parser->set_options($driver_opt);
}

# ok, now we prepare the parsing, unfortunately we have to do
# some complicated magic with the string data...
Expand Down Expand Up @@ -393,9 +396,9 @@ If you indent the 'h1' tag with any whitespace, it will not appear in the TOC (o
Use this method if you have your content in a specific markup format.
See below for details to the arguments supported by this method.
$book->add_content( data => $POD_in,
driver => 'EBook::MOBI::Driver::POD',
pagemode => 1,
$book->add_content( data => $data_as_string,
driver => $driver_name,
driver_options => {plugin_option => $value}
);
The method uses a plugin system to transform your format into an ebook.
Expand All @@ -408,18 +411,9 @@ A string, containing your text for the ebook.
=head3 driver
The name of the module which parses your data.
If this value is not set, the default is C<EBook::MOBI::Driver::POD>.
If this value is not set, the default is L<EBook::MOBI::Driver::POD>.
You are welcome to add your own driver for your markup!
=head3 pagemode
If you pass any true value here, every head1 chapter will end with a peagebreak. This mostly makes sence, so it is a good idea to use this feature.
Default is to not insert pagebreak.
This only works, if the driver has implemented this functionality!
For C<EBook::MOBI::Driver::POD> that's the case.
=head2 add_pagebreak
Use this method to seperate content and give some structure to your book.
Expand Down
91 changes: 32 additions & 59 deletions lib/EBook/MOBI/Driver.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,14 @@ sub new {
return $ref;
}


sub parse {
die ("method parse() no overriden.\n");
}

sub html_body {
my ($self, $boolean) = @_;

if (@_ > 1) {
$self->{+P . 'body'} = $boolean;
}
else {
return $self->{+P . 'body'};
}
}

sub pagemode {
my ($self, $boolean) = @_;

if (@_ > 1) {
$self->{+P . 'pages'} = $boolean;
}
else {
return $self->{+P . 'pages'};
}
die ("method parse() not overriden.\n");
}

sub head0_mode {
my ($self, $boolean) = @_;

if (@_ > 1) {
$self->{+P . 'head0_mode'} = $boolean;
}
else {
return $self->{+P . 'head0_mode'};
}
sub set_options {
die ("method set_options() not overriden.\n");
}


sub debug_on {
my ($self, $ref_to_debug_sub) = @_;

Expand All @@ -97,30 +66,6 @@ sub debug_msg {
}
}

# encode_entities() from HTML::Entities does not translate it correctly
# this is why I make it here manually as a quick fix
# don't reall know where how to handle this utf8 problem for now...
sub html_enc {
my $string = shift;

$string = encode_entities($string);
# ^
my $lt = LT; # |
my $gt = GT; # |
my $am = AMP; # |
my $co = COL; # |-- don't change this order!
my $qu = QUO; # |
my $dqu= DQUO; # |
$string =~ s/$lt/</g; # |
$string =~ s/$gt/>/g; # |
$string =~ s/$am/&/g; # |
$string =~ s/$co/;/g; # |
$string =~ s/$qu/'/g; # |
$string =~ s/$dqu/"/g; #<---|

return $string;
}

1;

__END__
Expand All @@ -136,7 +81,35 @@ EBook::MOBI::Driver - Interface for plugins.
use EBook::MOBI::Driver;
=head1 METHODS
=head1 IMPLEMENTED METHODS
=head2 new
Saves a plugin the need to wrtite this one.
=head2 debug_on
Enable debugging by passing a sub.
=head2 debug_off
Stop debug messages.
=head2 debug_msg
Write a debug message.
=head1 EMPTY METHODS
=head2 parse
Should be implemented by the plugin!
Takes a string, returns a string.
=head2 set_options
Should be implemented by the plugin!
Takes a %hash with arguments.
=head1 COPYRIGHT & LICENSE
Expand Down
5 changes: 5 additions & 0 deletions lib/EBook/MOBI/Driver/Example.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ sub parse {
return $mobiFormat;
}

sub set_options {
my $self = shift;
$self->debug_msg('this plugin has no options');
}

1;

__END__
Expand Down
84 changes: 70 additions & 14 deletions lib/EBook/MOBI/Driver/POD.pm
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,53 @@ sub parse {
return $buffer4html;
};

sub set_options {
my $self = shift;
my $args = shift;

if (ref($args) eq "HASH") {
$self->html_body ($args->{html_body}) if (exists $args->{html_body});
$self->head0_mode($args->{head0_mode}) if (exists $args->{head0_mode});
$self->pagemode ($args->{pagemode}) if (exists $args->{pagemode});
}
else {
$self->debug_msg('Plugin options are not in a HASH');
}
}

sub html_body {
my ($self, $boolean) = @_;

if (@_ > 1) {
$self->{+P . 'body'} = $boolean;
}
else {
return $self->{+P . 'body'};
}
}

sub pagemode {
my ($self, $boolean) = @_;

if (@_ > 1) {
$self->{+P . 'pages'} = $boolean;
}
else {
return $self->{+P . 'pages'};
}
}

sub head0_mode {
my ($self, $boolean) = @_;

if (@_ > 1) {
$self->{+P . 'head0_mode'} = $boolean;
}
else {
return $self->{+P . 'head0_mode'};
}
}

# encode_entities() from HTML::Entities does not translate it correctly
# this is why I make it here manually as a quick fix
# don't reall know where how to handle this utf8 problem for now...
Expand Down Expand Up @@ -635,34 +682,37 @@ __END__
=head1 NAME
EBook::MOBI::Pod2Mhtml - Create HTML, flavoured for the MOBI format, out of POD.
EBook::MOBI::Driver::POD - Create HTML, flavoured for the MOBI format, out of POD.
This module extends L<Pod::Parser> for parsing capabilities. The module L<HTML::Entities> is used to translate chars to HTML entities.
=head1 SYNOPSIS
use EBook::MOBI::Pod2Mhtml;
my $p2h = new EBook::MOBI::Pod2Mhtml;
This module is a plugin for L<EBook::MOBI>.
You probably don't need to access this module directly, unless you are a developer for C<EBook::MOBI>.
# $pod_h and $html_out_h are file handles
# or IO::String objects
$p2h->parse_from_filehandle($pod_h, $html_out_h);
use EBook::MOBI::Driver::POD;
my $plugin = new EBook::MOBI::Driver::POD;
# result is now in $html_out_h
my $mobi_format = $plugin->parse($pod);
=head1 METHODS
=head2 parse_from_filehandle
=head2 parse
This is the method you need to call, if you want this module to be of any help for you. It will take your data in the POD format and return it in special flavoured HTML, which can be then further used for the MOBI format.
This is the method each plugin should provide!
It takes the input format as a string and returns MHTML.
Hand over two file handles or Objects of L<IO::String>. The first handle points to your POD, the second waits to receive the result.
=head1 METHODS (POD plugin specific)
# $pod_h and $html_out_h are file handles
# or IO::String objects
$p2h->parse_from_filehandle($pod_h, $html_out_h);
=head2 set_options
# result is now in $html_out_h
This method is provided by all plugins.
This module supports the following options:
$plugin->set_options(pagemode => 1, head0_mode => 1, pagemode => 1);
See description below for more details of the options.
=head2 pagemode
Expand Down Expand Up @@ -706,6 +756,8 @@ Pass any true value to enable 'html_body'. If set, parsed content will be encaps
Default is to not encapsulate in a body tag.
=head1 METHODS (inherited from Driver code)
=head2 debug_on
You can just ignore this method if you are not interested in debuging!
Expand All @@ -716,6 +768,10 @@ Pass a reference to a debug subroutine and enable debug messages.
Stop debug messages and erease the reference to the subroutine.
=head2 debug_msg
Write a message.
=head2 INHERITED INTERNAL METHODS
=head3 begin_input
Expand Down
10 changes: 5 additions & 5 deletions t/20_mobi.t
Original file line number Diff line number Diff line change
Expand Up @@ -329,26 +329,26 @@ $res = $obj->print_mhtml(1);
is($res, $POD_res_default, "Book -> driver_select");

$obj->reset();
$obj->add_content(data => $POD_in, head0_mode => 1);
$obj->add_content(data => $POD_in, driver_options => {head0_mode => 1});
$obj->make();
$res = $obj->print_mhtml(1);
is($res, $POD_res_head0_mode, "Book -> head0_mode");

$obj->reset();
$obj->add_content(data => $POD_in, pagemode => 1);
$obj->add_content(data => $POD_in, driver_options => {pagemode => 1});
$obj->make();
$res = $obj->print_mhtml(1);
is($res, $POD_res_pagemode, "Book -> pagemode");

$obj->reset();
$obj->add_content(data => $POD_in, pagemode => 1, head0_mode => 1);
$obj->add_content(data => $POD_in, driver_options => {pagemode => 1, head0_mode => 1});
$obj->make();
$res = $obj->print_mhtml(1);
is($res, $POD_res_head0_and_pagemode, "Book -> head0+pagemode");

$obj->reset();
$obj->add_toc_once();
$obj->add_content(data => $POD_in, pagemode => 1, head0_mode => 1);
$obj->add_content(data => $POD_in, driver_options => {pagemode => 1, head0_mode => 1});
$obj->make();
$res = $obj->print_mhtml(1);
is($res, $POD_res_toc_and_head0_and_pagemode, "Book -> toc+head0+pagemode");
Expand All @@ -362,7 +362,7 @@ is($res, $POD_res_namedtoc, "Book -> namedtoc");

$obj->reset();
$obj->add_toc_once('TOC_NAME');
$obj->add_content(data => $POD_in, pagemode => 1, head0_mode => 1);
$obj->add_content(data => $POD_in, driver_options => {pagemode => 1, head0_mode => 1});
$obj->make();
$res = $obj->print_mhtml(1);
is($res, $POD_res_namedtoc_and_head0_and_pagemode, "Book -> namedtoc+head0+pagemode");
Expand Down
6 changes: 4 additions & 2 deletions t/26_driver_example.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use File::Temp qw(tempfile);
#######################
# TESTING starts here #
#######################
use Test::More tests => 6;
use Test::More tests => 8;

###########################
# General module tests... #
Expand All @@ -24,9 +24,11 @@ my $obj = $module->new();

isa_ok($obj, $module);

can_ok($obj, 'pagemode');
can_ok($obj, 'parse');
can_ok($obj, 'set_options');
can_ok($obj, 'debug_on');
can_ok($obj, 'debug_off');
can_ok($obj, 'debug_msg');

################################
# We define some parsing input #
Expand Down

0 comments on commit 0704392

Please sign in to comment.