Generator/Exporter/PDF.pm

filter: Statementstmt 26.1% 34 / 130 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 44.0% 11 / 25 Podpod 0.0% 0 / 5 totaltotal 21.2% 45 / 212 CC 22 SCAR 29.2
CC
22
Cov
20%
__ANON__
56.0
__ANON__
30.0
writeEBook
30.0
CRAP 18.6
« Generator/Parser.pm ↑ Summary Generator/Exporter/XHTML.pm »
1 package EBook::Generator::Exporter::PDF;
2  
3 1 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Image::Size;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use LaTeX::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use EBook::Generator::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $VERSION = '0.01';
18  
19 sub new
20 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my ($class, @args) = @_;
22 0 my $self = bless {}, $class;
23 0 return $self->init(@args);
24 }
25  
26 sub init
27 0 ▶{
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my ($self, $browser) = @_;
29  
30 0 $self->{'final-dir'} = '/tmp/';
31 0 $self->{'browser'} = $browser;
32 0 $self->{'log'} = [];
33  
34 $self->{'handlers'} = {
35 # garamond shapes:
36 #
37 # {\garamond normal}
38 # {\slshape This is garamond slanted} \\
39 # {\bfseries This is garamond bold face} \\
40 # {\scshape This is in small caps} \\
41 # {\slshape \bfseries This is slanted and bold face} \\
42  
43 0 ▶ 'document' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
44 0 my ($self, $node) = @_;
45 0 my ($type, $opts, @subnodes) = @{$node};
46 0 my $content = $self->transform_nodes(@subnodes);
47 return
48 '\title{'.$self->{'meta'}->{'title'}.' \footnote{'.$self->transform_text($self->{'meta'}->{'url'}).'}}'."\n".
49 '\author{'.$self->{'meta'}->{'author'}.'}'."\n".
50 '\date{'.$self->{'meta'}->{'date'}.'}'."\n".
51
52 '\documentclass[12pt,a6paper]{article}'."\n".
53 '\usepackage[left=5mm,right=5mm,top=5mm,bottom=5mm,nohead,includefoot]{geometry}'."\n".
54 '\footnotesep=0mm'."\n".
55
56 '\parskip=5mm'."\n".
57 '\parindent=0mm'."\n".
58 #'\sloppy'."\n".
59
60 #'\renewcommand{\familydefault}{\sfdefault}'."\n".
61 '\usepackage{garamond}'."\n".
62 '\usepackage{lmodern}'."\n".
63 '\usepackage{pslatex}'."\n".
64 '\usepackage[T1]{fontenc}'."\n".
65
66 # no margins in table of contents
67 '\usepackage{tocloft}'."\n".
68 '\renewcommand{\cftsecindent}{0em}'."\n".
69 '\renewcommand{\cftsecnumwidth}{1.9em}'."\n".
70 '\renewcommand{\cftsubsecindent}{1.9em}'."\n".
71 '\renewcommand{\cftsubsecnumwidth}{2.8em}'."\n".
72 '\renewcommand{\cftsubsubsecindent}{0em}'."\n".
73 '\renewcommand{\cftsubsubsecnumwidth}{3.2em}'."\n".
74
75 '\usepackage{graphicx}'."\n".
76
77 '\usepackage{paralist}'."\n".
78 '\setdefaultleftmargin{1.3em}{}{}{}{.5em}{.5em}'."\n".
79
80 '\usepackage{listings}'."\n".
81 '\lstset{numbers=left, numberstyle=\tiny, numbersep=5pt} \lstset{language=Perl}'."\n".
82
83 '\begin{document}'."\n".
84
85 '\garamond'."\n".
86 '\maketitle'."\n".
87 '\newpage'."\n".
88
89 '\garamond'."\n".
90 0 ▶ ($self->{'has-section'} > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$self->{'has-section'} > 1 ? : 0 0 0
91 '\tableofcontents'."\n".
92 '\newpage'."\n" : '').
93
94 '\garamond'."\n".
95 $content.
96 '\end{document}'."\n";
97 },
98 0 ▶ 'paragraph' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 my ($self, $node) = @_;
100 0 my ($type, $opts, @subnodes) = @{$node};
101 0 return "\n\n".$self->transform_nodes(@subnodes)."\n\n";
102 },
103 0 ▶ 'headline' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
104 0 my ($self, $node) = @_;
105 0 $self->{'has-section'} = 1;
106 0 my ($type, $opts, @subnodes) = @{$node};
107 my $name =
108 ($opts->{'level'} == 1 ? 'section' :
109 ($opts->{'level'} == 2 ? 'subsection' :
110 ($opts->{'level'} == 3 ? 'subsubsection' :
111 0 ▶ ($opts->{'level'} == 4 ? 'paragraph' :
Branch: 4 branches
BranchTrueFalseTotal
$opts->{'level'} == 1 ? : 0 0 0
$opts->{'level'} == 2 ? : 0 0 0
$opts->{'level'} == 3 ? : 0 0 0
$opts->{'level'} == 4 ? : 0 0 0
112 'subparagraph'))));
113 0 return "\n\n".'\\'.$name.'{'.$self->transform_nodes(@subnodes)."}\n\n";
114 },
115 0 ▶ 'text' => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
116 0 my ($self, $node) = @_;
117 0 my ($type, $opts, @subnodes) = @{$node};
118 0 my $latex = $self->transform_nodes(@subnodes);
119 $latex = ' {\footnotesize\lstinline|'.$latex.'|} '
120 0 ▶ if exists $opts->{'preformatted'} && $opts->{'preformatted'} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"preformatted"} and $opts->{"preformatted"} == 1 0 0 0
Condition0%0/3
exists $opts->{"preformatted"} and $opts->{"preformatted"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"preformatted"} and $opts->{"preformatted"} == 1
exists $opts->{"preformatted"} $opts->{"preformatted"} == 1
Truth table0%0/3
A and B
A= exists $opts->{"preformatted"}B= $opts->{"preformatted"} == 1
ABresult
0X0
100
111
121 $latex = ' \textbf{'.$latex.'} '
122 0 ▶ if exists $opts->{'weight'} && $opts->{'weight'} eq 'bold';
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"weight"} and $opts->{"weight"} eq "bold" 0 0 0
Condition0%0/3
exists $opts->{"weight"} and $opts->{"weight"} eq "bold"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"weight"} and $opts->{"weight"} eq "bold"
exists $opts->{"weight"} $opts->{"weight"} eq "bold"
Truth table0%0/3
A and B
A= exists $opts->{"weight"}B= $opts->{"weight"} eq "bold"
ABresult
0X0
100
111
123 $latex = ' \textit{'.$latex.'} '
124 0 ▶ if exists $opts->{'style'} && $opts->{'style'} eq 'italic';
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"style"} and $opts->{"style"} eq "italic" 0 0 0
Condition0%0/3
exists $opts->{"style"} and $opts->{"style"} eq "italic"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"style"} and $opts->{"style"} eq "italic"
exists $opts->{"style"} $opts->{"style"} eq "italic"
Truth table0%0/3
A and B
A= exists $opts->{"style"}B= $opts->{"style"} eq "italic"
ABresult
0X0
100
111
125 0 return $latex;
126 },
127 0 ▶ 'link' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 my ($self, $node) = @_;
129 0 my ($type, $opts, @subnodes) = @{$node};
130 return
131 ' '.$self->transform_nodes(@subnodes).
132 0 '\footnote{'.$self->transform_text($opts->{'target'})."}";
133
134 },
135 0 ▶ 'media' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
136 0 my ($self, $node) = @_;
137 0 my ($type, $opts, @subnodes) = @{$node};
138 0 ▶ if ($opts->{'type'} =~ /^(gif|jpe?g|png)$/i && -f $opts->{'filename'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"type"} =~ /^(gif|jpe?g|png)$/i and -f $opts->{"filename"}) 0 0 0
Condition0%0/3
$opts->{"type"} =~ /^(gif|jpe?g|png)$/i and -f $opts->{"filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"type"} =~ /^(gif|jpe?g|png)$/i and -f $opts->{"filename"}
$opts->{"type"} =~ /^(gif|jpe?g|png)$/i -f $opts->{"filename"}
Truth table0%0/3
A and B
A= $opts->{"type"} =~ /^(gif|jpe?g|png)$/iB= -f $opts->{"filename"}
ABresult
0X0
100
111
139 # picture
140 return
141 "\n\n".
142 '\includegraphics[width=\columnwidth,keepaspectratio=true]{'.
143 0 $opts->{'filename'}."}".
144 "\n\n";
145 }
146 0 return '';
147 },
148 0 ▶ 'list' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
149 0 my ($self, $node) = @_;
150 0 my ($type, $opts, @subnodes) = @{$node};
151 0 ▶ my $name = ($opts->{'type'} eq 'ordered' ? 'enumerate' : 'itemize');
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'type'} eq 'ordered' ? : 0 0 0
152 return
153 "\n\n".'\begin{'.$name.'}'."\n".
154 0 join('', map {'\item '.$self->transform_nodes($_)."\n\n"} @subnodes).
155 '\end{'.$name.'}'."\n\n";
156 },
157 0 ▶ 'preformatted' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 my ($self, $node) = @_;
159 0 my ($type, $opts, @subnodes) = @{$node};
160 return
161 0 "\n\n".'{\footnotesize\begin{lstlisting}{Name}'.
162 $self->transform_nodes(@subnodes).
163 '\end{lstlisting}}'."\n\n";
164 },
165 0 ▶ 'quote' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 my ($self, $node) = @_;
167 0 my ($type, $opts, @subnodes) = @{$node};
168 0 return '\textit{'.$self->transform_nodes(@subnodes)."}";
169 },
170 0 };
171
172 0 return $self;
173 }
174  
175 sub writeEBook
176 0 ▶{
Subroutine writeEBook: not called CC=5 SCAR=34.0
Pod undocumented
177 0 my ($self, $ebook, $options) = @_;
178  
179 0 $self->{'log'} = [];
180 0 $self->{'url'} = $ebook->{'url'};
181 0 $self->{'options'} = $options;
182  
183 # used while processing
184 0 $self->{'has-section'} = 0;
185  
186 # convert to latex
187 0 $self->{'meta'} = $ebook->{'meta'};
188 0 my $converted = $self->transform_nodes($ebook->{'data'});
189
190 0 my $tmpdir = File::Temp->newdir();
191  
192 # write tex file
193 0 my $filename = $tmpdir->dirname()."/main.tex";
194 0 ▶ unlink($filename) if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 0 0
195 0 my $fh = IO::File->new("> ".$filename);
196 0 binmode($fh);
197 0 print $fh $converted;
198
199 # create pdf
200 0 print "creating pdf...";
201  
202 my $cmd =
203 # temporarily set path of local texmf tree to make
204 # it able to find garamond files ;)
205 'cd "'.$tmpdir->dirname().'"; '.
206 "TEXMFHOME='".$self->{'options'}->{'local-tex-tree-path'}."' ".
207 #EBook::Generator::Parser::getCommandPath('pdflatex').' '.
208 (-f '/usr/bin/pdflatex' ? '/usr/bin/pdflatex' : '/usr/texbin/pdflatex ').' '.
209 0 ▶ ($ebook->{'options'}->{'debug'} == 1 ? () : '-interaction=batchmode').' '.
Branch: 2 branches
BranchTrueFalseTotal
-f '/usr/bin/pdflatex' ? : 0 0 0
$ebook->{'options'}{'debug'} == 1 ? : 0 0 0
210 'main.tex'.
211 ' 2>&1';
212 #' > /dev/null 2>&1';
213
214 0 my $output = `$cmd`;
215 0 ▶ $output .= `$cmd` if $self->{'has-section'}; # second time for table-of-contents
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"has-section"} 0 0 0
216
217 # copy pdf to final place
218 0 my $pdf_filename = $tmpdir->dirname().'/main.pdf';
219 0 my $final_filename = $self->{'final-dir'}.$ebook->{'meta'}->{'title'}.'.pdf';
220 0 my $copy_result = copy($pdf_filename, $final_filename);
221
222 0 push @{$self->{'log'}},
223 'dummy'
224 # 'filename = '.$filename.' (exist: '.(-f $filename).')',
225 # 'cmd = '.$cmd,
226 # 'cmd-output = '.$output,
227 # 'which-pdflatex = '.`which pdflatex`,
228 # 'pdf-filename = '.$pdf_filename.' (exist: '.(-f $pdf_filename).')',
229 # 'final-filename = '.$final_filename.' (exist: '.(-f $final_filename).')',
230 # 'copy-result = '.$copy_result.' (error: '.$!.')',
231 ;
232
233 0 return $final_filename;
234 }
235  
236 sub transform_nodes
237 0 ▶{
Subroutine transform_nodes: not called CC=4 SCAR=30.0
Pod undocumented
238 0 my ($self, @nodes) = @_;
239 0 my $conv = '';
240 0 foreach my $node (@nodes) {
241 0 ▶ if (ref $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 0 0 0
242 0 my $type = $node->[0];
243 0 ▶ if (exists $self->{'handlers'}->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'handlers'}{$type}) { } 0 0 0
244 0 $conv .= $self->{'handlers'}->{$type}->($self, $node);
245 } else {
246 0 my ($type, $opts, @subnodes) = @{$node};
247 0 $conv .= $self->transform_nodes(@subnodes);
248 }
249 }
250 else {
251 0 $conv .= $self->transform_text($node);
252 }
253 }
254 0 return $conv;
255 }
256  
257 sub transform_text
258 0 ▶{
Subroutine transform_text: not called CC=1 SCAR=6.9
Pod undocumented
259 0 my ($self, $s) = @_;
260  
261 0 $s =~ s/([\.\;\?\!\:])/$1 /g;
262  
263 0 $s = decode_entities($s);
264 0 $s = latex_encode($s);
265  
266 # make URLs shorter by adding split-marks
267 #$s =~ s/\/(?!\/)/\\-\/\\-/g;
268
269 0 return $s;
270 }
271  
272 1 1;
273 __END__
« Generator/Parser.pm ↑ Summary Generator/Exporter/XHTML.pm »