EBook-Generator
view release on metacpan or search on metacpan
lib/EBook/Generator/Exporter/PDF.pm view on Meta::CPAN
package EBook::Generator::Exporter::PDF;
use 5.008009;
use strict;
use warnings;
use Data::Dumper;
use XML::LibXML;
use Image::Size;
use HTML::Entities;
use LaTeX::Encode;
use File::Temp;
use File::Copy;
use EBook::Generator::Parser;
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'} = {
# garamond shapes:
#
# {\garamond normal}
# {\slshape This is garamond slanted} \\
# {\bfseries This is garamond bold face} \\
# {\scshape This is in small caps} \\
# {\slshape \bfseries This is slanted and bold face} \\
'document' => sub {
my ($self, $node) = @_;
my ($type, $opts, @subnodes) = @{$node};
my $content = $self->transform_nodes(@subnodes);
return
'\title{'.$self->{'meta'}->{'title'}.' \footnote{'.$self->transform_text($self->{'meta'}->{'url'}).'}}'."\n".
'\author{'.$self->{'meta'}->{'author'}.'}'."\n".
'\date{'.$self->{'meta'}->{'date'}.'}'."\n".
'\documentclass[12pt,a6paper]{article}'."\n".
'\usepackage[left=5mm,right=5mm,top=5mm,bottom=5mm,nohead,includefoot]{geometry}'."\n".
'\footnotesep=0mm'."\n".
'\parskip=5mm'."\n".
'\parindent=0mm'."\n".
#'\sloppy'."\n".
#'\renewcommand{\familydefault}{\sfdefault}'."\n".
'\usepackage{garamond}'."\n".
'\usepackage{lmodern}'."\n".
'\usepackage{pslatex}'."\n".
'\usepackage[T1]{fontenc}'."\n".
# no margins in table of contents
'\usepackage{tocloft}'."\n".
'\renewcommand{\cftsecindent}{0em}'."\n".
'\renewcommand{\cftsecnumwidth}{1.9em}'."\n".
'\renewcommand{\cftsubsecindent}{1.9em}'."\n".
'\renewcommand{\cftsubsecnumwidth}{2.8em}'."\n".
'\renewcommand{\cftsubsubsecindent}{0em}'."\n".
'\renewcommand{\cftsubsubsecnumwidth}{3.2em}'."\n".
'\usepackage{graphicx}'."\n".
'\usepackage{paralist}'."\n".
'\setdefaultleftmargin{1.3em}{}{}{}{.5em}{.5em}'."\n".
'\usepackage{listings}'."\n".
'\lstset{numbers=left, numberstyle=\tiny, numbersep=5pt} \lstset{language=Perl}'."\n".
'\begin{document}'."\n".
'\garamond'."\n".
'\maketitle'."\n".
'\newpage'."\n".
'\garamond'."\n".
($self->{'has-section'} > 1 ?
'\tableofcontents'."\n".
'\newpage'."\n" : '').
'\garamond'."\n".
$content.
'\end{document}'."\n";
},
'paragraph' => sub {
my ($self, $node) = @_;
my ($type, $opts, @subnodes) = @{$node};
return "\n\n".$self->transform_nodes(@subnodes)."\n\n";
},
'headline' => sub {
my ($self, $node) = @_;
$self->{'has-section'} = 1;
my ($type, $opts, @subnodes) = @{$node};
my $name =
($opts->{'level'} == 1 ? 'section' :
($opts->{'level'} == 2 ? 'subsection' :
($opts->{'level'} == 3 ? 'subsubsection' :
($opts->{'level'} == 4 ? 'paragraph' :
'subparagraph'))));
return "\n\n".'\\'.$name.'{'.$self->transform_nodes(@subnodes)."}\n\n";
},
'text' => sub {
my ($self, $node) = @_;
my ($type, $opts, @subnodes) = @{$node};
my $latex = $self->transform_nodes(@subnodes);
$latex = ' {\footnotesize\lstinline|'.$latex.'|} '
if exists $opts->{'preformatted'} && $opts->{'preformatted'} == 1;
$latex = ' \textbf{'.$latex.'} '
if exists $opts->{'weight'} && $opts->{'weight'} eq 'bold';
$latex = ' \textit{'.$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).
'\footnote{'.$self->transform_text($opts->{'target'})."}";
},
'media' => sub {
my ($self, $node) = @_;
my ($type, $opts, @subnodes) = @{$node};
if ($opts->{'type'} =~ /^(gif|jpe?g|png)$/i && -f $opts->{'filename'}) {
# picture
return
"\n\n".
'\includegraphics[width=\columnwidth,keepaspectratio=true]{'.
$opts->{'filename'}."}".
"\n\n";
}
return '';
},
'list' => sub {
my ($self, $node) = @_;
my ($type, $opts, @subnodes) = @{$node};
my $name = ($opts->{'type'} eq 'ordered' ? 'enumerate' : 'itemize');
return
"\n\n".'\begin{'.$name.'}'."\n".
join('', map {'\item '.$self->transform_nodes($_)."\n\n"} @subnodes).
( run in 1.005 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )