| 1 | # ABSTRACT: pack novel/bbs content to ebook | ||||||||||||||||||||||||||||||||
| 2 | package Novel::Robot::Packer; | ||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Encode::Locale; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Encode; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use File::Temp qw/tempfile /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Template; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = 0.21; | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | my ( $self, %opt ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | $opt{type} ||= 'html'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"type"} ||= "html"
MC/DC0%0/1
$opt{"type"} ||= "html"
$opt{"type"}
Truth table50%1/2
A
A= $opt{"type"}
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 3 | bless {%opt}, __PACKAGE__; | |||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | sub set_type { | ||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | my ( $self, $output, $o ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
set_type: called CC=4 SCAR=17.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | if ( exists $o->{type} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 25 | 0 | return $o->{type}; | |||||||||||||||||||||||||||||||
| 26 | ▶ | } elsif ( defined $output | |||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $output and not exists $o->{"output_scalar"}
MC/DC0%0/2
defined $output and not exists $o->{"output_scalar"}
defined $output
not exists $o->{"output_scalar"}
Truth table33%1/3
A and not(B)
A= defined $outputB= not exists $o->{"output_scalar"}
| |||||||||||||||||||||||||||||||||
| 27 | and ( !exists $o->{output_scalar} ) ) { | ||||||||||||||||||||||||||||||||
| 28 | 0 | ( $o->{type} ) = $output =~ m#\.([^.]+)$#; | |||||||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||||||
| 30 | 1 | $o->{type} = 'html'; | |||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 1 | return $o->{type}; | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | sub detect_writer_book_type { | ||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my ( $self, $input, $o ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
detect_writer_book_type: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 0 | $input = decode( locale => $input ); | |||||||||||||||||||||||||||||||
| 40 | 0 | my ( $writer, $book, $type ) = $input =~ /([^\\\/]+?)-([^\\\/]+?)\.([^.\\\/]+)$/; | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $writer = $o->{writer} if ( defined $o->{writer} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $writer //= ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$writer //= ""
MC/DC0%0/1
$writer //= ""
$writer
Truth table0%0/2
A
A= $writer
| |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $book = $o->{book} if ( defined $o->{book} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $book //= ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$book //= ""
MC/DC0%0/1
$book //= ""
$book
Truth table0%0/2
A
A= $book
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 0 | return ( $writer, $book, $type ); | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | sub convert_novel { | ||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my ( $self, $input, $output, $o ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
convert_novel: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return $input unless ( -f $input and -s $input ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $input and -s $input
MC/DC0%0/2
-f $input and -s $input
-f $input
-s $input
Truth table0%0/3
A and B
A= -f $inputB= -s $input
| |||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 0 | my ( $writer, $book, $in_type ) = $self->detect_writer_book_type( $input, $o ); | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( !defined $output ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | 0 | $output = $input; | |||||||||||||||||||||||||||||||
| 60 | 0 | $output =~ s/\.([^.]+)$/.$o->{type}/; | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return $input if ( $output eq $input ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 0 | my ( $out_type ) = $output =~ m#\.([^.]+)$#; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 0 | my %conv = ( | |||||||||||||||||||||||||||||||
| 67 | 'authors' => $writer, | ||||||||||||||||||||||||||||||||
| 68 | 'author-sort' => $writer, | ||||||||||||||||||||||||||||||||
| 69 | 'title' => $book, | ||||||||||||||||||||||||||||||||
| 70 | 'chapter-mark' => "none", | ||||||||||||||||||||||||||||||||
| 71 | 'page-breaks-before' => "/", | ||||||||||||||||||||||||||||||||
| 72 | 'max-toc-links' => 0, | ||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 0 | my $conv_str = join( " ", map { qq[--$_ "$conv{$_}"] } keys( %conv ) ); | |||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ( $o->{type} =~ /\.epub$/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 77 | 0 | $conv_str .= " --dont-split-on-page-breaks "; | |||||||||||||||||||||||||||||||
| 78 | } elsif ( $o->{type} =~ /\.mobi$/ ) { | ||||||||||||||||||||||||||||||||
| 79 | 0 | $conv_str .= " --mobi-keep-original-images "; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | $conv_str = encode( locale => $conv_str ); | |||||||||||||||||||||||||||||||
| 83 | 0 | my $cmd = qq[ebook-convert "$input" "$output" $conv_str]; | |||||||||||||||||||||||||||||||
| 84 | #print "$cmd\n"; | ||||||||||||||||||||||||||||||||
| 85 | #system($cmd); | ||||||||||||||||||||||||||||||||
| 86 | 0 | `$cmd`; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 0 | return $output; | |||||||||||||||||||||||||||||||
| 89 | } ## end sub convert_novel | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | sub main { | ||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | my ( $self, $book_ref, $o ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
main: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 1 | $self->set_type( $o->{output}, $o ); | |||||||||||||||||||||||||||||||
| 95 | 1 | $self->format_item_output( $book_ref, $o ); | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | if ( $o->{type} ne 'html' and ( !exists $o->{output_scalar} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$o->{"type"} ne "html" and not exists $o->{"output_scalar"}
MC/DC0%0/2
$o->{"type"} ne "html" and not exists $o->{"output_scalar"}
$o->{"type"} ne "html"
not exists $o->{"output_scalar"}
Truth table33%1/3
A and not(B)
A= $o->{"type"} ne "html"B= not exists $o->{"output_scalar"}
| |||||||||||||||||||||||||||||||||
| 98 | 0 | my ( $fh, $html_f ) = tempfile( "run_novel-html-XXXXXXXXXXXXXX", TMPDIR => 1, SUFFIX => ".html" ); | |||||||||||||||||||||||||||||||
| 99 | 0 | $self->process_html_template( $book_ref, $html_f, $o ); | |||||||||||||||||||||||||||||||
| 100 | 0 | $self->convert_novel( $html_f, $o->{output}, { type => $o->{type}, writer => $book_ref->{writer}, book => $book_ref->{book} } ); | |||||||||||||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||||||||||||||
| 102 | 1 | $self->process_html_template( $book_ref, $o->{output}, $o ); | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 1 | return $o->{output}; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | sub format_item_output { | ||||||||||||||||||||||||||||||||
| 109 | 1 | ▶ | my ( $self, $book_ref, $o ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
format_item_output: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | if ( !$o->{output} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 1 | my $html = ''; | |||||||||||||||||||||||||||||||
| 112 | $o->{output} = | ||||||||||||||||||||||||||||||||
| 113 | exists $o->{output_scalar} | ||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | ? \$html | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | : $self->format_default_filename( $book_ref, $o ); | ||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | 1 | return $o->{output}; | |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | sub format_default_filename { | ||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my ( $self, $r, $o ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
format_default_filename: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 0 | my $item_info = ''; | |||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ( exists $o->{min_item_num} and $o->{min_item_num} > 1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
exists $o->{"min_item_num"} and $o->{"min_item_num"} > 1
MC/DC0%0/2
exists $o->{"min_item_num"} and $o->{"min_item_num"} > 1
exists $o->{"min_item_num"}
$o->{"min_item_num"} > 1
Truth table0%0/3
A and B
A= exists $o->{"min_item_num"}B= $o->{"min_item_num"} > 1
| |||||||||||||||||||||||||||||||||
| 125 | 0 | $item_info = "-$o->{min_item_num}"; | |||||||||||||||||||||||||||||||
| 126 | ▶ | } elsif ( exists $o->{min_page_num} and $o->{min_page_num} > 1 ) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
exists $o->{"min_page_num"} and $o->{"min_page_num"} > 1
MC/DC0%0/2
exists $o->{"min_page_num"} and $o->{"min_page_num"} > 1
exists $o->{"min_page_num"}
$o->{"min_page_num"} > 1
Truth table0%0/3
A and B
A= exists $o->{"min_page_num"}B= $o->{"min_page_num"} > 1
| |||||||||||||||||||||||||||||||||
| 127 | 0 | $item_info = "-$o->{min_page_num}"; | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | 0 | my $f = "$r->{writer}-$r->{book}$item_info." . $o->{type}; | |||||||||||||||||||||||||||||||
| 130 | 0 | $f =~ s{[\[\]/><\\`;'\$^*\(\)\%#@!"&:\?|\s^,~]}{}g; | |||||||||||||||||||||||||||||||
| 131 | 0 | return encode( locale => $f ); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | sub process_html_template { | ||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | my ( $self, $book_ref, $dst, $o ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
process_html_template: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | my $toc = $o->{with_toc} | ||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | ? qq{<div id="toc"><ul> | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | [% FOREACH r IN item_list %] | ||||||||||||||||||||||||||||||||
| 140 | [% IF r.content %] <li>[% r.id %]. <a href="#toc[% r.id %]">[% r.writer %] [% r.title %]</a></li> [% END %] | ||||||||||||||||||||||||||||||||
| 141 | [% END %] | ||||||||||||||||||||||||||||||||
| 142 | </ul> | ||||||||||||||||||||||||||||||||
| 143 | </div>} | ||||||||||||||||||||||||||||||||
| 144 | : ''; | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 1 | my $txt = <<__HTML__; | |||||||||||||||||||||||||||||||
| 147 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | ||||||||||||||||||||||||||||||||
| 148 | <html> | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | <head> | ||||||||||||||||||||||||||||||||
| 151 | <meta charset="UTF-8"> | ||||||||||||||||||||||||||||||||
| 152 | <meta property="opf.authors" content="[% writer %]"> | ||||||||||||||||||||||||||||||||
| 153 | <meta property="opf.titlesort" content="[% book %]"> | ||||||||||||||||||||||||||||||||
| 154 | <title> [% writer %] 《 [% book %] 》</title> | ||||||||||||||||||||||||||||||||
| 155 | <style type="text/css"> | ||||||||||||||||||||||||||||||||
| 156 | body { | ||||||||||||||||||||||||||||||||
| 157 | font-size: large; | ||||||||||||||||||||||||||||||||
| 158 | margin: 1em 8em 1em 8em; | ||||||||||||||||||||||||||||||||
| 159 | text-indent: 2em; | ||||||||||||||||||||||||||||||||
| 160 | line-height: 145%; | ||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | #title, .chapter { | ||||||||||||||||||||||||||||||||
| 163 | border-bottom: 0.2em solid #ee9b73; | ||||||||||||||||||||||||||||||||
| 164 | margin: 0.8em 0.2em 0.8em 0.2em; | ||||||||||||||||||||||||||||||||
| 165 | text-indent: 0em; | ||||||||||||||||||||||||||||||||
| 166 | font-size: xx-large; | ||||||||||||||||||||||||||||||||
| 167 | font-weight: bold; | ||||||||||||||||||||||||||||||||
| 168 | padding-bottom: 0.25em; | ||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | #title, ol { line-height: 150%; } | ||||||||||||||||||||||||||||||||
| 171 | #title { text-align: center; } | ||||||||||||||||||||||||||||||||
| 172 | </style> | ||||||||||||||||||||||||||||||||
| 173 | </head> | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | <body> | ||||||||||||||||||||||||||||||||
| 176 | <div id="title"><a href="[% writer_url %]">[% writer %]</a> 《 <a href="[% url %]">[% book %]</a> 》</div> | ||||||||||||||||||||||||||||||||
| 177 | $toc | ||||||||||||||||||||||||||||||||
| 178 | <div id="content"> | ||||||||||||||||||||||||||||||||
| 179 | [% FOREACH r IN item_list %] | ||||||||||||||||||||||||||||||||
| 180 | [% IF r.content %] | ||||||||||||||||||||||||||||||||
| 181 | <div class="item"> | ||||||||||||||||||||||||||||||||
| 182 | <div class="chapter">[% r.id %]. <a name="toc[% r.id %]">[% r.writer %] [% r.title %]</a> [% r.time %]</div> | ||||||||||||||||||||||||||||||||
| 183 | <div class="content">[% r.content %]</div> | ||||||||||||||||||||||||||||||||
| 184 | </div>[% END %] | ||||||||||||||||||||||||||||||||
| 185 | [% END %] | ||||||||||||||||||||||||||||||||
| 186 | </div> | ||||||||||||||||||||||||||||||||
| 187 | </body> | ||||||||||||||||||||||||||||||||
| 188 | </html> | ||||||||||||||||||||||||||||||||
| 189 | __HTML__ | ||||||||||||||||||||||||||||||||
| 190 | 1 | my $tt = Template->new(); | |||||||||||||||||||||||||||||||
| 191 | 1 | ▶ | $tt->process( \$txt, $book_ref, $dst, { binmode => ':utf8' } ) || die $tt->error(), "\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 192 | 1 | return $dst; | |||||||||||||||||||||||||||||||
| 193 | } ## end sub process_html_template | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 3 | 1; | |||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||