package EBook::Generator::Exporter::XHTML; use 5.008009; use strict; use warnings; use MIME::Base64; our $VERSION = '0.01'; sub new { my ($class, @args) = @_; my $self = bless {}, $class; return $self->init(@args); } sub init { my ($self, $browser) = @_; $self->{'final-dir'} = '/tmp/'; $self->{'browser'} = $browser; $self->{'log'} = []; $self->{'handlers'} = { 'document' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; my $content = $self->transform_nodes(@subnodes); $self->{'meta'}->{'date'} = time() if $self->{'meta'}->{'date'} =~ /\\today/; return ''."\n". ''."\n". ''.$self->{'meta'}->{'title'}.''."\n". ''."\n". ''."\n". ''."\n". '
'."\n". $self->{'meta'}->{'title'}.'
'."\n". 'by '.$self->{'meta'}->{'author'}.'
'."\n". $self->{'meta'}->{'date'}.'

'."\n". $self->transform_text($self->{'meta'}->{'url'})."\n". '
'."\n". $content."\n". ''."\n". ''."\n"; }, 'paragraph' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; return "

".$self->transform_nodes(@subnodes)."

\n"; }, 'headline' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; return "\n\n". '{'level'}.'>'. $self->transform_nodes(@subnodes). "{'level'}.">\n\n"; }, 'text' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; my $latex = $self->transform_nodes(@subnodes); $latex = ''.$latex.'' if exists $opts->{'preformatted'} && $opts->{'preformatted'} == 1; $latex = ''.$latex.'' if exists $opts->{'weight'} && $opts->{'weight'} eq 'bold'; $latex = ''.$latex.'' if exists $opts->{'style'} && $opts->{'style'} eq 'italic'; return ' '.$latex; }, 'link' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; return ' '. $self->transform_nodes(@subnodes). ''; }, 'media' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; if ($opts->{'type'} =~ /^(gif|jpe?g|png)$/i) { # picture if ($self->{'options'}->{'embed-images'}) { my $fh = IO::File->new('< '.$opts->{'filename'}); my $data = join '', <$fh>; return "

". #'Image'. 'Image'. "

\n"; } else { return "

". 'Image'. "

\n"; } } return ''; }, 'list' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; my $name = ($opts->{'type'} eq 'ordered' ? 'ol' : 'ul'); return '<'.$name.'>'."\n". join('', map {'
  • '.$self->transform_nodes($_)."
  • \n"} @subnodes). ''."\n"; }, 'preformatted' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; return '
    '.
    				$self->transform_nodes(@subnodes).
    				'
    '."\n"; }, 'quote' => sub { my ($self, $node) = @_; my ($type, $opts, @subnodes) = @{$node}; return ' '.$self->transform_nodes(@subnodes).' '."\n"; }, }; return $self; } sub writeEBook { my ($self, $ebook, $options) = @_; $self->{'url'} = $ebook->{'url'}; $self->{'options'} = $options; $self->{'meta'} = $ebook->{'meta'}; # convert to xhtml my $converted = $self->transform_nodes($ebook->{'data'}); # wrap xhtml inside an EPUB container my $filename; if ($self->{'options'}->{'epub'}) { use EBook::EPUB; my $epub = EBook::EPUB->new(); # Set metadata: title/author/language/id $epub->add_title($self->{'meta'}->{'title'}); $epub->add_author($self->{'meta'}->{'author'}); $epub->add_date($self->{'meta'}->{'date'}); #$epub->add_language('en'); #$epub->add_identifier('1440465908', 'ISBN'); # Add package content: stylesheet, font, xhtml and cover #$epub->copy_stylesheet('/path/to/style.css', 'style.css'); #$epub->copy_file('/path/to/figure1.png', 'figure1.png', 'image/png'); #$epub->encrypt_file('/path/to/CharisSILB.ttf', 'CharisSILB.ttf', 'application/x-font-ttf'); #my $chapter_id = $epub->copy_xhtml('/path/to/page1.xhtml', 'page1.xhtml'); # note: $epub->copy_xhtml(...) seems to be buggy $epub->add_xhtml('index.xhtml', $converted, linear => 'no'); # Add top-level nav-point #my $navpoint = $epub->add_navpoint( # label => "Chapter 1", # id => $chapter_id, # content => 'page1.xhtml', # play_order => 1 # should always start with 1 #); # Generate resulting ebook $filename = $self->{'final-dir'}.$ebook->{'meta'}->{'title'}.".epub"; $epub->pack_zip($filename); } else { # write xhtml file $filename = $self->{'final-dir'}.$ebook->{'meta'}->{'title'}.".html"; unlink($filename) if -f $filename; my $fh = IO::File->new("> ".$filename); print $fh $converted; } print "have fun with: ".$filename."\n"; return $filename; } sub transform_nodes { my ($self, @nodes) = @_; my $conv = ''; foreach my $node (@nodes) { if (ref $node) { my $type = $node->[0]; if (exists $self->{'handlers'}->{$type}) { $conv .= $self->{'handlers'}->{$type}->($self, $node); } else { my ($type, $opts, @subnodes) = @{$node}; $conv .= $self->transform_nodes(@subnodes); } } else { $conv .= $self->transform_text($node); } } return $conv; } sub transform_text { my ($self, $s) = @_; # ... return $s; } 1; __END__ # Below is stub documentation for your module. You'd better edit it! =head1 NAME EBook::Generator - Perl extension for creating EBooks from Websites. =head1 SYNOPSIS use EBook::Generator; my $g = EBook::Generator->new(); my $ebook = $g->readSource("http://www.google.com", fontSize => 12, grayImages => 1); $ebook->writeEBook("./Google.pdf"); =head1 DESCRIPTION EBook::Generator can be used to generate a beautifully looking e-book out of a website, aka HTML source. It uses LaTeX to create the actual e-book. =head2 EXPORT None by default. =head1 SEE ALSO Mention other useful documentation such as the documentation of related modules or operating system documentation (such as man pages in UNIX), or any relevant external documentation such as RFCs or standards. If you have a mailing list set up for your module, mention it here. If you have a web site set up for your module, mention it here. =head1 AUTHOR Tom Kirchner, Ekitomer@cpan.orgE =head1 COPYRIGHT AND LICENSE Copyright (C) 2010 by Tom Kirchner This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.9 or, at your option, any later version of Perl 5 you may have available. =cut