| 1 | package EBook::Generator::Exporter::Text; | ||||||||||
| 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 Text::Wrap; | ||||||||
|
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
| |||||||||||
| 34 | return | ||||||||||
| 35 | $self->{'meta'}->{'title'}."\n\n". | ||||||||||
| 36 | 'by '.$self->{'meta'}->{'author'}."\n". | ||||||||||
| 37 | $self->{'meta'}->{'date'}."\n". | ||||||||||
| 38 | 0 | $self->transform_text($self->{'meta'}->{'url'})."\n\n". | |||||||||
| 39 | $content; | ||||||||||
| 40 | }, | ||||||||||
| 41 | 0 | ▶ | 'paragraph' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 42 | 0 | my ($self, $node) = @_; | |||||||||
| 43 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 44 | 0 | return wrap('','',$self->transform_nodes(@subnodes))."\n\n"; | |||||||||
| 45 | }, | ||||||||||
| 46 | 0 | ▶ | 'headline' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 47 | 0 | my ($self, $node) = @_; | |||||||||
| 48 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 49 | 0 | return wrap('','',$self->transform_nodes(@subnodes))."\n\n"; | |||||||||
| 50 | }, | ||||||||||
| 51 | 0 | ▶ | 'text' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 52 | 0 | my ($self, $node) = @_; | |||||||||
| 53 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 54 | 0 | return ' '.$self->transform_nodes(@subnodes); | |||||||||
| 55 | }, | ||||||||||
| 56 | 0 | ▶ | 'link' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 57 | 0 | my ($self, $node) = @_; | |||||||||
| 58 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 59 | return | ||||||||||
| 60 | ' '.$self->transform_nodes(@subnodes). | ||||||||||
| 61 | 0 | ' [see '.$self->transform_text($opts->{'target'}).'] '; | |||||||||
| 62 | }, | ||||||||||
| 63 | 0 | ▶ | 'media' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 64 | 0 | my ($self, $node) = @_; | |||||||||
| 65 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 66 | 0 | ▶ | if ($opts->{'type'} =~ /^(gif|jpe?g|png)$/i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | # picture | ||||||||||
| 68 | 0 | return "\n\n".'[Image: '.$opts->{'url'}.'"]'."\n\n"; | |||||||||
| 69 | } | ||||||||||
| 70 | 0 | return ''; | |||||||||
| 71 | }, | ||||||||||
| 72 | 0 | ▶ | 'list' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 73 | 0 | my ($self, $node) = @_; | |||||||||
| 74 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 75 | 0 | ▶ | my $name = ($opts->{'type'} eq 'ordered' ? 'ol' : 'ul'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | my $count = 0; | |||||||||
| 77 | return | ||||||||||
| 78 | join('', map { | ||||||||||
| 79 | 0 | $count++; | |||||||||
| 80 | 0 | "\n\n".$count.'.'.wrap('','',$self->transform_nodes($_))."\n\n"; | |||||||||
| 81 | } @subnodes); | ||||||||||
| 82 | }, | ||||||||||
| 83 | 0 | ▶ | 'preformatted' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 84 | 0 | my ($self, $node) = @_; | |||||||||
| 85 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 86 | 0 | return ' '.$self->transform_nodes(@subnodes); | |||||||||
| 87 | }, | ||||||||||
| 88 | 0 | ▶ | 'quote' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 89 | 0 | my ($self, $node) = @_; | |||||||||
| 90 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 91 | 0 | return ' '.$self->transform_nodes(@subnodes); | |||||||||
| 92 | }, | ||||||||||
| 93 | 0 | }; | |||||||||
| 94 | |||||||||||
| 95 | 0 | return $self; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | sub writeEBook | ||||||||||
| 99 | 0 | ▶ | { | ||||||||
|
Subroutine
writeEBook: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 100 | 0 | my ($self, $ebook, $options) = @_; | |||||||||
| 101 | |||||||||||
| 102 | 0 | $self->{'url'} = $ebook->{'url'}; | |||||||||
| 103 | 0 | $self->{'options'} = $options; | |||||||||
| 104 | |||||||||||
| 105 | # convert to xhtml | ||||||||||
| 106 | 0 | $self->{'meta'} = $ebook->{'meta'}; | |||||||||
| 107 | 0 | my $converted = $self->transform_nodes($ebook->{'data'}); | |||||||||
| 108 | |||||||||||
| 109 | # write xhtml file | ||||||||||
| 110 | 0 | my $filename = $self->{'final-dir'}.$ebook->{'meta'}->{'title'}.".txt"; | |||||||||
| 111 | 0 | ▶ | unlink($filename) if -f $filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 0 | my $fh = IO::File->new("> ".$filename); | |||||||||
| 113 | 0 | print $fh $converted; | |||||||||
| 114 | |||||||||||
| 115 | 0 | print "have fun with: ".$filename."\n"; | |||||||||
| 116 | 0 | return $filename; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | sub transform_nodes | ||||||||||
| 120 | 0 | ▶ | { | ||||||||
|
Subroutine
transform_nodes: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 121 | 0 | my ($self, @nodes) = @_; | |||||||||
| 122 | 0 | my $conv = ''; | |||||||||
| 123 | 0 | foreach my $node (@nodes) { | |||||||||
| 124 | 0 | ▶ | if (ref $node) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 0 | my $type = $node->[0]; | |||||||||
| 126 | 0 | ▶ | if (exists $self->{'handlers'}->{$type}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | $conv .= $self->{'handlers'}->{$type}->($self, $node); | |||||||||
| 128 | } else { | ||||||||||
| 129 | 0 | my ($type, $opts, @subnodes) = @{$node}; | |||||||||
| 130 | 0 | $conv .= $self->transform_nodes(@subnodes); | |||||||||
| 131 | } | ||||||||||
| 132 | } | ||||||||||
| 133 | else { | ||||||||||
| 134 | 0 | $conv .= $self->transform_text($node); | |||||||||
| 135 | } | ||||||||||
| 136 | } | ||||||||||
| 137 | 0 | return $conv; | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | sub transform_text | ||||||||||
| 141 | 0 | ▶ | { | ||||||||
|
Subroutine
transform_text: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 142 | 0 | my ($self, $s) = @_; | |||||||||
| 143 | # ... | ||||||||||
| 144 | 0 | return $s; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 1 | 1; | |||||||||
| 148 | __END__ | ||||||||||