lib/Novel/Robot/Parser/txt.pm

filter: Statementstmt 100% 92 / 92 Branchbran 68.7% 11 / 16 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 14 / 14 Podpod 20.0% 1 / 5 totaltotal 85.3% 122 / 143 CC 18 SCAR 17.9
CC
18
Cov
91%
read_single_txt
10.3
parse_novel
8.5
format_chapter_content
2.0
CRAP 6.0
« lib/Novel/Robot/Packer.pm ↑ Summary lib/Novel/Robot/Parser/jjwxc.pm »
1 # ABSTRACT: txt parser
2 =pod
3
4 =encoding utf8
5
6 =head1 FUNCTION
7
8 =head2 parse_novel
9
10 解析txt
11
12 my $txt_content_ref = $self->parse_novel(
13 [ '/somedir/', '/someotherdir/somefile.txt' ],
14 writer => 'some_writer',
15 book => 'some_book',
16 chapter_regex => qr/(第\d+章)/,
17 );
18
19
20 =cut
21 package Novel::Robot::Parser::txt;
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use base 'Novel::Robot::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
25
26 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Encode::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Encode::Detect::CJK qw/detect/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
31
32
33 sub parse_novel {
34 2 ▶ my ($self, $path, %opt) = @_;
Subroutine parse_novel: called CC=8 SCAR=21.4
Pod documented
35 2 ▶ $opt{chapter_regex} ||= get_default_chapter_regex();
Condition33%1/3
$opt{"chapter_regex"} ||= get_default_chapter_regex()
l!l&&r!l&&!r
020
MC/DC0%0/2
$opt{"chapter_regex"} ||= get_default_chapter_regex()
$opt{"chapter_regex"} get_default_chapter_regex()
Truth table33%1/3
A ||= B
A= $opt{"chapter_regex"}B= get_default_chapter_regex()
ABresult
1X1
011
000
36
37 2 my %data;
38 2 ▶ $data{writer} = $opt{writer} || 'unknown';
Condition50%1/2
$opt{'writer'} || 'unknown'
l!l
02
MC/DC0%0/1
$opt{'writer'} || 'unknown'
$opt{'writer'}
Truth table50%1/2
A
A= $opt{'writer'}
Aresult
11
00
39 2 ▶ $data{book} = $opt{book} || 'unknown';
Condition50%1/2
$opt{'book'} || 'unknown'
l!l
02
MC/DC0%0/1
$opt{'book'} || 'unknown'
$opt{'book'}
Truth table50%1/2
A
A= $opt{'book'}
Aresult
11
00
40
41 2 ▶ my $p_ref = ref($path) eq 'ARRAY' ? $path : [ $path ];
Branch: 1 branch
BranchTrueFalseTotal
ref $path eq 'ARRAY' ? : 0 2 2
42 2 for my $p (@$p_ref){
43 2 my @txts = sort File::Find::Rule->file()->in($p);
44 2 for my $txt (@txts){
45 2 my $txt_data_ref = $self->read_single_txt($txt, %opt);
46 2 my $txt_file = decode(locale => $txt);
47 2 for my $t (@$txt_data_ref){
48 #$t->{url} = $txt_file;
49 6 push @{$data{item_list}}, $t;
50 }
51 }
52 }
53
54 2 $self->update_item_list($data{item_list});
55
56 #$data{url} = '';
57
58 2 return \%data;
59 }
60
61 sub get_default_chapter_regex {
62 #指定分割章节的正则表达式
63
64 #序号
65 2 ▶ my $r_num =
Subroutine get_default_chapter_regex: called CC=1 SCAR=0.0
Pod undocumented
66 qr/[0123456789零○〇一二三四五六七八九十百千\d]+/;
67 2 my $r_split = qr/[上中下]/;
68 2 my $r_not_chap_head = qr/引子|楔子|尾声|内容简介|正文|番外|终章|序言|后记|文案/;
69
70 #第x章,卷x,第x章(大结局),尾声x
71 2 my $r_head = qr/(卷|第|$r_not_chap_head)?/;
72 2 my $r_tail = qr/(章|卷|回|部|折)?/;
73 2 my $r_post = qr/([.\s\-\(\/(]+.{0,35})?/;
74 2 my $regex_a = qr/(【?$r_head\s*$r_num\s*$r_tail$r_post】?)/;
75
76 #(1),(1)xxx
77 #xxx(1),xxx(1)yyy
78 #(1-上|中|下)
79 2 my $regex_b_index = qr/[((]$r_num[))]/;
80 2 my $regex_b_tail = qr/$regex_b_index\s*\S+/;
81 2 my $regex_b_head = qr/\S+\s*$regex_b_index.{0,10}/;
82 2 my $regex_b_split = qr/[((]$r_num[--]$r_split[))]/;
83 2 my $regex_b = qr/$regex_b_head|$regex_b_tail|$regex_b_index|$regex_b_split/;
84
85 #1、xxx,一、xxx
86 2 my $regex_c = qr/$r_num[、.. ].{0,10}/;
87
88 #第x卷 xxx 第x章 xxx
89 #第x卷/第x章 xxx
90 2 my $regex_d = qr/($regex_a(\s+.{0,10})?){2}/;
91
92 #后记 xxx
93 2 my $regex_e = qr/(【?$r_not_chap_head\s*$r_post】?)/;
94
95 #总体
96 2 my $chap_r = qr/^\s*($regex_a|$regex_b|$regex_c|$regex_d|$regex_e)\s*$/m;
97
98 2 return $chap_r;
99 }
100
101
102
103 sub read_single_txt {
104
105 #读入单个txt文件
106 2 ▶ my ($self, $txt, %opt) = @_;
Subroutine read_single_txt: called CC=10 SCAR=23.3
Pod undocumented
107
108 2 my $charset = $self->detect_file_charset($txt);
109 1 ▶ open my $sh, "<:encoding($charset)", $txt;
Subroutine BEGIN: called CC=1 SCAR=0.0
110
111 2 my @data;
112 2 my ( $single_toc, $single_content ) = ( '', '' );
113
114 #第一章
115 2 ▶ while (<$sh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sh)) 2 0 2
116 2 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 0 2 2
117 2 ▶ $single_toc = /$opt{chapter_regex}/ ? $1 : $_;
Branch: 1 branch
BranchTrueFalseTotal
/$opt{'chapter_regex'}/m ? : 2 0 2
118 2 last;
119 } ## end while (<$sh>)
120
121 #后续章节
122 2 ▶ while (<$sh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sh)) 26 2 28
123 26 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 10 16 26
124 16 ▶ if ( my ($new_single_toc) = /$opt{chapter_regex}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($new_single_toc) = /$opt{'chapter_regex'}/m) { } 4 12 16
125 4 ▶ if ( $single_toc =~ /\S/ and $single_content =~ /\S/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($single_toc =~ /\S/ and $single_content =~ /\S/s) 4 0 4
Condition33%1/3
$single_toc =~ /\S/ and $single_content =~ /\S/s
!ll&&!rl&&r
004
MC/DC0%0/2
$single_toc =~ /\S/ and $single_content =~ /\S/s
$single_toc =~ /\S/ $single_content =~ /\S/s
Truth table33%1/3
A and B
A= $single_toc =~ /\S/B= $single_content =~ /\S/s
ABresult
0X0
100
111
126 4 push @data, { title => $single_toc, content => $single_content };
127 4 $single_toc = '';
128 } ## end if ( $single_toc =~ /\S/...)
129 4 $single_toc .= $new_single_toc . "\n";
130 4 $single_content = '';
131 }
132 else {
133 12 $single_content .= $_;
134 } ## end else [ if ( my ($new_single_toc...))]
135 } ## end while (<$sh>)
136
137 2 push @data, { title => $single_toc, content => $single_content };
138 2 $self->format_chapter_content($_) for @data;
139
140 2 return \@data;
141 } ## end sub read_single_txt
142
143 sub format_chapter_content {
144 6 ▶ my ($self, $r) = @_;
Subroutine format_chapter_content: called CC=2 SCAR=6.9
Pod undocumented
145 6 for ($r->{content}) {
146 6 s#<br\s*/?\s*>#\n#gi;
147 6 s#\s*(.*\S)\s*#<p>$1</p>\n#gm;
148 6 s#<p>\s*</p>##g;
149 } ## end for ($chap_c)
150
151 6 return $self;
152 }
153
154 sub detect_file_charset {
155 2 ▶ my ($self, $file) = @_;
Subroutine detect_file_charset: called CC=1 SCAR=0.0
Pod undocumented
156 2 open my $fh, '<', $file;
157 2 read $fh, my $text, 360;
158 2 return detect($text);
159 } ## end sub detect_file_charset
160
161 1 1;
« lib/Novel/Robot/Packer.pm ↑ Summary lib/Novel/Robot/Parser/jjwxc.pm »