lib/FB3/Convert/FB2.pm untested

filter: Statementstmt 0.0% 0 / 269 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 447 CC 43 SCAR 64.6
CC
43
Cov
0%
Reaper
1056.0
FB3Creator
132.0
CreateEpigraph
6.0
CRAP 641.4
1 package FB3::Convert::FB2;
2  
3 use strict;
4 use base 'FB3::Convert';
5 use XML::LibXML;
6 use File::Basename;
7 use File::Temp qw(tempfile tempdir);
8 use Encode qw(encode_utf8 decode_utf8);
9 use MIME::Base64 qw(decode_base64);
10 use Clone qw(clone);
11 use List::Util qw(none);
12 use utf8;
13  
14 my $XPC = XML::LibXML::XPathContext->new;
15  
16 sub Reaper {
17 my $self = shift;
18 my $X = shift;
19  
20 my %Args = @_;
21 my $Source = $Args{'source'} || $X->Error("Source path not defined");
22 my $TmpDir;
23 if (!$X->{'src_type'} && -d $Source) {
24 $TmpDir = $Source;
25 opendir(DH, $Source);
26 for (readdir(DH)){
27 if (lc($_) =~ /\.fb2$/) {
28 $Source = $Source.'/'.$_;
29 last;
30 }
31 }
32 closedir(DH);
33 }
34  
35 $X->Error("File '.fb2' not defined") if (!$X->{'src_type'} && lc($Source) !~ /\.fb2$/);
36  
37 my $XC = XML::LibXML::XPathContext->new();
38
39 my $FB2Doc = XML::LibXML->load_xml( location => $Source );
40 $FB2Doc->setEncoding('utf-8');
41  
42 $XPC->registerNs('fb', 'http://www.gribuser.ru/xml/fictionbook/2.0');
43  
44 #fix FB2 with regular expression checks (what XLST 1.0 can't do itself)
45 my $IsValidUUIDRegEx = qr/^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$/;
46 my( $FB2IdNode ) = $XPC->findnodes(
47 '/fb:FictionBook/fb:description/fb:document-info/fb:id/text()', $FB2Doc );
48 if( $FB2IdNode && $FB2IdNode->data !~ $IsValidUUIDRegEx ) {
49 $FB2IdNode->setData( '00000000-0000-0000-0000-000000000000' );
50 }
51 for my $PersonNode ( $XPC->findnodes(
52 '/fb:FictionBook/fb:description/fb:title-info/fb:author
53 | /fb:FictionBook/fb:description/fb:title-info/fb:translator', $FB2Doc )) {
54  
55 my( $IdTextNode ) = $XPC->findnodes( 'fb:id/text()', $PersonNode );
56 if( $IdTextNode && $IdTextNode->data !~ $IsValidUUIDRegEx ) {
57 $IdTextNode->setData( '00000000-0000-0000-0000-000000000000' );
58 }
59 }
60 my( $PublishInfoNode ) =
61 $XPC->findnodes( '/fb:FictionBook/fb:description/fb:publish-info', $FB2Doc );
62 if( $PublishInfoNode ) {
63 my $IsValidISBNRegEx = qr/([0-9]+[\-\s]){3,6}[0-9]*[xX0-9]/;
64 for my $ISBNNode ( $XPC->findnodes( 'fb:isbn', $PublishInfoNode )) {
65 my $ISBN = $ISBNNode->textContent;
66 unless( $ISBN =~ /^$IsValidISBNRegEx$/ ) {
67 my $FixedISBN;
68 if( $ISBN =~ /^[0-9]{10,13}$/ ) {
69 # this is ISBN without separators. Need just to prettify it
70 if( length( $ISBN ) == 10 ) {
71 # ISBN 10, format it as 5-699-15000-5
72 $FixedISBN = substr($ISBN,0,1).'-'.substr($ISBN,1,3).'-'.substr($ISBN,4,5).
73 '-'.substr($ISBN,9);
74 } else {
75 # ISBN 13, format it as 978-5-17-084861-4
76 $FixedISBN = substr($ISBN,0,3).'-'.substr($ISBN,3,1).'-'.substr($ISBN,4,2).
77 '-'.substr($ISBN,6,6).'-'.substr($ISBN,12);
78 }
79 } elsif( $ISBN =~ /($IsValidISBNRegEx)/ ) {
80 # Whole ISBN is invalid, but it contains valid ISBN inside
81 $FixedISBN = $1;
82 }
83 if( $FixedISBN ) {
84 my( $ISBNTextNode ) = $XPC->findnodes( 'text()', $ISBNNode );
85 $ISBNTextNode->setData( $FixedISBN );
86 } else {
87 # this is completely corrupted ISBN, throw it away
88 $PublishInfoNode->removeChild( $ISBNNode );
89 #warn "Removed ISBN '$ISBN' as it's not matching pattern for valid ISBNs in FB3";
90 }
91 }
92 }
93 }
94  
95 # корректируем заголовок сноски: берём текст между фигурными/квадратными скобками
96 my $AutoNoteNumber = 0;
97 for my $note ( $XPC->findnodes('//fb:a[@type="note"]', $FB2Doc) ) {
98 if ( my $text = [ $note->findnodes('./text()') ]->[0] ) {
99 $text->setData($1) if ( $text->data =~ /\[(\w+)\]/ or $text->data =~ /\{(\w+)\}/ );
100 }
101 else {
102 # если в заголовке сноски нет ничего, формируем самостоятельно
103 $note->appendTextNode('#' . ++$AutoNoteNumber);
104 }
105 }
106  
107 # Особо сложные эпиграфы порубим на куски тут. TODO полностью перенести работу с эпиграфами сюда.
108 for my $Cite ( $XPC->findnodes('//fb:epigraph/fb:cite[1]', $FB2Doc )) {
109 my @SetInEpigraph;
110 for my $Sibling ( $XPC->findnodes('following-sibling::*', $Cite)) { # всё что после первого cite выделяем в отдельный эпиграф
111 push @SetInEpigraph, $Sibling;
112 if ($Sibling->nodeName eq 'cite') {
113 CreateEpigraph($FB2Doc, $Cite->parentNode, @SetInEpigraph);
114 @SetInEpigraph = ();
115 }
116 }
117 CreateEpigraph($FB2Doc, $Cite->parentNode, @SetInEpigraph); # сбросим хвост
118 }
119  
120 # Заменяем <poem> на <blockquote> в тех местах, где poem не должно быть
121 for my $Poem ( $XPC->findnodes('//fb:poem', $FB2Doc )) {
122 if (none {$Poem->parentNode()->nodeName() eq $_} ('section', 'cite', 'epigraph')) {
123 my $BlockNode = $FB2Doc->createElement('blockquote');
124 foreach my $ChildInside ($Poem->getChildnodes) {
125 if ($ChildInside->nodeName() eq 'stanza') {
126 foreach my $StanzaInside ($ChildInside->getChildnodes) {
127 $BlockNode->addChild($StanzaInside->cloneNode(1));
128 }
129 } else {
130 $BlockNode->addChild($ChildInside->cloneNode(1));
131 }
132 }
133 $Poem->replaceNode($BlockNode);
134 }
135 }
136  
137 #<body>some</body><body><title> все склеивает в невалидный вид
138 #если кол-во <body> < 2 или нет body/title, можно оставить как есть
139 if (scalar @{$XPC->findnodes('//fb:body[not(@name="notes")]', $FB2Doc)} > 1) {
140 #иначе в каждый <body> c <title> обернем <section>
141 for my $TitleNode ($XPC->findnodes('//fb:body[not(@name="notes")]/fb:title', $FB2Doc )) {
142 my $BodyNode = $TitleNode->parentNode();
143 my $SecNode = $FB2Doc->createElement('section');
144 $SecNode->setAttribute('id' => $FB2IdNode);
145 foreach my $ChildInside ($BodyNode->getChildnodes) {
146 $SecNode->addChild($ChildInside->cloneNode(1));
147 $BodyNode->removeChild($ChildInside);
148 }
149 $BodyNode->addChild($SecNode);
150 }
151 }
152  
153 #notes всегда внизу
154 for my $Notes ( $XPC->findnodes('//fb:body[@name="notes"]', $FB2Doc)) {
155 $Notes->parentNode()->addChild($Notes->cloneNode(1));
156 $Notes->parentNode()->removeChild($Notes);
157 }
158  
159 # приведём в порядок цитаты без текста, только с заголовками
160 for my $Cite ( $XPC->findnodes('//fb:section/fb:cite', $FB2Doc )) {
161  
162 # <empty-line/> сразу после <subtitle/> недопустимы
163 for my $EmptyLine ( $XPC->findnodes('./fb:subtitle/following-sibling::fb:empty-line', $Cite) ) {
164  
165 $EmptyLine->unbindNode();
166 }
167  
168 # текст цитаты найден, всё в порядке, идём дальше
169 next if scalar $XPC->findnodes('./fb:p|./fb:poem|./fb:text-author', $Cite);
170  
171 # если не нашли ни заголовков ни текста -- удаляем цитату и идём дальше
172 my @Titles = $XPC->findnodes('./fb:subtitle', $Cite);
173 unless ( scalar @Titles ) {
174  
175 my $NodeToRemove = $Cite;
176 while (my $Parent = $NodeToRemove->parentNode) {
177 $NodeToRemove->unbindNode();
178 if ($Parent->nonBlankChildNodes) {
179 # в родительской секции что-то ещё есть - заканчиваем на этом
180 last;
181 } else {
182 # родительская секция оказалась пустой - придётся удалить и её
183 $NodeToRemove = $Parent;
184 }
185 }
186 next;
187 }
188  
189 for my $Title ( @Titles ) {
190  
191 my $PNode = $FB2Doc->createElement('p');
192 my $Strong = $FB2Doc->createElement('strong');
193  
194 my $strongAdd = 0;
195 my $paragraphAdd = 0;
196  
197 for my $Node ( $XPC->findnodes('*', $Title) ) {
198  
199 if ( 'strong' eq $Node->nodeName() ) {
200  
201 $PNode->appendChild($Node);
202 $paragraphAdd = 1;
203  
204 } else {
205  
206 $Strong->appendChild($Node);
207 $strongAdd = 1;
208 }
209 }
210  
211 if ( my $Text = $Title->textContent ) {
212  
213 $Text =~ s/^\s+//s; $Text =~ s/\s+$//s;
214  
215 if ( $Text ) {
216  
217 $Strong->appendTextNode($Text);
218 $strongAdd = 1;
219 }
220 }
221  
222 $Title->unbindNode();
223  
224 $PNode->appendChild($Strong) if ( $strongAdd );
225 $Cite->appendChild($PNode) if ( $paragraphAdd or $strongAdd );
226 }
227 }
228  
229 # Работаем с картинками, которые нужно выделить в div или section
230 for my $ImgNode ( $XPC->findnodes('//fb:section[ancestor::fb:body[not(@name="notes")]]/fb:image', $FB2Doc )) {
231 # Ищем подпись. Все <p> до <empty-line/>, но не более 300 символов.
232 my @SubscrNodes;
233 my $ImgSubscrLength;
234 # удалим пустые строки до картинки. Этот кусок можно в будущем использовать для нахождения подписи до картинки
235 my @NodesForDelete;
236 my $PrevSibling = $ImgNode;
237 while ($PrevSibling = $PrevSibling->previousSibling()) {
238 if ($PrevSibling->nodeName eq 'empty-line') {
239 push @NodesForDelete, $PrevSibling;
240 } else {
241 last;
242 }
243 }
244 foreach (@NodesForDelete) {
245 $_->unbindNode();
246 }
247 # найдем подпись и удалим пустые строки после картинки
248 my $EmptyLineFound = 0;
249 for my $Sibling ( $XPC->findnodes('following-sibling::*', $ImgNode )) {
250 if ($ImgSubscrLength <= 300) {
251 my $SiblingName = $Sibling->nodeName;
252 if ($SiblingName eq 'p' && !$EmptyLineFound) {
253 $ImgSubscrLength += length($Sibling->textContent);
254 push @SubscrNodes, $Sibling;
255 } elsif ($SiblingName eq 'empty-line') {
256 $Sibling->unbindNode();
257 $EmptyLineFound = 1;
258 } else {
259 @SubscrNodes = () unless $EmptyLineFound;
260 last;
261 }
262 } else {
263 @SubscrNodes = ();
264 last;
265 }
266 }
267  
268 # Перенесем подпись и картинку куда положено
269 # Переносим картинку
270 my $ParentNode = $ImgNode->parentNode;
271 my $PNode = $FB2Doc->createElement('p');
272 my $NextSibling = $ImgNode->nextNonBlankSibling();
273 my $NewParentNode;
274 if ($XPC->exists('following-sibling::fb:section', $ImgNode)) {
275 $NewParentNode = $FB2Doc->createElement('section');
276 } else {
277 $NewParentNode = $FB2Doc->createElement('div');
278 $NewParentNode->setAttribute('float', 'center');
279 $NewParentNode->setAttribute('on-one-page', '1');
280 }
281 $ParentNode->insertBefore($NewParentNode, $ImgNode);
282 $NewParentNode->appendChild($PNode);
283 $PNode->appendChild($ImgNode);
284 #Переносим подпись
285 foreach (@SubscrNodes) {
286 $NewParentNode->appendChild($_);
287 }
288 }
289  
290 $TmpDir ||= tempdir(CLEANUP=>1);
291  
292 my $TmpFB2File = "$TmpDir/book.fb2";
293 open my $fh, '>:utf8', $TmpFB2File
294 or die "Could not open $TmpFB2File for writing";
295 print $fh decode_utf8($FB2Doc->toString);
296 close $fh;
297 $X->{'temp_fb2'} = $TmpFB2File;
298 }
299  
300 sub CreateEpigraph {
301 my $FB2Doc = shift;
302 my $Epigraph = shift;
303 my @SetInEpigraph = @_;
304 return unless scalar @SetInEpigraph;
305  
306 my $NewEpigraph = $FB2Doc->createElement('epigraph');
307 $Epigraph->parentNode->insertAfter($NewEpigraph, $Epigraph);
308 foreach (@SetInEpigraph) {
309 $Epigraph->removeChild($_);
310 $NewEpigraph->appendChild($_);
311 }
312  
313 return $NewEpigraph;
314 }
315  
316 sub FB3Creator {
317 my $self = shift;
318 my $X = shift;
319 $X->Msg("Create FB3\n","w");
320  
321 my $FB3Path = $X->{'DestinationDir'};
322 my $XSLT = XML::LibXSLT->new;
323 my $FB2Doc = XML::LibXML->load_xml( location => $X->{'temp_fb2'} );
324  
325 #prepare directory structure for FB3
326  
327 foreach ("/fb3", "/fb3/img", "/fb3/style", "/fb3/meta", "/fb3/_rels", "/_rels"){
328 mkdir "$FB3Path$_";
329 }
330 $X->Msg("Directory structure is created successfully");
331  
332 my @TransformConfig = (
333 {stylesheet => "/body.xsl", output => "/fb3/body.xml"},
334 {stylesheet => "/body_rels.xsl", output => "/fb3/_rels/body.xml.rels"},
335 {stylesheet => "/core.xsl", output => "/fb3/meta/core.xml"}
336 );
337 unless( -s $FB3Path."/fb3/description.xml" ) {
338 # no meta file was given, so need to generate meta from FB2
339 push @TransformConfig,
340 {stylesheet => "/description.xsl", output => "/fb3/description.xml"};
341 }
342  
343 #transform
344 for (@TransformConfig) {
345 my $Stylesheet = $XSLT->parse_stylesheet_file($X->{'xsl_path'}.$_->{stylesheet});
346 my $Results = $Stylesheet->transform_file( $X->{'temp_fb2'} );
347 $Stylesheet->output_file($Results, $FB3Path.$_->{output});
348 }
349 unlink $X->{'temp_fb2'}; # удаляем за ненадобностью
350 $X->Msg("XML transformation has been successful");
351  
352 #extract images
353 my ($CoverNode)=$XPC->findnodes('/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image[1]',$FB2Doc);
354 my $CoverID;
355 if ($CoverNode){
356 $CoverID=lc($CoverNode->getAttribute('l:href'));
357 $CoverID=~s/^#//;
358 }
359  
360 my $ImagesFound = 0;
361 for ($XPC->findnodes('/fb:FictionBook/fb:binary',$FB2Doc)) {
362 $ImagesFound = 1;
363  
364 my $id=$_->getAttribute('id');
365 $X->Msg("Converting image '$id'...\n");
366 my $ContentType=$_->getAttribute('content-type');
367  
368 if (defined($id) && $ContentType=~ /image\/(jpeg|png)/i) {
369 my $FN="$FB3Path/fb3/img/".lc($id);
370 open IMGFILE, ">$FN" or die "$FN: $!";
371 binmode IMGFILE;
372 print (IMGFILE decode_base64($_->string_value()));
373 close IMGFILE;
374  
375 } elsif (defined($id) && $ContentType=~ /image\/gif/i) {
376 $!=18;
377 die "GIF image found!";
378 } elsif (defined($id) && $ContentType){
379 $!=18;
380 die "Unknown type '$ContentType' binary found!";
381 }
382 }
383 $X->Msg("Cover image found: $CoverID") if $CoverID;
384 $X->Msg("Pictures extracted successfully") if $ImagesFound;
385  
386 #extract stylesheets
387 my $SheetCount = 0;
388 for my $SheetNode ( $XPC->findnodes( '/fb:FictionBook/fb:stylesheet', $FB2Doc )) {
389 $SheetCount++;
390  
391 $X->Msg("Converting #$SheetCount stylesheet...");
392  
393 my $ContentType = $SheetNode->getAttribute('type');
394 unless( $ContentType eq 'text/css' ) {
395 $!=18;
396 die "Can convert only text/css stylesheets, but received a $ContentType";
397 }
398  
399 my $FN="$FB3Path/fb3/style/style$SheetCount.css";
400 open FH, ">", $FN
401 or die "$FN: $!";
402 print FH $SheetNode->string_value;
403 close FH;
404 }
405 $X->Msg("Stylesheets extracted successfully.") if $SheetCount > 0;
406  
407 #compile required files
408 my $FN="$FB3Path/[Content_Types].xml";
409 open FH, ">$FN" or die "$FN: $!";
410 print FH <<EOF;
411 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
412 <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
413 <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
414 <Default Extension="png" ContentType="image/png"/>
415 <Default Extension="jpg" ContentType="image/jpeg"/>
416 <Default Extension="jpeg" ContentType="image/jpeg"/>
417 <Default Extension="gif" ContentType="image/gif"/>
418 <Default Extension="svg" ContentType="image/svg+xml"/>
419 <Default Extension="xml" ContentType="application/xml"/>
420 <Default Extension="css" ContentType="text/css"/>
421  
422 <Override PartName="/fb3/meta/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
423  
424 <Override PartName="/fb3/description.xml" ContentType="application/fb3-description+xml"/>
425 <Override PartName="/fb3/body.xml" ContentType="application/fb3-body+xml"/>
426 </Types>
427 EOF
428 close FH;
429  
430 my $FN="$FB3Path/_rels/.rels";
431 open FH, ">$FN" or die "$FN: $!";
432 print FH qq{<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
433 <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
434 }.( $CoverID ? qq{<Relationship Id="rId0" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail" Target="fb3/img/$CoverID"/>} : '' ).qq{
435 <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="fb3/meta/core.xml"/>
436 <Relationship Id="rId2" Type="http://www.fictionbook.org/FictionBook3/relationships/Book" Target="fb3/description.xml"/>
437 </Relationships>};
438 close FH;
439  
440 my $FN="$FB3Path/fb3/_rels/description.xml.rels";
441 open FH, ">$FN" or die "$FN: $!";
442 print FH <<EOF;
443 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
444 <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
445 <Relationship Id="rId0"
446 Target="body.xml"
447 Type="http://www.fictionbook.org/FictionBook3/relationships/body" />
448 </Relationships>
449 EOF
450 close FH;
451 $X->Msg("Reference files successfully created.");
452  
453 return $FB3Path;
454 }
455  
456 1;