Robot/Parser/txt.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 128 CC 7 SCAR 28.1
CC
7
Cov
0%
read_single_txt
42.0
parse_novel
6.0
get_default_chapter_regex
2.0
CRAP 16.6
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 use strict;
23 use warnings;
24 use base 'Novel::Robot::Parser';
25  
26 use File::Find::Rule;
27 use Encode::Detect::CJK qw/detect/;
28 use utf8;
29
30
31 sub parse_novel {
32 my ($self, $path, %opt) = @_;
33 $opt{chapter_regex} ||= get_default_chapter_regex();
34
35 my %data;
36 $data{writer} = $opt{writer} || 'unknown';
37 $data{book} = $opt{book} || 'unknown';
38
39 my $p_ref = ref($path) eq 'ARRAY' ? $path : [ $path ];
40 for my $p (@$p_ref){
41 my @txts = sort File::Find::Rule->file()->in($p);
42 for my $txt (@txts){
43 my $txt_data_ref = $self->read_single_txt($txt, %opt);
44 for my $t (@$txt_data_ref){
45 #$t->{url} = $txt_file;
46 push @{$data{item_list}}, $t;
47 }
48 }
49 }
50
51 $self->update_item_list($data{item_list});
52
53 #$data{url} = '';
54
55 return \%data;
56 }
57
58 sub get_default_chapter_regex {
59 #指定分割章节的正则表达式
60
61 #序号
62 my $r_num =
63 qr/[0123456789零○〇一二三四五六七八九十百千\d]+/;
64 my $r_split = qr/[上中下]/;
65 my $r_not_chap_head = qr/引子|楔子|尾声|内容简介|正文|番外|终章|序言|后记|文案/;
66
67 #第x章,卷x,第x章(大结局),尾声x
68 my $r_head = qr/(卷|第|$r_not_chap_head)/;
69 my $r_tail = qr/(章|卷|回|部|折)/;
70 my $r_post = qr/([.\s\-\(\/(]+.{0,35})?/;
71 my $regex_a = qr/(【?$r_head?\s*$r_num\s*$r_tail?$r_post】?)/;
72
73 my $regex_a2 = qr/($r_head$r_num$r_tail[^。]{0,35})/;
74
75 #(1),(1)xxx
76 #xxx(1),xxx(1)yyy
77 #(1-上|中|下)
78 my $regex_b_index = qr/[((]$r_num[))]/;
79 my $regex_b_tail = qr/$regex_b_index\s*\S+/;
80 my $regex_b_head = qr/\S+\s*$regex_b_index.{0,10}/;
81 my $regex_b_split = qr/[((]$r_num[--]$r_split[))]/;
82 my $regex_b = qr/$regex_b_head|$regex_b_tail|$regex_b_index|$regex_b_split/;
83
84 #1、xxx,一、xxx
85 my $regex_c = qr/$r_num[、.. ].{0,10}/;
86
87 #第x卷 xxx 第x章 xxx
88 #第x卷/第x章 xxx
89 my $regex_d = qr/($regex_a(\s+.{0,10})?){2}/;
90
91 #后记 xxx
92 my $regex_e = qr/(【?$r_not_chap_head\s*$r_post】?)/;
93
94 #总体
95 my $chap_r = qr/^\s*($regex_a|$regex_a2|$regex_b|$regex_c|$regex_d|$regex_e)\s*$/m;
96
97 return $chap_r;
98 }
99
100
101
102 sub read_single_txt {
103
104 #读入单个txt文件
105 my ($self, $txt, %opt) = @_;
106
107 my $charset = $self->detect_file_charset($txt);
108 open my $sh, "<:encoding($charset)", $txt;
109
110 my @data;
111 my ( $single_toc, $single_content ) = ( '', '' );
112
113 #第一章
114 while (<$sh>) {
115 next unless /\S/;
116 $single_toc = /$opt{chapter_regex}/ ? $1 : $_;
117 last;
118 } ## end while (<$sh>)
119
120 #后续章节
121 while (<$sh>) {
122 next unless /\S/;
123 if ( my ($new_single_toc) = /$opt{chapter_regex}/ ) {
124 if ( $single_toc =~ /\S/ and $single_content =~ /\S/s ) {
125 push @data, { title => $single_toc, content => $single_content };
126 $single_toc = '';
127 } ## end if ( $single_toc =~ /\S/...)
128 $single_toc .= $new_single_toc . "\n";
129 $single_content = '';
130 }
131 else {
132 $single_content .= $_;
133 } ## end else [ if ( my ($new_single_toc...))]
134 } ## end while (<$sh>)
135
136 push @data, { title => $single_toc, content => $single_content };
137 $self->format_chapter_content($_) for @data;
138
139 return \@data;
140 } ## end sub read_single_txt
141
142 sub format_chapter_content {
143 my ($self, $r) = @_;
144 for ($r->{content}) {
145 s#<br\s*/?\s*>#\n#gi;
146 s#\s*(.*\S)\s*#<p>$1</p>\n#gm;
147 s#<p>\s*</p>##g;
148 } ## end for ($chap_c)
149
150 return $self;
151 }
152
153 sub detect_file_charset {
154 my ($self, $file) = @_;
155 open my $fh, '<', $file;
156 read $fh, my $text, 360;
157 return detect($text);
158 } ## end sub detect_file_charset
159
160 1;