Generator/Exporter/XHTML.pm

filter: Statementstmt 14.6% 16 / 109 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 26.3% 5 / 19 Podpod 0.0% 0 / 5 totaltotal 12.3% 21 / 170 CC 16 SCAR 28.4
CC
16
Cov
11%
__ANON__
56.0
transform_nodes
20.0
__ANON__
12.0
CRAP 17.1
« Generator/Exporter/PDF.pm ↑ Summary Generator/Exporter.pm »
1 package EBook::Generator::Exporter::XHTML;
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 MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.01';
10  
11 sub new
12 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
13 0 my ($class, @args) = @_;
14 0 my $self = bless {}, $class;
15 0 return $self->init(@args);
16 }
17  
18 sub init
19 0 ▶{
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
20 0 my ($self, $browser) = @_;
21  
22 0 $self->{'final-dir'} = '/tmp/';
23  
24 0 $self->{'browser'} = $browser;
25
26 0 $self->{'log'} = [];
27  
28 $self->{'handlers'} = {
29 0 ▶ 'document' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
30 0 my ($self, $node) = @_;
31 0 my ($type, $opts, @subnodes) = @{$node};
32 0 my $content = $self->transform_nodes(@subnodes);
33 0 ▶ $self->{'meta'}->{'date'} = time() if $self->{'meta'}->{'date'} =~ /\\today/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"meta"}{"date"} =~ /\\today/ 0 0 0
34 return
35 '<html>'."\n".
36 '<head>'."\n".
37 '<title>'.$self->{'meta'}->{'title'}.'</title>'."\n".
38 '<style type="text/css">'."\n".
39 'body { padding: 10pt } '."\n".
40 'body, p, b, i, a, h1, h2, h3, h4, h5 { font-family: Georgia, serif; } '."\n".
41 'h1, h2, h3, h4, h5, ol, ul, li { margin: 5pt 0 }'."\n".
42 '</style>'."\n".
43 '</head>'."\n".
44 '<body>'."\n".
45 '<center>'."\n".
46 $self->{'meta'}->{'title'}.'<br/>'."\n".
47 'by <i>'.$self->{'meta'}->{'author'}.'</i><br/>'."\n".
48 $self->{'meta'}->{'date'}.'<br/><br/>'."\n".
49 0 $self->transform_text($self->{'meta'}->{'url'})."\n".
50 '</center>'."\n".
51 $content."\n".
52 '</body>'."\n".
53 '</html>'."\n";
54 },
55 0 ▶ 'paragraph' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 my ($self, $node) = @_;
57 0 my ($type, $opts, @subnodes) = @{$node};
58 0 return "<p>".$self->transform_nodes(@subnodes)."</p>\n";
59 },
60 0 ▶ 'headline' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my ($self, $node) = @_;
62 0 my ($type, $opts, @subnodes) = @{$node};
63 return
64 "\n\n".
65 '<h'.$opts->{'level'}.'>'.
66 $self->transform_nodes(@subnodes).
67 0 "</h".$opts->{'level'}.">\n\n";
68 },
69 0 ▶ 'text' => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
70 0 my ($self, $node) = @_;
71 0 my ($type, $opts, @subnodes) = @{$node};
72 0 my $latex = $self->transform_nodes(@subnodes);
73 $latex = '<code>'.$latex.'</code>'
74 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
75 $latex = '<b>'.$latex.'</b>'
76 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
77 $latex = '<i>'.$latex.'</i>'
78 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
79 0 return ' '.$latex;
80 },
81 0 ▶ 'link' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my ($self, $node) = @_;
83 0 my ($type, $opts, @subnodes) = @{$node};
84 return
85 0 ' <a href="'.$self->transform_text($opts->{'target'}).'">'.
86 $self->transform_nodes(@subnodes).
87 '</a>';
88
89 },
90 0 ▶ 'media' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
91 0 my ($self, $node) = @_;
92 0 my ($type, $opts, @subnodes) = @{$node};
93 0 ▶ if ($opts->{'type'} =~ /^(gif|jpe?g|png)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"type"} =~ /^(gif|jpe?g|png)$/i) 0 0 0
94 # picture
95 0 ▶ if ($self->{'options'}->{'embed-images'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{'embed-images'}) { } 0 0 0
96 0 my $fh = IO::File->new('< '.$opts->{'filename'});
97 0 my $data = join '', <$fh>;
98 return
99 "<p>".
100 #'<img width="100%" alt="Image" src="'.$opts->{'url'}.'"/>'.
101 0 '<img width="100%" alt="Image" src="data:image/'.$opts->{'type'}.
102 ';base64,'.encode_base64($data).'"/>'.
103 "</p>\n";
104 } else {
105 return
106 "<p>".
107 0 '<img width="100%" alt="Image" src="'.$opts->{'url'}.'"/>'.
108 "</p>\n";
109 }
110 }
111 0 return '';
112 },
113 0 ▶ 'list' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
114 0 my ($self, $node) = @_;
115 0 my ($type, $opts, @subnodes) = @{$node};
116 0 ▶ my $name = ($opts->{'type'} eq 'ordered' ? 'ol' : 'ul');
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'type'} eq 'ordered' ? : 0 0 0
117 return
118 '<'.$name.'>'."\n".
119 0 join('', map {'<li>'.$self->transform_nodes($_)."</li>\n"} @subnodes).
120 '</'.$name.'>'."\n";
121 },
122 0 ▶ 'preformatted' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 0 my ($self, $node) = @_;
124 0 my ($type, $opts, @subnodes) = @{$node};
125 return
126 0 ' <pre>'.
127 $self->transform_nodes(@subnodes).
128 '</pre> '."\n";
129 },
130 0 ▶ 'quote' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 my ($self, $node) = @_;
132 0 my ($type, $opts, @subnodes) = @{$node};
133 0 return ' <quote>'.$self->transform_nodes(@subnodes).'</quote> '."\n";
134 },
135 0 };
136
137 0 return $self;
138 }
139  
140 sub writeEBook
141 0 ▶{
Subroutine writeEBook: not called CC=3 SCAR=22.3
Pod undocumented
142 0 my ($self, $ebook, $options) = @_;
143  
144 0 $self->{'url'} = $ebook->{'url'};
145 0 $self->{'options'} = $options;
146 0 $self->{'meta'} = $ebook->{'meta'};
147  
148 # convert to xhtml
149 0 my $converted = $self->transform_nodes($ebook->{'data'});
150
151 # wrap xhtml inside an EPUB container
152 0 my $filename;
153 0 ▶ if ($self->{'options'}->{'epub'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{'epub'}) { } 0 0 0
154 1 ▶ use EBook::EPUB;
Subroutine BEGIN: called CC=1 SCAR=0.0
155
156 0 my $epub = EBook::EPUB->new();
157  
158 # Set metadata: title/author/language/id
159 0 $epub->add_title($self->{'meta'}->{'title'});
160 0 $epub->add_author($self->{'meta'}->{'author'});
161 0 $epub->add_date($self->{'meta'}->{'date'});
162 #$epub->add_language('en');
163 #$epub->add_identifier('1440465908', 'ISBN');
164  
165 # Add package content: stylesheet, font, xhtml and cover
166 #$epub->copy_stylesheet('/path/to/style.css', 'style.css');
167 #$epub->copy_file('/path/to/figure1.png', 'figure1.png', 'image/png');
168 #$epub->encrypt_file('/path/to/CharisSILB.ttf', 'CharisSILB.ttf', 'application/x-font-ttf');
169 #my $chapter_id = $epub->copy_xhtml('/path/to/page1.xhtml', 'page1.xhtml');
170
171 # note: $epub->copy_xhtml(...) seems to be buggy
172 0 $epub->add_xhtml('index.xhtml', $converted, linear => 'no');
173  
174 # Add top-level nav-point
175 #my $navpoint = $epub->add_navpoint(
176 # label => "Chapter 1",
177 # id => $chapter_id,
178 # content => 'page1.xhtml',
179 # play_order => 1 # should always start with 1
180 #);
181  
182 # Generate resulting ebook
183 0 $filename = $self->{'final-dir'}.$ebook->{'meta'}->{'title'}.".epub";
184 0 $epub->pack_zip($filename);
185 }
186 else {
187 # write xhtml file
188 0 $filename = $self->{'final-dir'}.$ebook->{'meta'}->{'title'}.".html";
189 0 ▶ unlink($filename) if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 0 0
190 0 my $fh = IO::File->new("> ".$filename);
191 0 print $fh $converted;
192 }
193 0 print "have fun with: ".$filename."\n";
194 0 return $filename;
195 }
196  
197 sub transform_nodes
198 0 ▶{
Subroutine transform_nodes: not called CC=4 SCAR=30.0
Pod undocumented
199 0 my ($self, @nodes) = @_;
200 0 my $conv = '';
201 0 foreach my $node (@nodes) {
202 0 ▶ if (ref $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 0 0 0
203 0 my $type = $node->[0];
204 0 ▶ if (exists $self->{'handlers'}->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'handlers'}{$type}) { } 0 0 0
205 0 $conv .= $self->{'handlers'}->{$type}->($self, $node);
206 } else {
207 0 my ($type, $opts, @subnodes) = @{$node};
208 0 $conv .= $self->transform_nodes(@subnodes);
209 }
210 }
211 else {
212 0 $conv .= $self->transform_text($node);
213 }
214 }
215 0 return $conv;
216 }
217  
218 sub transform_text
219 0 ▶{
Subroutine transform_text: not called CC=1 SCAR=6.9
Pod undocumented
220 0 my ($self, $s) = @_;
221 # ...
222 0 return $s;
223 }
224  
225 1 1;
226 __END__
« Generator/Exporter/PDF.pm ↑ Summary Generator/Exporter.pm »