| 1 | package FB3::Convert; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Term::ANSIColor; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use File::Temp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use XML::LibXSLT; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use XML::LibXML; | ||
| 10 | use File::Basename; | ||
| 11 | use Hash::Merge; | ||
| 12 | use Cwd qw(cwd abs_path getcwd realpath); | ||
| 13 | use UUID::Tiny ':std'; | ||
| 14 | use File::Copy qw(copy); | ||
| 15 | use File::Temp qw/ tempfile tempdir /; | ||
| 16 | use FB3::Validator; | ||
| 17 | use utf8; | ||
| 18 | use URI::Escape; | ||
| 19 | use Encode qw(encode_utf8 decode_utf8); | ||
| 20 | use XML::Entities; | ||
| 21 | use XML::Entities::Data; | ||
| 22 | use Time::HiRes qw(gettimeofday sleep); | ||
| 23 | use Lingua::Identify qw(langof); | ||
| 24 | use Image::Magick; | ||
| 25 | use File::ShareDir qw/dist_dir/; | ||
| 26 | binmode(STDOUT,':utf8'); | ||
| 27 | |||
| 28 | our $VERSION = 0.48; | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | FB3::Convert - scripts and API for converting FB3 from and to different formats | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | my $Dist = dist_dir('FB3-Convert'); | ||
| 37 | my $ProfilesDir = $Dist."/profiles"; | ||
| 38 | my $CMYKProfile = $ProfilesDir.'/CoatedFOGRA27.icc'; | ||
| 39 | my $sRGBProfile = $ProfilesDir.'/sRGB_Color_Space.icc'; | ||
| 40 | |||
| 41 | my %MODULES; | ||
| 42 | # каким плагином работать - определяется тупо по расширению файла (см. ключ в хэше) | ||
| 43 | %MODULES = ( | ||
| 44 | 'epub' => { | ||
| 45 | 'class' => 'FB3::Convert::Epub', | ||
| 46 | 'unpack' => 1, | ||
| 47 | }, | ||
| 48 | 'fb2' => { | ||
| 49 | 'class' => 'FB3::Convert::FB2', | ||
| 50 | 'unpack' => 0, | ||
| 51 | }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | my @BlockLevel = | ||
| 55 | ('address','article','aside','blockquote','canvas','dd','div','dl','dt','fieldset','figcaption','figure','footer','form', | ||
| 56 | 'h1','h2','h3','h4','h5','h6', | ||
| 57 | 'header','hr','li','main','nav','noscript','ol','output','p','pre','section','table','tfoot','ul','video', | ||
| 58 | #формально не блок-левел, но нам их тоже приводить к нормальному виду | ||
| 59 | 'th','tr','td','caption' | ||
| 60 | ); | ||
| 61 | |||
| 62 | my $AllEntities = XML::Entities::Data::all; | ||
| 63 | delete $AllEntities->{'lt'}; | ||
| 64 | delete $AllEntities->{'gt'}; | ||
| 65 | delete $AllEntities->{'quot'}; | ||
| 66 | delete $AllEntities->{'apos'}; | ||
| 67 | delete $AllEntities->{'amp'}; | ||
| 68 | |||
| 69 | my %escapes = ( | ||
| 70 | '&' => '&', | ||
| 71 | '<' => '<', | ||
| 72 | '>' => '>', | ||
| 73 | '"' => '"', | ||
| 74 | '\'' => ''', | ||
| 75 | ); | ||
| 76 | my $xmlesc_rgx = join('|', keys %escapes); | ||
| 77 | |||
| 78 | sub xmlescape { | ||
| 79 | |||
| 80 | my $Esc = shift; | ||
| 81 | return unless defined $Esc; | ||
| 82 | |||
| 83 | $Esc =~ s/($xmlesc_rgx)/$escapes{$1}/gso; | ||
| 84 | return $Esc; | ||
| 85 | } | ||
| 86 | |||
| 87 | #Элементы, которые парсим в контенте и сохраняем в структуру | ||
| 88 | our $ElsMainList = { | ||
| 89 | 'span'=>undef, | ||
| 90 | 'a'=>undef, | ||
| 91 | 'em'=>undef, | ||
| 92 | 'sub'=>undef, | ||
| 93 | 'sup'=>undef, | ||
| 94 | 'code'=>undef, | ||
| 95 | 'img'=>undef, | ||
| 96 | 'i'=>undef, | ||
| 97 | 'u'=>undef, | ||
| 98 | 'underline'=>undef, | ||
| 99 | 'b'=>undef, | ||
| 100 | 'strong'=>undef, | ||
| 101 | 'br'=>undef, | ||
| 102 | }; | ||
| 103 | |||
| 104 | our $ElsMainList2={}; | ||
| 105 | map {$ElsMainList2->{$_}=undef;} keys %$ElsMainList; | ||
| 106 | $ElsMainList2->{p}=undef; | ||
| 107 | |||
| 108 | my @AccessImgFormat = ('png','gif','jpg','jpeg','svg','bmp','tiff','tif', | ||
| 109 | 'cur','tga','vda','icb','vst'#<-targa | ||
| 110 | ); | ||
| 111 | my @ConvertImgFormat = ('bmp','tiff','tif', | ||
| 112 | 'cur','tga','vda','icb','vst' | ||
| 113 | ); | ||
| 114 | my %UseImgProfile = ('tiff'=>1,'tif'=>1); | ||
| 115 | |||
| 116 | my %AllowElementsMain = ( | ||
| 117 | 'table' => { | ||
| 118 | 'allow_attributes' => ['id'], | ||
| 119 | 'allow_elements_inside' => {'tr'=>undef, 'caption'=>undef, title=>undef}, | ||
| 120 | }, | ||
| 121 | 'tr' => { | ||
| 122 | 'allow_attributes' => ['id','align'], | ||
| 123 | 'allow_elements_inside' => {'th'=>undef,'td'=>undef}, | ||
| 124 | }, | ||
| 125 | 'th' => { | ||
| 126 | 'allow_attributes' => ['colspan','rowspan','align','valign'], | ||
| 127 | 'allow_elements_inside' => $ElsMainList2, | ||
| 128 | }, | ||
| 129 | 'td' => { | ||
| 130 | 'allow_attributes' => ['colspan','rowspan','align','valign'], | ||
| 131 | 'allow_elements_inside' => $ElsMainList2, | ||
| 132 | }, | ||
| 133 | 'caption' => { | ||
| 134 | 'allow_elements_inside' => $ElsMainList2, | ||
| 135 | }, | ||
| 136 | 'i' => { | ||
| 137 | 'allow_attributes' => [], | ||
| 138 | 'allow_elements_inside' => $ElsMainList, | ||
| 139 | }, | ||
| 140 | 'strong' => { | ||
| 141 | 'allow_attributes' => ['id'], | ||
| 142 | 'allow_elements_inside' => $ElsMainList, | ||
| 143 | }, | ||
| 144 | 'underline' => { | ||
| 145 | 'allow_attributes' => ['id'], | ||
| 146 | 'allow_elements_inside' => $ElsMainList, | ||
| 147 | }, | ||
| 148 | 'em' => { | ||
| 149 | 'allow_attributes' => ['id'], | ||
| 150 | 'allow_elements_inside' => $ElsMainList, | ||
| 151 | }, | ||
| 152 | 'u' => { | ||
| 153 | 'allow_attributes' => ['id'] | ||
| 154 | }, | ||
| 155 | 'sup' => { | ||
| 156 | 'allow_attributes' => ['id'] | ||
| 157 | }, | ||
| 158 | 'sub' => { | ||
| 159 | 'allow_attributes' => ['id'] | ||
| 160 | }, | ||
| 161 | 'p' => { | ||
| 162 | 'allow_attributes' => ['id'], | ||
| 163 | 'allow_elements_inside' => $ElsMainList, | ||
| 164 | 'exclude_if_inside' => ['ul','ol','table'], #с этими вариантами лучше схлопнуться родительскому 'p' | ||
| 165 | }, | ||
| 166 | 'span' => { | ||
| 167 | 'allow_attributes' => ['id'], | ||
| 168 | 'allow_elements_inside' => $ElsMainList, | ||
| 169 | }, | ||
| 170 | 'title' => { | ||
| 171 | 'processor' => \&Transform2Valid, | ||
| 172 | 'allow_attributes' => ['id'], | ||
| 173 | 'allow_elements_inside' => {'p'=>undef}, | ||
| 174 | }, | ||
| 175 | 'subtitle' => { | ||
| 176 | 'allow_attributes' => ['id'], | ||
| 177 | 'allow_elements_inside' => $ElsMainList, | ||
| 178 | }, | ||
| 179 | 'ul' => { | ||
| 180 | 'allow_attributes' => ['id'], | ||
| 181 | 'allow_elements_inside' => {'li'=>undef, 'title'=>undef, 'epigraph'=>undef}, | ||
| 182 | }, | ||
| 183 | 'ol' => { | ||
| 184 | 'allow_attributes' => ['id'], | ||
| 185 | 'allow_elements_inside' => {'li'=>undef, 'title'=>undef, 'epigraph'=>undef}, | ||
| 186 | }, | ||
| 187 | 'li' => { | ||
| 188 | 'allow_attributes' => [], | ||
| 189 | 'allow_elements_inside' => {'em'=>undef,a=>undef}, | ||
| 190 | }, | ||
| 191 | 'root_fb3_container' => { | ||
| 192 | 'allow_elements_inside' => { | ||
| 193 | 'br' => undef, | ||
| 194 | 'underline'=>undef, | ||
| 195 | 'b'=>undef, | ||
| 196 | 'strong'=>undef, | ||
| 197 | 'span'=>undef, | ||
| 198 | 'div'=>undef, | ||
| 199 | 'b'=>undef, | ||
| 200 | 'h1'=>undef, | ||
| 201 | 'h2'=>undef, | ||
| 202 | 'h3'=>undef, | ||
| 203 | 'h4'=>undef, | ||
| 204 | 'h5'=>undef, | ||
| 205 | 'h6'=>undef, | ||
| 206 | 'table'=>undef, | ||
| 207 | 'ul'=>undef, | ||
| 208 | 'ol'=>undef, #полноправные элементы корня | ||
| 209 | 'p'=>undef, # см. ^^^ | ||
| 210 | 'title'=>undef, #разрешен, так как нужен в заголовочном section, но в body его необходимо удалять | ||
| 211 | 'subtitle'=>undef, | ||
| 212 | 'img' => undef, #некоторые элементы попадаются в корне, их так же нужно разрешить. Transform2Valid() их обрамит в <p> | ||
| 213 | 'a' => undef # см. ^^^ | ||
| 214 | } | ||
| 215 | }, | ||
| 216 | |||
| 217 | ); | ||
| 218 | |||
| 219 | my $XSLT = XML::LibXSLT->new; | ||
| 220 | my $XC = XML::LibXML::XPathContext->new(); | ||
| 221 | my $Parser = XML::LibXML->new(); | ||
| 222 | |||
| 223 | use constant { | ||
| 224 | NS_FB3_DESCRIPTION => 'http://www.fictionbook.org/FictionBook3/description' | ||
| 225 | }; | ||
| 226 | |||
| 227 | sub new { | ||
| 228 | my $class = shift; | ||
| 229 | my $X = {}; | ||
| 230 | my %Args = @_; | ||
| 231 | |||
| 232 | if ($Args{'empty'}) { | ||
| 233 | bless $X, $class; | ||
| 234 | return $X; | ||
| 235 | } | ||
| 236 | |||
| 237 | my $SourcePath = $Args{'source'}; | ||
| 238 | my $SourceFileName = $SourcePath; | ||
| 239 | $SourceFileName =~ s/.*?([^\/]+)$/$1/g; | ||
| 240 | |||
| 241 | my $DestinationDir = $Args{'destination_dir'} || tempdir(); | ||
| 242 | my $DestinationFile = $Args{'destination_file'}; | ||
| 243 | |||
| 244 | if ($DestinationFile) { | ||
| 245 | if ($DestinationFile =~ /^[^\/]/) { | ||
| 246 | $DestinationFile = cwd()."/".$DestinationFile; | ||
| 247 | } | ||
| 248 | my $DstFileDir = dirname($DestinationFile); | ||
| 249 | Error($X,"Directory for destination file not found! : ".$DstFileDir) unless -d $DstFileDir; | ||
| 250 | $DestinationFile = Cwd::realpath($DestinationFile); | ||
| 251 | } | ||
| 252 | |||
| 253 | Error($X,"source file not defined") unless $SourcePath; | ||
| 254 | Error($X,"source file '".$SourcePath."' not exists") unless -f $SourcePath; | ||
| 255 | |||
| 256 | my $FileType; | ||
| 257 | if ($Args{'src_type'}) { | ||
| 258 | $FileType = $Args{'src_type'}; | ||
| 259 | } else { | ||
| 260 | $SourcePath =~ /\.([^\.]+)$/; | ||
| 261 | $FileType = $1; | ||
| 262 | } | ||
| 263 | |||
| 264 | Error($X, "File '".$SourcePath."' format '".$FileType."' not detected") unless $MODULES{$FileType}; | ||
| 265 | |||
| 266 | my $Sub = $FileType; | ||
| 267 | |||
| 268 | my $Module = $MODULES{$Sub}->{class}; | ||
| 269 | #подгружаем нужный модуль | ||
| 270 | eval { | ||
| 271 | (my $MFile = $Module) =~ s|::|/|g; | ||
| 272 | require $MFile . '.pm'; | ||
| 273 | 1; | ||
| 274 | } or do { | ||
| 275 | Error($X, "Can't load module ".$Module." ".$@); | ||
| 276 | }; | ||
| 277 | |||
| 278 | $X->{'ClassName'} = $Sub; | ||
| 279 | $X->{'Source'} = $SourcePath; | ||
| 280 | $X->{'SourceFileName'} = $SourceFileName; | ||
| 281 | $X->{'SourceDir'} = undef; | ||
| 282 | $X->{'DestinationDir'} = $DestinationDir; | ||
| 283 | $X->{'DestinationFile'} = $DestinationFile; | ||
| 284 | $X->{'Module'} = $Module; | ||
| 285 | $X->{'verbose'} = $Args{'verbose'} ? $Args{'verbose'} : 0; | ||
| 286 | $X->{'euristic'} = $Args{'euristic'} || undef; | ||
| 287 | $X->{'euristic_debug'} = $Args{'euristic_debug'} || undef; | ||
| 288 | $X->{'phantom_js_path'} = $Args{'phantom_js_path'} || undef; | ||
| 289 | $X->{'showname'} = $Args{'showname'} ? 1 : 0; | ||
| 290 | $X->{'allow_elements'} = \%AllowElementsMain; | ||
| 291 | $X->{'href_list'} = {}; #собираем ссылки в документе | ||
| 292 | $X->{'id_list'} = {}; #собираем ссылки в документе | ||
| 293 | $X->{'bench'} = $Args{'bench'} ? 1 : 0; #бенчмарк режим в stdout | ||
| 294 | $X->{'bench2file'} = $Args{'bench2file'} ? $Args{'bench2file'} : 0; #бенчмарк режим в файл | ||
| 295 | $X->{'bench_list'} = {}; #бенчмарк режим | ||
| 296 | $X->{'simple'} = $Args{'simple'} ? 1 : 0; #Преобразование без создания структуры | ||
| 297 | $X->{'xsl_path'} = $Args{'xsl_path'}; | ||
| 298 | $X->{'src_type'} = $Args{'src_type'} if exists $MODULES{$Args{'src_type'}}; | ||
| 299 | |||
| 300 | unless ($Args{'simple'}) { | ||
| 301 | #Наша внутренняя структура данных конвертора. шаг влево - расстрел | ||
| 302 | $X->{'STRUCTURE'} = { | ||
| 303 | |||
| 304 | 'DESCRIPTION' => { | ||
| 305 | 'TITLE-INFO' => { | ||
| 306 | 'COVER_DESC' => undef, | ||
| 307 | 'AUTHORS' => undef, #[ | ||
| 308 | #{ | ||
| 309 | # 'first-name' => "Иван", | ||
| 310 | # 'id' => 'bcf95bde-eedc-49ef-926c-588bd4cfd9cd', | ||
| 311 | # 'middle-name' => undef, | ||
| 312 | # 'last-name' => "Растеряйло" | ||
| 313 | #}, | ||
| 314 | #], | ||
| 315 | 'PUBLISHER' => undef, | ||
| 316 | 'ANNOTATION' => undef, | ||
| 317 | 'BOOK-TITLE' => undef, | ||
| 318 | 'GENRES' => undef, #[ | ||
| 319 | # 'humor_prose', | ||
| 320 | # 'prose_su_classics' | ||
| 321 | #], | ||
| 322 | }, | ||
| 323 | 'DOCUMENT-INFO' => { | ||
| 324 | 'DATE' => undef, #{ | ||
| 325 | #'attributes' => { | ||
| 326 | # 'value' => undef | ||
| 327 | #} | ||
| 328 | #}, | ||
| 329 | 'TIME' => { | ||
| 330 | 'attributes' => { | ||
| 331 | 'value' => "00:00:00", | ||
| 332 | } | ||
| 333 | }, | ||
| 334 | 'LANGUAGE' => undef, | ||
| 335 | 'ID' => undef | ||
| 336 | } | ||
| 337 | }, | ||
| 338 | 'IMG_LIST' => [ # сюда складывает все встреченные картинки. Их ведь нужно копировать в новую папку, и rels строить | ||
| 339 | # { | ||
| 340 | # 'src_path' => 'images/_1000516292.jpg', | ||
| 341 | # 'id' => 'img1', | ||
| 342 | # 'new_path' => 'img/794__1000516292.jpg' | ||
| 343 | # } | ||
| 344 | ], | ||
| 345 | 'CSS_LIST' => [ # так же со стилями | ||
| 346 | #{ | ||
| 347 | # 'id' => 'mainCSS', | ||
| 348 | # 'new_path' => undef, | ||
| 349 | # 'src_path' => 'css/main.css' | ||
| 350 | #}, | ||
| 351 | ], | ||
| 352 | 'PAGES' => { # контент для body | ||
| 353 | 'attributes' => { | ||
| 354 | 'CP_compact' => 1 # узлы без контейнеров | ||
| 355 | }, | ||
| 356 | 'value' => [ | ||
| 357 | {} # дерево любой вложенности. см sub Obj2DOM | ||
| 358 | ] | ||
| 359 | } | ||
| 360 | }; | ||
| 361 | } | ||
| 362 | |||
| 363 | bless $X, $class; | ||
| 364 | Init($X); | ||
| 365 | |||
| 366 | #мета из файла (->fb3/description.xml) | ||
| 367 | if ($Args{'metadata'}) { | ||
| 368 | $X->Error("Meta file ".$Args{'metadata'}." not exists\n") unless -f $Args{'metadata'}; | ||
| 369 | $X->{'metadata'} = $Args{'metadata'}; | ||
| 370 | $X->ParseMetaFile() unless $Args{'simple'}; | ||
| 371 | File::Copy::copy($X->{'metadata'}, $X->{'DestinationDir'}."/fb3/description.xml"); | ||
| 372 | } elsif ($Args{'meta'}) { | ||
| 373 | #или мета из параметров | ||
| 374 | $X->BuildOuterMeta(meta => $Args{'meta'}); | ||
| 375 | } | ||
| 376 | |||
| 377 | return $X; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub Init { | ||
| 381 | my $X = shift; | ||
| 382 | Msg($X,"Init\n"); | ||
| 383 | my $FB3Path = $X->{'DestinationDir'}; | ||
| 384 | |||
| 385 | if (-d $FB3Path) { | ||
| 386 | $X->Msg("Remove old destination dir: ".$FB3Path."\n","w"); | ||
| 387 | ForceRmDir($X, $FB3Path); | ||
| 388 | } | ||
| 389 | |||
| 390 | Msg($X,"Create FB3 directory: ".$FB3Path."\n"); | ||
| 391 | mkdir "$FB3Path" or $X->Error("$FB3Path : $!"); | ||
| 392 | |||
| 393 | for my $Dir ("/fb3", "/fb3/img", "/fb3/style", "/fb3/meta", "/fb3/_rels", "/_rels") { | ||
| 394 | mkdir "$FB3Path$Dir" or $X->Error("$FB3Path$Dir : $!"); | ||
| 395 | } | ||
| 396 | Msg($X,"FB3: Directory structure is created successfully.\n"); | ||
| 397 | |||
| 398 | } | ||
| 399 | |||
| 400 | sub Reap { | ||
| 401 | my $X = shift; | ||
| 402 | my $Processor = $MODULES{$X->{'ClassName'}}; | ||
| 403 | my $File = $X->{'Source'}; | ||
| 404 | |||
| 405 | $X->Msg("working with file ".$File."\n",'w',1) if $X->{'showname'} || $X->{'verbose'}; | ||
| 406 | |||
| 407 | $X->_bs('unpack','Распаковка файла'); | ||
| 408 | $File = $Processor->{class}->_Unpacker($X,$File) if $Processor->{'unpack'}; | ||
| 409 | $X->_be('unpack'); | ||
| 410 | |||
| 411 | $X->_bs('reap','Потрошение исходного файла, cборка данных'); | ||
| 412 | $Processor->{'class'}->Reaper($X, source => ($File || $X->{'Source'}), src_type=>$X->{'src_type'}); | ||
| 413 | $X->_be('reap'); | ||
| 414 | |||
| 415 | return $X->{'STRUCTURE'}; | ||
| 416 | } | ||
| 417 | |||
| 418 | # -- методы могут быть переопределены в дочернем классе -- | ||
| 419 | |||
| 420 | #Распаковывает файл как zip, на выходе отдает директорию | ||
| 421 | sub _Unpacker { | ||
| 422 | my $self = shift; | ||
| 423 | my $X = shift; | ||
| 424 | my $Source = shift; | ||
| 425 | |||
| 426 | my $TMPPath = tempdir(CLEANUP=>1); | ||
| 427 | |||
| 428 | $X->{'SourceDir'} = $TMPPath; | ||
| 429 | $X->{'unzipped'} = 1; | ||
| 430 | |||
| 431 | my $Zip = Archive::Zip->new(); | ||
| 432 | |||
| 433 | unless ($Zip->read($Source) == Archive::Zip::AZ_OK ) { | ||
| 434 | Error($X,"Error reading file '".$Source."' as zip"); | ||
| 435 | } | ||
| 436 | |||
| 437 | my @FilesInZip = $Zip->members(); | ||
| 438 | |||
| 439 | Msg($X,"Unzip source file to directory: ".$TMPPath."\n"); | ||
| 440 | foreach (@FilesInZip) { | ||
| 441 | my $Fname = $_->fileName; | ||
| 442 | next if $Fname =~ /\.(ttf|otf|ttc)$/i; #излишество на данный момент | ||
| 443 | Error($X,"Error reading file '".$Source."' as zip. So file '".substr($Fname,-100,100)."' have strange structure. [may be exploit?]") if $Fname =~ m#\.\./# || $Fname =~ m#^\W*/#; | ||
| 444 | my $ExtFile = $TMPPath.'/'.$Fname; | ||
| 445 | |||
| 446 | my $Code; | ||
| 447 | eval{$Code = $Zip->extractMember($_, $ExtFile);}; | ||
| 448 | if ($@) { | ||
| 449 | my $Ext; | ||
| 450 | $Ext = "[dublicate names of dir and file?] " if $@ =~ /mkdir\s(.+?):/; | ||
| 451 | Error($X,"can't unpuck ".$Fname." from ".$Source." archive: $Ext".EncodeUtf8($X,$@)); | ||
| 452 | } | ||
| 453 | $X->Error($X,"can't unpuck ".$Fname." from ".$Source." archive. Code #".$Code) unless $Code == AZ_OK; | ||
| 454 | } | ||
| 455 | |||
| 456 | return $TMPPath; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub FB3Create { | ||
| 460 | my $X = shift; | ||
| 461 | my $Processor = $MODULES{$X->{'ClassName'}}; | ||
| 462 | |||
| 463 | my $FB3Path = $Processor->{'class'}->FB3Creator($X); | ||
| 464 | |||
| 465 | return $FB3Path; | ||
| 466 | } | ||
| 467 | |||
| 468 | sub FB3_2_Zip { | ||
| 469 | my $X = shift; | ||
| 470 | my %Args = @_; | ||
| 471 | |||
| 472 | Msg($X,"Create Zip from FB3: ".$X->{'DestinationFile'}."\n"); | ||
| 473 | |||
| 474 | my $old_dir = cwd(); | ||
| 475 | chdir("$X->{'DestinationDir'}"); | ||
| 476 | system("/usr/bin/zip -rq9 '".$X->{'DestinationFile'}."' ./*"); | ||
| 477 | chdir $old_dir if $old_dir; | ||
| 478 | Msg($X,"Delete dir after zip: $X->{'DestinationDir'}\n"); | ||
| 479 | ForceRmDir($X,$X->{'DestinationDir'}); | ||
| 480 | Msg($X,"OK\n"); | ||
| 481 | } | ||
| 482 | |||
| 483 | sub InNode { | ||
| 484 | my $X = shift; | ||
| 485 | my $Node = shift; | ||
| 486 | join "",map {$_->toString} $Node->childNodes; | ||
| 487 | } | ||
| 488 | |||
| 489 | # function Obj2DOM() | ||
| 490 | # | ||
| 491 | # example: | ||
| 492 | # my $Doc = Obj2DOM($X, | ||
| 493 | # obj => { | ||
| 494 | # attributes => {attr1 => 1, attr2 => 2}, | ||
| 495 | # title0 => 'TITLE0', | ||
| 496 | # title1 => { | ||
| 497 | # attributes => {attr3 => 3, attr4 => 4}, | ||
| 498 | # value => 'TITLE1', | ||
| 499 | # }, | ||
| 500 | # title2 => { | ||
| 501 | # attributes => {attr5 => 5, attr6 => 6}, | ||
| 502 | # value => [7,8,{item9=>[10,12,{item12=>12}]}], | ||
| 503 | # }, | ||
| 504 | # title3 => [ | ||
| 505 | # {'item13' => 13}, | ||
| 506 | # {'item14' => 14}, | ||
| 507 | # 15,16, | ||
| 508 | # '_17','_18', | ||
| 509 | # ], | ||
| 510 | # 'title4' => { | ||
| 511 | # attributes => {CP_compact => 1}, | ||
| 512 | # value => [123,{span=>'spanvalue'}] | ||
| 513 | # } | ||
| 514 | # } | ||
| 515 | # ); | ||
| 516 | # print $Doc->toString( | ||
| 517 | # 1 #pretty | ||
| 518 | # ); | ||
| 519 | # | ||
| 520 | # (hashes sorted by keys) => | ||
| 521 | # <?xml version="1.0" encoding="utf-8"?> | ||
| 522 | # <root attr1="1" attr2="2"> | ||
| 523 | # <title0>TITLE0</title0> | ||
| 524 | # <title1 attr4="4" attr3="3">TITLE1</title1> | ||
| 525 | # <title2 attr6="6" attr5="5"> | ||
| 526 | # <item>7</item> | ||
| 527 | # <item>8</item> | ||
| 528 | # <item> | ||
| 529 | # <item>10</item> | ||
| 530 | # <item>12</item> | ||
| 531 | # <item12>12</item12> | ||
| 532 | # </item9> | ||
| 533 | # </title2> | ||
| 534 | # <title3> | ||
| 535 | # <item13>13</item13> | ||
| 536 | # <item14>14</item14> | ||
| 537 | # <item>15</item> | ||
| 538 | # <item>16</item> | ||
| 539 | # <item>_17</item> | ||
| 540 | # <item>_18</item> | ||
| 541 | # </title3> | ||
| 542 | # <title4> | ||
| 543 | # 123<span>spanvalue</span> | ||
| 544 | # </title4> | ||
| 545 | # </root> | ||
| 546 | |||
| 547 | my $Doc; | ||
| 548 | sub Obj2DOM { | ||
| 549 | my $X = shift; | ||
| 550 | my %Args = @_; | ||
| 551 | |||
| 552 | my $Obj = $Args{'obj'}; | ||
| 553 | my $Parent = $Args{'parent'} || undef; | ||
| 554 | my $Sub = $Args{'sub'} || 0; | ||
| 555 | my $Root = $Args{'root'} || {name=>'root'}; | ||
| 556 | |||
| 557 | #параметры, влияющие на поведение отрисовки DOM | ||
| 558 | my $ArrayNodeNameLikeParent = $Args{'like_parent'} || 0; # title1 =>[{},{}] ==> <title1><title1/><title1/></title1> | ||
| 559 | my $Compact = $Args{'compact'} || 0; # like_parent не актуально. элементы не обрамляются в item-контейнеры | ||
| 560 | |||
| 561 | undef $Doc unless $Sub; #первый заход парсинга. $Doc fresh and virginity | ||
| 562 | |||
| 563 | unless ($Doc) { | ||
| 564 | $Doc = XML::LibXML::Document->new('1.0', 'utf-8'); | ||
| 565 | } | ||
| 566 | |||
| 567 | my $First=0; | ||
| 568 | if (!$Parent) { | ||
| 569 | $First=1; | ||
| 570 | $Parent = $Doc->createElement($Root->{'name'}); | ||
| 571 | if ($Root->{'attributes'}) { | ||
| 572 | foreach my $RAttr ( keys %{$Root->{'attributes'}}) { | ||
| 573 | $Parent->addChild($Doc->createAttribute( $RAttr => $Root->{'attributes'}->{$RAttr} )); | ||
| 574 | } | ||
| 575 | } | ||
| 576 | } | ||
| 577 | |||
| 578 | if (ref $Obj eq 'HASH') { | ||
| 579 | |||
| 580 | foreach my $Key (sort keys %$Obj) { | ||
| 581 | my $Value = $Obj->{$Key}; | ||
| 582 | |||
| 583 | if ($Key eq 'attributes') { | ||
| 584 | foreach (sort keys %{$Obj->{'attributes'}}) { | ||
| 585 | $Compact = 1 if $_ eq 'CP_compact' && $Obj->{'attributes'}->{$_}; #все следующие вложенные ноды - в формате compact | ||
| 586 | $Parent->addChild($Doc->createAttribute( $_ => $Obj->{'attributes'}->{$_} )) unless $First; | ||
| 587 | } | ||
| 588 | } elsif ($Key eq 'value') { | ||
| 589 | Obj2DOM($X, sub=>1, obj => $Obj->{$Key}, parent => $Parent, like_parent => $ArrayNodeNameLikeParent, compact => $Compact); | ||
| 590 | } else { | ||
| 591 | my $Child = $Doc->createElement($Key); | ||
| 592 | Obj2DOM($X, sub=>1, obj => $Obj->{$Key}, parent => $Child, like_parent => $ArrayNodeNameLikeParent, compact => $Compact); | ||
| 593 | $Parent->addChild($Child); | ||
| 594 | } | ||
| 595 | |||
| 596 | } | ||
| 597 | |||
| 598 | } elsif (ref $Obj eq 'ARRAY') { | ||
| 599 | foreach my $Item (@$Obj) { | ||
| 600 | if (ref $Item eq '' | ||
| 601 | || (!$Compact && ref $Item eq 'HASH') | ||
| 602 | ) { | ||
| 603 | my $Child; | ||
| 604 | $Child = $Doc->createElement($ArrayNodeNameLikeParent?$Parent->nodeName:'item') unless $Compact; | ||
| 605 | Obj2DOM($X, sub=>1, obj => $Item, parent => ($Compact ? $Parent : $Child), like_parent => $ArrayNodeNameLikeParent, compact => $Compact); | ||
| 606 | $Parent->addChild($Child) unless $Compact; | ||
| 607 | } else { | ||
| 608 | Obj2DOM($X, sub=>1, obj => $Item, parent => $Parent, like_parent => $ArrayNodeNameLikeParent, compact => $Compact); | ||
| 609 | } | ||
| 610 | } | ||
| 611 | } elsif (ref $Obj eq '') { | ||
| 612 | $Parent->appendTextNode($Obj); | ||
| 613 | } | ||
| 614 | |||
| 615 | $Doc->setDocumentElement($Parent) unless $Sub; | ||
| 616 | return $Doc; | ||
| 617 | } | ||
| 618 | |||
| 619 | sub Content2Tree { | ||
| 620 | my $X = shift; | ||
| 621 | my $Obj = shift; | ||
| 622 | my $Content = $Obj->{'content'}; | ||
| 623 | my $File = Encode::decode_utf8(uri_unescape($Obj->{'file'})); | ||
| 624 | |||
| 625 | $Content = $X->trim($Content); | ||
| 626 | |||
| 627 | #ровняем пробелы у block-level | ||
| 628 | my $BlockRegExp = '('.(join "|", @BlockLevel).')'; | ||
| 629 | $Content =~ s#\s*(</?$BlockRegExp[^>]*>)\s*#$1#gi; | ||
| 630 | |||
| 631 | my $XMLDoc = XML::LibXML->new( | ||
| 632 | expand_entities => 0, # не считать & за entity | ||
| 633 | no_network => 1, # не будем тянуть внешние вложения | ||
| 634 | recover => ($X->{'verbose'} && $X->{'verbose'} > 1 ? 1 : 2), # не падать при кривой структуре. например, не закрыт тег. entity и пр | => 1 - вопить, 2 - совсем молчать | ||
| 635 | load_ext_dtd => 0, # полный молчок про dtd | ||
| 636 | ); | ||
| 637 | |||
| 638 | my $NodeDoc = $XMLDoc->parse_string('<root_fb3_container>'.$Content.'</root_fb3_container>') || $X->Error("Can't parse! ".$!); | ||
| 639 | |||
| 640 | my $RootEl = $NodeDoc->getDocumentElement; | ||
| 641 | my $Result = &ProcNode($X,$RootEl,$File,'root_fb3_container'); | ||
| 642 | |||
| 643 | $X->_bs('TRPost','Постпроцессинг трансфомированных'); | ||
| 644 | $Result = $X->TransformedPostprocess($Result); | ||
| 645 | $X->_be('TRPost'); | ||
| 646 | |||
| 647 | |||
| 648 | return { | ||
| 649 | content => $X->NormalizeTree($Result), | ||
| 650 | ID => $X->Path2ID(uri_unescape($File),undef,'main_section'), #main-sectioon | ||
| 651 | ID_SUB => $X->UUID(), #sub-section | ||
| 652 | }; | ||
| 653 | } | ||
| 654 | |||
| 655 | #после трансформации нод (Epub::TransformTo и т.д), общая картина может стать невалидной | ||
| 656 | #еще раз обработаем дерево с учетом текущих реалий | ||
| 657 | sub TransformedPostprocess { | ||
| 658 | my $X = shift; | ||
| 659 | my $Data = shift; | ||
| 660 | my %AllowElements = %{$X->{'allow_elements'}}; | ||
| 661 | |||
| 662 | return unless ref $Data eq 'ARRAY'; | ||
| 663 | |||
| 664 | foreach my $Item (@$Data) { | ||
| 665 | if (ref $Item eq 'HASH') { | ||
| 666 | foreach my $NodeName (sort keys %$Item) { | ||
| 667 | my $NodeValue = $Item->{$NodeName}; | ||
| 668 | if (ref $NodeValue eq 'HASH' && exists $NodeValue->{'was'} && $NodeValue->{'was'} ne $NodeName) { #была трансформация. кандидат на рассмотрение | ||
| 669 | my $Exclude = 0; | ||
| 670 | if ($AllowElements{$NodeValue->{'was'}}->{'exclude_if_inside'}) { #проверка на вшивость | ||
| 671 | $Exclude = 1 if $X->StructHaveInside($NodeValue->{'value'}, $AllowElements{$NodeValue->{'was'}}->{'exclude_if_inside'}); | ||
| 672 | } | ||
| 673 | if ($Exclude) { | ||
| 674 | $Item = $NodeValue->{'value'}; | ||
| 675 | } | ||
| 676 | } | ||
| 677 | if (ref $NodeValue eq 'HASH') { | ||
| 678 | delete $NodeValue->{'was'} if exists $NodeValue->{'was'}; | ||
| 679 | TransformedPostprocess($X,$NodeValue->{'value'}) if exists $NodeValue->{'value'}; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | } elsif (ref $Item eq 'ARRAY') { | ||
| 683 | TransformedPostprocess($X,$Item); | ||
| 684 | } | ||
| 685 | } | ||
| 686 | |||
| 687 | return $Data; | ||
| 688 | } | ||
| 689 | |||
| 690 | sub StructHaveInside { | ||
| 691 | my $X = shift; | ||
| 692 | my $Node = shift; | ||
| 693 | my $ExcludeList = shift; | ||
| 694 | return unless ref $Node eq 'ARRAY'; | ||
| 695 | |||
| 696 | my $Finded = 0; | ||
| 697 | foreach my $Item (@$Node) { | ||
| 698 | last if $Finded; | ||
| 699 | if (ref $Item eq 'ARRAY') { | ||
| 700 | return 1 if $X->StructHaveInside($Item,$ExcludeList); | ||
| 701 | } elsif (ref $Item eq 'HASH') { #рассматриваем только первый уровень, глубже - не наше поле ответственности | ||
| 702 | foreach my $NodeNameInside (sort keys %$Item) { | ||
| 703 | return 1 if grep {$NodeNameInside eq $_} @$ExcludeList; | ||
| 704 | } | ||
| 705 | } | ||
| 706 | } | ||
| 707 | return $Finded; | ||
| 708 | } | ||
| 709 | |||
| 710 | sub NormalizeTree { | ||
| 711 | my $X = shift; | ||
| 712 | my $Data = shift; | ||
| 713 | |||
| 714 | return unless ref $Data eq 'ARRAY'; | ||
| 715 | |||
| 716 | my @Ar; | ||
| 717 | foreach my $Item (@$Data) { | ||
| 718 | |||
| 719 | if (ref $Item eq 'HASH') { | ||
| 720 | foreach (keys %$Item) { | ||
| 721 | $Item->{$_}->{'value'} = NormalizeTree($X,$Item->{$_}->{'value'}) | ||
| 722 | if ref $Item->{$_} eq 'HASH' && exists $Item->{$_}->{'value'}; | ||
| 723 | } | ||
| 724 | } | ||
| 725 | |||
| 726 | if (ref $Item eq 'ARRAY') { | ||
| 727 | $Item = NormalizeTree($X,$Item); | ||
| 728 | push @Ar, @$Item; | ||
| 729 | next; | ||
| 730 | } | ||
| 731 | |||
| 732 | if (!ref $Item) { | ||
| 733 | my $Str; | ||
| 734 | unless ($Item =~ /^\s+$/) { | ||
| 735 | $Str = $X->trim($Item); | ||
| 736 | } else { | ||
| 737 | $Str = $Item; | ||
| 738 | } | ||
| 739 | $Str =~ s/[\n\r]//g; | ||
| 740 | next if $Str eq ''; | ||
| 741 | } | ||
| 742 | |||
| 743 | push @Ar, $Item; | ||
| 744 | } | ||
| 745 | |||
| 746 | @$Data = @Ar; | ||
| 747 | return \@Ar; | ||
| 748 | } | ||
| 749 | |||
| 750 | sub ProcNode { | ||
| 751 | my $X = shift; | ||
| 752 | my $Node = shift; | ||
| 753 | my $RelPath = shift; | ||
| 754 | my $LastGoodParent = lc(shift); | ||
| 755 | my @Dist; | ||
| 756 | |||
| 757 | my %AllowElements = %{$X->{'allow_elements'}}; | ||
| 758 | |||
| 759 | foreach my $Child ( $Node->getChildnodes ) { | ||
| 760 | |||
| 761 | my $ChildNodeName = lc($Child->nodeName); | ||
| 762 | |||
| 763 | my $Allow = 1; | ||
| 764 | |||
| 765 | if ($AllowElements{$ChildNodeName}->{'exclude_if_inside'}) { #проверка на вшивость 1 | ||
| 766 | $Allow = 0 if $X->NodeHaveInside($Child, $AllowElements{$ChildNodeName}->{'exclude_if_inside'}); | ||
| 767 | } | ||
| 768 | |||
| 769 | if ($Allow) { #проверка на вшивость 2 | ||
| 770 | $Allow = | ||
| 771 | ($Child->nodeName !~ /#text/i | ||
| 772 | && exists $AllowElements{$LastGoodParent} | ||
| 773 | && ( | ||
| 774 | # !$AllowElements{$LastGoodParent}->{'allow_elements_inside'} || | ||
| 775 | exists $AllowElements{$LastGoodParent}->{'allow_elements_inside'}->{$ChildNodeName}) ) | ||
| 776 | ? 1 : 0; | ||
| 777 | } | ||
| 778 | |||
| 779 | #если ноду прибиваем, нужно чтобы дочерние работали по правилам пэрент-ноды (она ведь теперь и есть пэрент для последующих вложенных) | ||
| 780 | my $GoodParent = $Allow ? $ChildNodeName : $LastGoodParent; #если нода прошла разрешение, то теперь она становится последней parent в ветке и далее равняемся на ее правила | ||
| 781 | |||
| 782 | my $OldNodeName = $Child->nodeName; | ||
| 783 | |||
| 784 | #разрешенные атрибуты текущей ноды | ||
| 785 | my $AllowAttributes = | ||
| 786 | $Allow | ||
| 787 | && exists $AllowElements{$ChildNodeName}->{'allow_attributes'} | ||
| 788 | && $AllowElements{$ChildNodeName}->{'allow_attributes'} | ||
| 789 | ? $X->NodeGetAllowAttributes($Child) | ||
| 790 | : 0; | ||
| 791 | |||
| 792 | if ($Allow && $AllowElements{$ChildNodeName}->{'processor'}) { | ||
| 793 | $Child = $AllowElements{$ChildNodeName}->{'processor'}->($X,'node'=>$Child, 'relpath'=>$RelPath, params=>$AllowElements{$ChildNodeName}->{'processor_params'}); | ||
| 794 | } | ||
| 795 | |||
| 796 | my $NodeName = $Child->nodeName; #имя могло измениться процессором | ||
| 797 | |||
| 798 | push @Dist, $NodeName =~ /#text/i | ||
| 799 | ? unquot($X,$Child->toString) #текстовая нода | ||
| 800 | : | ||
| 801 | $Allow | ||
| 802 | ? { #тэг в ноде выводим | ||
| 803 | $NodeName => $AllowAttributes | ||
| 804 | ? {attributes => ConvertIds($X,$X->NodeGetAllowAttributes($Child),$RelPath), 'value' => &ProcNode($X,$Child,$RelPath,$GoodParent), 'was'=>$OldNodeName} # надо с атрибутами выводить | ||
| 805 | : &ProcNode($X,$Child,$RelPath,$GoodParent), # дочку строим по упрощенной схеме ('value' не обязателен) | ||
| 806 | } | ||
| 807 | : &ProcNode($X,$Child,$RelPath,$GoodParent) # не выводим тэг, шагаем дальше строить дерево | ||
| 808 | ; | ||
| 809 | } | ||
| 810 | |||
| 811 | return \@Dist; | ||
| 812 | } | ||
| 813 | |||
| 814 | sub NodeHaveInside { | ||
| 815 | my $X = shift; | ||
| 816 | my $Node = shift; | ||
| 817 | my $Elements = shift; | ||
| 818 | |||
| 819 | my %H = map {lc($_)=>1} @$Elements; | ||
| 820 | |||
| 821 | foreach my $Child ( $Node->getChildnodes ) { | ||
| 822 | if (exists $H{lc($Child->nodeName())}) { | ||
| 823 | undef %H; | ||
| 824 | return 1; | ||
| 825 | } | ||
| 826 | } | ||
| 827 | |||
| 828 | undef %H; | ||
| 829 | return 0; | ||
| 830 | } | ||
| 831 | |||
| 832 | sub ConvertIds { | ||
| 833 | my $X = shift; | ||
| 834 | my $Attributes = shift; | ||
| 835 | my $RelPath = shift; | ||
| 836 | |||
| 837 | #если есть рутовое имя, соорудим новый id-атрибут | ||
| 838 | if ($RelPath && exists $Attributes->{'id'}) { | ||
| 839 | my $Href = $RelPath."#".$Attributes->{'id'}; | ||
| 840 | #так же должны быть преобразованы <a href !!! | ||
| 841 | my $Id = $X->Path2ID(uri_unescape($Href),undef,'convert_id'); | ||
| 842 | $Attributes->{'id'} = 'link_'.$Id; | ||
| 843 | $X->{'id_list'}->{$Attributes->{'id'}} = $Href; | ||
| 844 | } | ||
| 845 | |||
| 846 | return $Attributes; | ||
| 847 | } | ||
| 848 | |||
| 849 | sub Path2ID { | ||
| 850 | my $X = shift; | ||
| 851 | my $DestPath = shift || return undef; #Искомый путь | ||
| 852 | my $LocalPath = shift || undef; #путь, относительно которого вычисляем Искомый | ||
| 853 | my $Debug = shift; | ||
| 854 | my $Skip = shift; | ||
| 855 | |||
| 856 | unless ($LocalPath) { | ||
| 857 | $LocalPath = $X->{'ContentDir'}; #Если не указан, работаем от корневой папки с контентом | ||
| 858 | } else { | ||
| 859 | $LocalPath = dirname($X->RealPath($X->{'ContentDir'}.'/'.$LocalPath)); | ||
| 860 | } | ||
| 861 | |||
| 862 | my $Link = $X->RealPath($LocalPath.'/'.$DestPath, $Debug, $Skip); | ||
| 863 | |||
| 864 | my $Path = defined $Link && $Link ne '' ? $X->UUID($Link) : undef; | ||
| 865 | |||
| 866 | return $Path; | ||
| 867 | } | ||
| 868 | |||
| 869 | sub RealPath { | ||
| 870 | my $X = shift; | ||
| 871 | my $Path = shift; | ||
| 872 | my $Debug = shift; | ||
| 873 | my $Skip = shift; | ||
| 874 | |||
| 875 | my $RealPath = undef; | ||
| 876 | |||
| 877 | if ($RealPath = Cwd::realpath($Path)) { | ||
| 878 | $RealPath =~ s/%20/ /g; | ||
| 879 | my $RealPath2 = $RealPath; | ||
| 880 | $RealPath2 =~ s/#.*$//g; | ||
| 881 | $RealPath = undef if !-f $RealPath2 && !-d $RealPath2; | ||
| 882 | } | ||
| 883 | |||
| 884 | $X->Error("Wrong path!\n$! $Path".($Debug?' ('.$Debug.')':'')) if !$RealPath && !$Skip; | ||
| 885 | return $RealPath; | ||
| 886 | } | ||
| 887 | |||
| 888 | sub NodeGetAllowAttributes { | ||
| 889 | my $X = shift; | ||
| 890 | my $Node = shift; | ||
| 891 | |||
| 892 | my %AllowElements = %{$X->{'allow_elements'}}; | ||
| 893 | |||
| 894 | my %Attrs; | ||
| 895 | my @Attrs = $Node->findnodes( "./@*"); | ||
| 896 | foreach my $Attr (@Attrs) { | ||
| 897 | $Attrs{$Attr->nodeName} = $Attr->value | ||
| 898 | if | ||
| 899 | (ref $AllowElements{$Node->nodeName}->{'allow_attributes'} eq 'ARRAY' | ||
| 900 | && grep {$Attr->nodeName eq $_} @{$AllowElements{$Node->nodeName}->{'allow_attributes'}} | ||
| 901 | ); | ||
| 902 | } | ||
| 903 | |||
| 904 | return \%Attrs; | ||
| 905 | } | ||
| 906 | |||
| 907 | sub UUID { | ||
| 908 | my $X = shift; | ||
| 909 | my $Str = shift || ''; | ||
| 910 | return $Str ? lc(uuid_to_string(create_uuid(UUID_V5,$Str))) : lc(create_uuid_as_string(UUID_V4)); | ||
| 911 | } | ||
| 912 | |||
| 913 | sub trim { | ||
| 914 | my $X = shift; | ||
| 915 | my $str = shift; | ||
| 916 | $str =~ s/\t/ /g; | ||
| 917 | $str =~ s/\s+/ /g; | ||
| 918 | $str =~ s/^\s*//; | ||
| 919 | $str =~ s/\s*$//; | ||
| 920 | $str =~ s/^\t*//; | ||
| 921 | $str =~ s/\t*$//; | ||
| 922 | return $str; | ||
| 923 | } | ||
| 924 | |||
| 925 | sub trim_soft { | ||
| 926 | my $X = shift; | ||
| 927 | my $str = shift; | ||
| 928 | $str =~ s/^\s*//; | ||
| 929 | $str =~ s/\s*$//; | ||
| 930 | $str =~ s/^\t*//; | ||
| 931 | $str =~ s/\t*$//; | ||
| 932 | return $str; | ||
| 933 | } | ||
| 934 | |||
| 935 | sub unquot { | ||
| 936 | my $X = shift; | ||
| 937 | my $str = shift; | ||
| 938 | |||
| 939 | $str =~ s/&/&/g; | ||
| 940 | $str =~ s/</</g; | ||
| 941 | $str =~ s/>/>/g; | ||
| 942 | $str =~ s/&guot;/"/g; | ||
| 943 | $str =~ s/'/'/g; | ||
| 944 | |||
| 945 | return $str; | ||
| 946 | } | ||
| 947 | |||
| 948 | sub quot { | ||
| 949 | my $X = shift; | ||
| 950 | my $str = shift; | ||
| 951 | |||
| 952 | $str =~ s/&/&/g; | ||
| 953 | $str =~ s/&/&/g; | ||
| 954 | $str =~ s/ / /g; | ||
| 955 | $str =~ s/</</g; | ||
| 956 | $str =~ s/>/>/g; | ||
| 957 | $str =~ s/"/"/g; | ||
| 958 | $str =~ s/"/'/g; | ||
| 959 | |||
| 960 | return $str; | ||
| 961 | } | ||
| 962 | |||
| 963 | sub qent { | ||
| 964 | my $X = shift; | ||
| 965 | my $Str = shift; | ||
| 966 | |||
| 967 | $Str =~ s/&#(0+)?60;/</g; | ||
| 968 | $Str =~ s/&#(0+)?62;/>/g; | ||
| 969 | $Str =~ s/&#x(0+)?3e;/>/gi; | ||
| 970 | $Str =~ s/&#x(0+)?3c;/</gi; | ||
| 971 | |||
| 972 | $Str =~ s/&#(0+)?38;/&/g; | ||
| 973 | $Str =~ s/&#x(0+)?26;/&/g; | ||
| 974 | |||
| 975 | $Str =~ s/&#(0+)?34;/"/g; | ||
| 976 | $Str =~ s/&#x(0+)?22;/"/g; | ||
| 977 | |||
| 978 | $Str =~ s/&#(0+)?39;/'/g; | ||
| 979 | $Str =~ s/&#x(0+)?27;/'/g; | ||
| 980 | |||
| 981 | XML::Entities::_decode_entities($Str, $AllEntities, 0); | ||
| 982 | $Str =~ s/&(?!amp;|quot;|apos;|lt;|gt;)/&/gi; | ||
| 983 | return $Str; | ||
| 984 | } | ||
| 985 | |||
| 986 | sub html_trim { | ||
| 987 | my $X = shift; | ||
| 988 | my $str = shift; | ||
| 989 | |||
| 990 | $str = quot($X,trim($X,$str)); | ||
| 991 | |||
| 992 | return $str; | ||
| 993 | } | ||
| 994 | |||
| 995 | sub EraseTags { | ||
| 996 | my $X = shift; | ||
| 997 | my $str = shift; | ||
| 998 | $str =~ s/<.*?>//g; | ||
| 999 | return $str; | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | # <= string, [type (i|w|e) ] | ||
| 1003 | sub Msg { | ||
| 1004 | my $X = shift; | ||
| 1005 | my $Str = shift; | ||
| 1006 | my $Type = shift || 'i'; | ||
| 1007 | my $Force = shift; | ||
| 1008 | |||
| 1009 | return if !$X->{'verbose'} && $Type ne 'e' && !$Force; | ||
| 1010 | |||
| 1011 | #my $Color; | ||
| 1012 | #if ($Type eq 'w') { | ||
| 1013 | # $Color = "bold green"; | ||
| 1014 | #} elsif ($Type eq 'e') { | ||
| 1015 | # $Color = "bold red"; | ||
| 1016 | #} | ||
| 1017 | |||
| 1018 | #print color($Color) if $Color; | ||
| 1019 | print $Str; | ||
| 1020 | #print color('reset') if $Color; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | sub Error { | ||
| 1024 | my $X = shift; | ||
| 1025 | my $ErrStr = shift; | ||
| 1026 | Msg($X,$ErrStr."\n",'e'); | ||
| 1027 | Cleanup($X,1); | ||
| 1028 | exit; | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | #проверка валидности полученного FB3 | ||
| 1032 | sub Validate { | ||
| 1033 | my $X = shift; | ||
| 1034 | my %Args = @_; | ||
| 1035 | my $ValidateDir = $Args{'path'}; | ||
| 1036 | my $XsdPath = $Args{'xsd'}; | ||
| 1037 | |||
| 1038 | $X->Msg("Validate result\n"); | ||
| 1039 | my $Valid = FB3::Validator->new( $XsdPath ); | ||
| 1040 | return $Valid->Validate($ValidateDir||$X->{'DestinationDir'}); | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | sub Cleanup { | ||
| 1044 | my $X = shift; | ||
| 1045 | my $CleanDest = shift; | ||
| 1046 | |||
| 1047 | if ($X->{'unzipped'} && $X->{'SourceDir'}) { #если наследили распаковкой в tmp | ||
| 1048 | ForceRmDir($X,$X->{'SourceDir'}); | ||
| 1049 | Msg($X,"Clean tmp directory ".$X->{'SourceDir'}."\n"); | ||
| 1050 | } | ||
| 1051 | |||
| 1052 | #просят почистить результат | ||
| 1053 | if ($CleanDest) { | ||
| 1054 | ForceRmDir($X,$X->{'DestinationDir'}) if $X->{'DestinationDir'}; | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | } | ||
| 1058 | |||
| 1059 | sub ForceRmDir{ | ||
| 1060 | my $X = shift; | ||
| 1061 | my $DirToClean=shift; | ||
| 1062 | return unless -e $DirToClean; | ||
| 1063 | my @FilesToKill; | ||
| 1064 | opendir(INPUT_FOLDER, $DirToClean); | ||
| 1065 | for (readdir(INPUT_FOLDER)){ | ||
| 1066 | next if /\A\.\Z|(\A\.\.\Z)/; | ||
| 1067 | if (-d "$DirToClean/$_"){ | ||
| 1068 | ForceRmDir($X,"$DirToClean/$_") | ||
| 1069 | } else { | ||
| 1070 | push (@FilesToKill, $_) | ||
| 1071 | } | ||
| 1072 | } | ||
| 1073 | closedir(INPUT_FOLDER); | ||
| 1074 | for (@FilesToKill){ | ||
| 1075 | unlink "$DirToClean/$_" or warn "error '$!' deleting file '$DirToClean/$_'" | ||
| 1076 | } | ||
| 1077 | rmdir($DirToClean) or $X->Error("Error removing dir $DirToClean!\n$!"); | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | sub Reaper { | ||
| 1081 | print "This method in package " . __PACKAGE__ . " and not defined in Processor class\n"; | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | sub FB3Creator { | ||
| 1085 | print "This method in package " . __PACKAGE__ . " and not defined in Processor class\n"; | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | sub TransformTable2Valid { | ||
| 1089 | my $X = shift; | ||
| 1090 | my %Args = @_; | ||
| 1091 | my $Node = $Args{'node'}; | ||
| 1092 | |||
| 1093 | foreach my $TH ($Node->findnodes('./tr/th')) { | ||
| 1094 | $TH->addChild(XML::LibXML::Text->new('')) unless $TH->getChildnodes; | ||
| 1095 | $X->Transform2Valid(node=>$TH); | ||
| 1096 | } | ||
| 1097 | foreach my $TD ($Node->findnodes('./tr/td')) { | ||
| 1098 | $TD->addChild(XML::LibXML::Text->new('')) unless $TD->getChildnodes; | ||
| 1099 | $X->Transform2Valid(node=>$TD); | ||
| 1100 | } | ||
| 1101 | foreach my $TL ($Node->findnodes('./title')) { | ||
| 1102 | $TL->addChild(XML::LibXML::Text->new('')) unless $TL->getChildnodes; | ||
| 1103 | $X->Transform2Valid(node=>$TL); | ||
| 1104 | } | ||
| 1105 | |||
| 1106 | return $Node; | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | sub Transform2Valid { | ||
| 1110 | my $X = shift; | ||
| 1111 | my %Args = @_; | ||
| 1112 | my $Node = $Args{'node'}; | ||
| 1113 | |||
| 1114 | my $NewNode = XML::LibXML::Element->new($Node->nodeName); #будем собирать новую ноду | ||
| 1115 | foreach ($Node->getAttributes) { #скопируем атрибуты | ||
| 1116 | $NewNode->setAttribute($_->name => $_->value); | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | my $Wrap = XML::LibXML::Element->new("p"); | ||
| 1120 | |||
| 1121 | foreach my $Child ($Node->getChildnodes) { | ||
| 1122 | if ($Child->nodeName =~ /^(p|table|ul|ol|title|subtitle|section)$/) { | ||
| 1123 | if ($Wrap->hasChildNodes) { | ||
| 1124 | #закроем текуший враппер и создадим новый | ||
| 1125 | $NewNode->addChild($Wrap->cloneNode(1)); | ||
| 1126 | $Wrap = XML::LibXML::Element->new("p") if $Wrap->hasChildNodes; | ||
| 1127 | } | ||
| 1128 | #на текущие ноды не применяем враппер | ||
| 1129 | $NewNode->addChild($Child->cloneNode(1)); | ||
| 1130 | next; | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | # остальные в <p>node</p> | ||
| 1134 | $Wrap->addChild($Child->cloneNode(1)); | ||
| 1135 | |||
| 1136 | } | ||
| 1137 | |||
| 1138 | #закрываем остатки враппера | ||
| 1139 | $NewNode->addChild($Wrap->cloneNode(1)) if $Wrap->hasChildNodes; | ||
| 1140 | |||
| 1141 | $Node->replaceNode($NewNode); | ||
| 1142 | return $Node; | ||
| 1143 | } | ||
| 1144 | |||
| 1145 | sub BuildOuterMeta { | ||
| 1146 | my $X = shift; | ||
| 1147 | my %Args = @_; | ||
| 1148 | |||
| 1149 | my $Meta = $Args{'meta'}; | ||
| 1150 | |||
| 1151 | my $ID = $Meta->{'id'}; | ||
| 1152 | my $LANGUAGE = $Meta->{'language'}; | ||
| 1153 | my $TITLE = $Meta->{'title'}; | ||
| 1154 | my $ANNOTATION = $Meta->{'annotation'}; | ||
| 1155 | my $GENRES = $Meta->{'genres'}; | ||
| 1156 | my $AUTHORS = $Meta->{'authors'}; | ||
| 1157 | my $DATE = $Meta->{'date'}; | ||
| 1158 | |||
| 1159 | my $MetaFile = $X->{'metadata'} if -s $X->{'metadata'}; | ||
| 1160 | if (-s $MetaFile) { | ||
| 1161 | my $xpc = XML::LibXML::XPathContext->new($Parser->load_xml( | ||
| 1162 | location => $MetaFile, | ||
| 1163 | expand_entities => 0, | ||
| 1164 | no_network => 1, | ||
| 1165 | load_ext_dtd => 0 | ||
| 1166 | )); | ||
| 1167 | $xpc->registerNs('fbd', &NS_FB3_DESCRIPTION); | ||
| 1168 | $ID = ($xpc->findnodes('/fbd:fb3-description')->[0])->getAttribute('id'); | ||
| 1169 | }; | ||
| 1170 | |||
| 1171 | my $DESCRIPTION = $X->{'STRUCTURE'}->{'DESCRIPTION'}; | ||
| 1172 | |||
| 1173 | $DESCRIPTION->{'DOCUMENT-INFO'}->{'ID'} = $ID if defined $ID; | ||
| 1174 | $DESCRIPTION->{'DOCUMENT-INFO'}->{'LANGUAGE'} = $LANGUAGE if defined $LANGUAGE; | ||
| 1175 | $DESCRIPTION->{'DOCUMENT-INFO'}->{'DATE'} = {'attributes'=>{'value'=>$DATE}} if defined $DATE; | ||
| 1176 | $DESCRIPTION->{'TITLE-INFO'}->{'BOOK-TITLE'} = $TITLE if defined $TITLE; | ||
| 1177 | $DESCRIPTION->{'TITLE-INFO'}->{'ANNOTATION'} = $ANNOTATION if defined $ANNOTATION; | ||
| 1178 | |||
| 1179 | $DESCRIPTION->{'TITLE-INFO'}->{'GENRES'} = [ map { trim($X,$_) } split /,/,$GENRES ] if defined $GENRES; | ||
| 1180 | $DESCRIPTION->{'TITLE-INFO'}->{'AUTHORS'} = [ map { BuildAuthorName($X,$_) } split /,/,$AUTHORS ] if defined $AUTHORS; | ||
| 1181 | |||
| 1182 | } | ||
| 1183 | |||
| 1184 | sub BuildAuthorName { | ||
| 1185 | my $X = shift; | ||
| 1186 | my ($FirstName, $MiddleName, $LastName) = split /\s/,shift,3; | ||
| 1187 | unless ($LastName) { | ||
| 1188 | $LastName = $MiddleName; | ||
| 1189 | $MiddleName = undef; | ||
| 1190 | } | ||
| 1191 | |||
| 1192 | return { | ||
| 1193 | 'id' => UUID(), | ||
| 1194 | 'first-name' => $FirstName, | ||
| 1195 | 'middle-name' => $MiddleName, | ||
| 1196 | 'last-name' => $LastName, | ||
| 1197 | }; | ||
| 1198 | } | ||
| 1199 | |||
| 1200 | sub EncodeUtf8 { | ||
| 1201 | my $X = shift; | ||
| 1202 | my $Out = shift; | ||
| 1203 | $Out = Encode::encode_utf8($Out) if $Out; | ||
| 1204 | return $Out; | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | sub IsEmptyLineValue { | ||
| 1208 | my $X = shift; | ||
| 1209 | my $Item = shift; | ||
| 1210 | return 0 unless ref $Item eq 'ARRAY'; | ||
| 1211 | |||
| 1212 | return 1 if (!scalar @$Item || (scalar @$Item == 1 && $Item->[0] =~ /^[\s\t\n\r]+$/) ); | ||
| 1213 | return 0; | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | sub CutLinkDiez { | ||
| 1217 | my $X = shift; | ||
| 1218 | my $Str = shift; | ||
| 1219 | $Str =~ s/^#//; | ||
| 1220 | return $Str; | ||
| 1221 | } | ||
| 1222 | |||
| 1223 | sub CorrectOuterLink{ | ||
| 1224 | my $X = shift; | ||
| 1225 | my $Str = shift; | ||
| 1226 | |||
| 1227 | unless ($Str =~ /^(http|https|mailto|ftp)\:(.+)/i) { | ||
| 1228 | $X->Msg("Find not valid Link and delete [$Str]\n"); | ||
| 1229 | return ""; | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | my $Protocol = $1; | ||
| 1233 | my $Link = $2; | ||
| 1234 | $Link = $X->trim_soft($Link); | ||
| 1235 | |||
| 1236 | if ($Protocol eq 'mailto') { | ||
| 1237 | unless (ValidEMAIL($Link)) { | ||
| 1238 | $X->Msg("Find not valid Email and delete [".$Protocol.":".$Link."]\n"); | ||
| 1239 | return ""; | ||
| 1240 | } | ||
| 1241 | } else { | ||
| 1242 | unless (ValidURL($Protocol.':'.$Link)) { | ||
| 1243 | $X->Msg("Find not valid URL and delete [".$Protocol.":".$Link."]\n"); | ||
| 1244 | return ""; | ||
| 1245 | } | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | return $Protocol.':'.$Link; | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | sub ValidURL{ | ||
| 1252 | my $Url=shift; | ||
| 1253 | return 0 unless $Url; | ||
| 1254 | return 0 if length($Url)>300; | ||
| 1255 | |||
| 1256 | my $RegExp = | ||
| 1257 | '^(https|http|ftp):\/\/'. # protocol | ||
| 1258 | '(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+'. # username | ||
| 1259 | '(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?'. # password | ||
| 1260 | '@)?(?#'. # auth requires @ | ||
| 1261 | ')((([a-z0-9]\.|[a-z0-9][a-z0-9-]*[a-z0-9]\.)*'. # domain segments AND | ||
| 1262 | '[a-z][a-z0-9-]*[a-z0-9]'. # top level domain OR | ||
| 1263 | '|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}'. | ||
| 1264 | '(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])'. # IP address | ||
| 1265 | ')(:\d{1,5})?'. # port | ||
| 1266 | ')(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'. # path | ||
| 1267 | '(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'. # query string | ||
| 1268 | '?)?)?'. # path and query string optional | ||
| 1269 | '(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'. # fragment | ||
| 1270 | '$'; | ||
| 1271 | |||
| 1272 | return $Url=~/$RegExp/i; | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | sub ValidEMAIL{ | ||
| 1276 | my $Email=shift; | ||
| 1277 | return 0 unless $Email; | ||
| 1278 | return 0 if length($Email)>50; | ||
| 1279 | return $Email=~ /^[-+a-z0-9_]+(\.[-+a-z0-9_]+)*\@([-a-z0-9_]+\.)+[a-z]{2,10}$/i; | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | sub ShitFixFile { | ||
| 1283 | my $X = shift; | ||
| 1284 | my $Fname = uri_unescape(shift); | ||
| 1285 | |||
| 1286 | my $Content; | ||
| 1287 | open my $Fo,"<".$Fname or $X->Error("Can't open file $Fname: $!"); | ||
| 1288 | map {$Content.=$_;} <$Fo>; | ||
| 1289 | close $Fo; | ||
| 1290 | |||
| 1291 | $Content = $X->ShitFix($Content); | ||
| 1292 | |||
| 1293 | open my $Fs,">".$Fname or $X->Error("Can't open file $Fname: $!"); | ||
| 1294 | print $Fs $Content; | ||
| 1295 | close $Fs; | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | sub MetaFix { | ||
| 1299 | my $X = shift; | ||
| 1300 | my $Str = shift; | ||
| 1301 | # закрываем то, что в html не обязано | ||
| 1302 | $Str =~ s/<\s*\/\s*(meta|link)\s*>//gi; | ||
| 1303 | $Str =~ s/(<(meta|link|img)[^>]+?)\s*?(\/?\s*?)>/$1\/>/g; | ||
| 1304 | |||
| 1305 | return $Str; | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | #phantomjs любит превращать кое-что в нечитаемое для Libxml | ||
| 1309 | sub SomeFix { | ||
| 1310 | my $X = shift; | ||
| 1311 | my $Str = shift; | ||
| 1312 | |||
| 1313 | $Str =~ s#<([bB][rR])>\s*</\1>#<br/>#g; # <br> </br> => <br/> | ||
| 1314 | $Str =~ s/<\s*[bB][rR]\s*>/<br\/>/g; # <br> => <br/> | ||
| 1315 | |||
| 1316 | return $Str; | ||
| 1317 | } | ||
| 1318 | |||
| 1319 | |||
| 1320 | sub ShitFix { | ||
| 1321 | my $X = shift; | ||
| 1322 | my $Str = shift; | ||
| 1323 | # /i здесь вызывает невероятные тормоза к сожалению | ||
| 1324 | $Str =~ s#<([iI][mM][gG]) ([^>]+?/?)>\s*</\1>#<img $2>#g; # <img> </img> => <img/> | ||
| 1325 | |||
| 1326 | #DOM такое не любит | ||
| 1327 | $Str =~ s/<([aA])([^>]*?)\/\s*>/<$1$2><\/$1>/g; # <a/> => <a></a> | ||
| 1328 | $Str =~ s/<([dD][iI][vV])([^>]*?)\/\s*>/<$1$2><\/$1>/g; # <div/> => <div></div> | ||
| 1329 | |||
| 1330 | $Str = $X->SomeFix($Str); | ||
| 1331 | $Str = $X->MetaFix($Str); | ||
| 1332 | |||
| 1333 | return $Str; | ||
| 1334 | } | ||
| 1335 | |||
| 1336 | sub ParseMetaFile { | ||
| 1337 | my $X = shift; | ||
| 1338 | |||
| 1339 | my $MetaFile = $X->{'metadata'} if -f $X->{'metadata'}; | ||
| 1340 | |||
| 1341 | if ($MetaFile) { | ||
| 1342 | |||
| 1343 | my $DESCRIPTION = $X->{'STRUCTURE'}->{'DESCRIPTION'}; | ||
| 1344 | |||
| 1345 | Msg($X,"Parse metafile ".$MetaFile."\n"); | ||
| 1346 | my $xpc = XML::LibXML::XPathContext->new($Parser->load_xml( | ||
| 1347 | location => $MetaFile, | ||
| 1348 | expand_entities => 0, | ||
| 1349 | no_network => 1, | ||
| 1350 | load_ext_dtd => 0 | ||
| 1351 | )); | ||
| 1352 | $xpc->registerNs('fbd', &NS_FB3_DESCRIPTION); | ||
| 1353 | |||
| 1354 | my $ID = ($xpc->findnodes('/fbd:fb3-description')->[0])->getAttribute('id'); | ||
| 1355 | $DESCRIPTION->{'DOCUMENT-INFO'}->{'ID'} = $ID if defined $ID; | ||
| 1356 | |||
| 1357 | my $TITLE = $xpc->findnodes('/fbd:fb3-description/fbd:title/fbd:main')->[0]; | ||
| 1358 | $DESCRIPTION->{'TITLE-INFO'}->{'BOOK-TITLE'} = EncodeUtf8($X,$TITLE->string_value) if defined $TITLE; | ||
| 1359 | |||
| 1360 | my $ANNOTATION = $xpc->findnodes('/fbd:fb3-description/fbd:annotation/fbd:p')->[0]; | ||
| 1361 | $DESCRIPTION->{'TITLE-INFO'}->{'ANNOTATION'} = EncodeUtf8($X,$ANNOTATION->string_value) if defined $ANNOTATION; | ||
| 1362 | |||
| 1363 | my $LANGUAGE = $xpc->findnodes('/fbd:fb3-description/fbd:lang')->[0]; | ||
| 1364 | $DESCRIPTION->{'DOCUMENT-INFO'}->{'LANGUAGE'} = $LANGUAGE->string_value if defined $LANGUAGE; | ||
| 1365 | |||
| 1366 | my @GENRES = map { EncodeUtf8($X,$_->string_value) } ($xpc->findnodes('/fbd:fb3-description/fbd:fb3-classification/fbd:subject')); | ||
| 1367 | $DESCRIPTION->{'TITLE-INFO'}->{'GENRES'} = [ @GENRES ]; | ||
| 1368 | |||
| 1369 | my @AUTHORS; | ||
| 1370 | foreach my $Subject ($xpc->findnodes('/fbd:fb3-description/fbd:fb3-relations/fbd:subject')) { | ||
| 1371 | my $SubjID = $Subject->getAttribute("id") ; | ||
| 1372 | my $SubjLink = $Subject->getAttribute("link") ; | ||
| 1373 | my $SubjPercent = $Subject->getAttribute("percent") ; | ||
| 1374 | my $SubjNAME = $Subject->getElementsByTagName("main"); | ||
| 1375 | |||
| 1376 | my ($FirstName, $MiddleName, $LastName) = split /\s+/, $SubjNAME, 3; | ||
| 1377 | |||
| 1378 | unless ($LastName) { | ||
| 1379 | $LastName = $MiddleName; | ||
| 1380 | $MiddleName = undef; | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | push @AUTHORS, { | ||
| 1384 | 'id' => $SubjID, | ||
| 1385 | 'link' => $SubjLink, | ||
| 1386 | 'percent' => $SubjPercent, | ||
| 1387 | 'first-name' => EncodeUtf8($X,$FirstName), | ||
| 1388 | 'middle-name' => EncodeUtf8($X,$MiddleName), | ||
| 1389 | 'last-name' => EncodeUtf8($X,$LastName), | ||
| 1390 | }; | ||
| 1391 | } | ||
| 1392 | $DESCRIPTION->{'TITLE-INFO'}->{'AUTHORS'} = [ @AUTHORS ]; | ||
| 1393 | |||
| 1394 | } | ||
| 1395 | |||
| 1396 | } | ||
| 1397 | |||
| 1398 | ### BENCHMARK | ||
| 1399 | |||
| 1400 | #Точка старта | ||
| 1401 | sub _bs { | ||
| 1402 | my $X = shift; | ||
| 1403 | my $Key = shift; | ||
| 1404 | my $Desc = shift || undef; | ||
| 1405 | return if ( !(exists $X->{'bench'} && $X->{'bench'}) && !(exists $X->{'bench2file'} && $X->{'bench2file'}) ); | ||
| 1406 | |||
| 1407 | $X->Error("Bench: _bs(); key not defined in string format") unless $Key; | ||
| 1408 | $X->Error("Bench: _bs(): Key is not a string") if ref $Key; | ||
| 1409 | |||
| 1410 | $X->{'bench_list'}->{$Key} = { | ||
| 1411 | 'desc' => $Desc, | ||
| 1412 | 'timers' => [] | ||
| 1413 | } unless exists $X->{'bench_list'}->{$Key}; | ||
| 1414 | |||
| 1415 | my $Timers = $X->{'bench_list'}->{$Key}->{'timers'}; | ||
| 1416 | |||
| 1417 | if (@$Timers) { | ||
| 1418 | $X->Error("Bench: last timer don't closed with X->_be('$Key') function") | ||
| 1419 | if exists $Timers->[scalar @$Timers - 1]->{'start'} && !exists $Timers->[scalar @$Timers - 1]->{'end'}; | ||
| 1420 | } | ||
| 1421 | |||
| 1422 | my $ts = gettimeofday(); | ||
| 1423 | push @$Timers, { | ||
| 1424 | 'start' => $ts, | ||
| 1425 | }; | ||
| 1426 | } | ||
| 1427 | |||
| 1428 | #Точка окончания | ||
| 1429 | sub _be { | ||
| 1430 | my $X = shift; | ||
| 1431 | my $Key = shift; | ||
| 1432 | return if ( !(exists $X->{'bench'} && $X->{'bench'}) && !(exists $X->{'bench2file'} && $X->{'bench2file'}) ); | ||
| 1433 | |||
| 1434 | $X->Error("Bench: _be(); key not defined in string format") unless $Key; | ||
| 1435 | $X->Error("Bench: _be(): Key is not a string") if ref $Key; | ||
| 1436 | |||
| 1437 | $X->Error("Bench: _be(); Key '$Key' not exists. Do you make X->_bs($Key)??") unless exists $X->{'bench_list'}->{$Key}; | ||
| 1438 | |||
| 1439 | my $Timers = $X->{'bench_list'}->{$Key}->{'timers'}; | ||
| 1440 | $X->Error("Bench: _be(): Can't close timer. Two o more calls of _be($Key)??") | ||
| 1441 | if exists $Timers->[scalar @$Timers - 1]->{'end'}; | ||
| 1442 | my $te = gettimeofday(); | ||
| 1443 | $Timers->[scalar @$Timers - 1]->{'end'} = $te; | ||
| 1444 | } | ||
| 1445 | |||
| 1446 | #Сброс статистики | ||
| 1447 | sub _bf { | ||
| 1448 | my $X = shift; | ||
| 1449 | return if ( !(exists $X->{'bench'} && $X->{'bench'}) && !(exists $X->{'bench2file'} && $X->{'bench2file'}) ); | ||
| 1450 | |||
| 1451 | my $Out = "BENCHMARK ".localtime().":\n\n"; | ||
| 1452 | |||
| 1453 | foreach my $Key (sort keys %{$X->{'bench_list'}}) { | ||
| 1454 | my $Item = $X->{'bench_list'}->{$Key}; | ||
| 1455 | my $Cnt = scalar @{$Item->{'timers'}}; | ||
| 1456 | my $Summ=0; | ||
| 1457 | $Out .= "[key: '$Key'] "; | ||
| 1458 | $Out .= "[cnt: $Cnt] "; | ||
| 1459 | foreach my $t ( @{$Item->{'timers'}} ) { | ||
| 1460 | $Summ += ($t->{'end'} - $t->{'start'}); | ||
| 1461 | } | ||
| 1462 | $Out .= "[time: ".sprintf('%.4f',$Summ)." sec] "; | ||
| 1463 | $Out .= "[avg: ".sprintf('%.4f',$Summ/$Cnt)." sec]\n"; | ||
| 1464 | $Out .= "desc: ".$Item->{'desc'}."\n\n" if $Item->{'desc'}; | ||
| 1465 | } | ||
| 1466 | |||
| 1467 | $X->{'bench_list'} = {}; | ||
| 1468 | |||
| 1469 | $X->Msg("\n".$Out,'w',1) if $X->{'bench'}; | ||
| 1470 | |||
| 1471 | if ($X->{'bench2file'}) { | ||
| 1472 | open my $F,">>:utf8",$X->{'bench2file'} or $X->Error($!); | ||
| 1473 | print $F $Out; | ||
| 1474 | close $F; | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | } | ||
| 1478 | |||
| 1479 | sub isUseImageProfile { | ||
| 1480 | my $X = shift; | ||
| 1481 | my $ImgType = lc(shift) || return; | ||
| 1482 | return 1 if exists $UseImgProfile{$ImgType} && $UseImgProfile{$ImgType}; | ||
| 1483 | } | ||
| 1484 | |||
| 1485 | sub isConvertImageType { | ||
| 1486 | my $X = shift; | ||
| 1487 | my $ImgType = shift || return; | ||
| 1488 | return grep {lc($ImgType) eq $_} @ConvertImgFormat; | ||
| 1489 | } | ||
| 1490 | |||
| 1491 | sub isAllowedImageType { | ||
| 1492 | my $X = shift; | ||
| 1493 | my $ImgType = shift || return; | ||
| 1494 | return grep {lc($ImgType) eq $_} @AccessImgFormat; | ||
| 1495 | } | ||
| 1496 | |||
| 1497 | sub GuessLang { | ||
| 1498 | my $X = shift; | ||
| 1499 | my $Text = shift; | ||
| 1500 | return if length($Text) < 150; | ||
| 1501 | $Text = substr($Text,0,2000); | ||
| 1502 | my %LangsHashLocal = Lingua::Identify::langof($Text); | ||
| 1503 | my @SortedProbability = sort {$b <=> $a} values(%LangsHashLocal); | ||
| 1504 | foreach my $Lang (keys %LangsHashLocal) { | ||
| 1505 | return lc($Lang) if $LangsHashLocal{$Lang} == $SortedProbability[0]; | ||
| 1506 | } | ||
| 1507 | } | ||
| 1508 | |||
| 1509 | sub Img2JPG { | ||
| 1510 | my $X = shift; | ||
| 1511 | my $ImgFile = shift; | ||
| 1512 | my $UseProfile = shift || undef; | ||
| 1513 | my $To = $ImgFile.".jpg"; | ||
| 1514 | my $Image = new Image::Magick; | ||
| 1515 | |||
| 1516 | $Image->Read($ImgFile); #почему-то у IM "or die" всегда срабатывает...; | ||
| 1517 | |||
| 1518 | if ($UseProfile) { | ||
| 1519 | if (!-s $sRGBProfile) { | ||
| 1520 | $X->Msg("Can't find sRGB ICC profile $sRGBProfile. Try convert to RGB colorspace without profile. May wrong colors for CMYK->RGB\n"); | ||
| 1521 | $Image->Quantize(colorspace=>'sRGB'); | ||
| 1522 | } elsif (!-s $CMYKProfile) { | ||
| 1523 | $X->Msg("Can't find CMYK ICC profile $CMYKProfile. Try convert to RGB colorspace without profile. May wrong colors for CMYK->RGB\n"); | ||
| 1524 | $Image->Quantize(colorspace=>'sRGB'); | ||
| 1525 | } else { | ||
| 1526 | |||
| 1527 | #вытираем профили | ||
| 1528 | $Image->Profile(name => 'ICC', profile => ''); | ||
| 1529 | $Image->Profile(name => 'IPTC', profile => ''); | ||
| 1530 | |||
| 1531 | open my $PHRGB,"<".$sRGBProfile or die $!; | ||
| 1532 | binmode($PHRGB); | ||
| 1533 | my $ProfRGBBLOB = join("", <$PHRGB>); | ||
| 1534 | close $PHRGB; | ||
| 1535 | |||
| 1536 | open my $PHCMYK,"<".$CMYKProfile or die $!; | ||
| 1537 | binmode($PHCMYK); | ||
| 1538 | my $ProfCMYKBLOB = join("", <$PHCMYK>); | ||
| 1539 | close $PHCMYK; | ||
| 1540 | |||
| 1541 | $Image->Profile(name => 'ICC', profile => $ProfCMYKBLOB); | ||
| 1542 | $Image->Profile(name => 'ICC', profile => $ProfRGBBLOB); | ||
| 1543 | |||
| 1544 | } | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | $Image->[0]->Write($To); | ||
| 1548 | |||
| 1549 | unless (-s $To) { | ||
| 1550 | $X->Error("Cant't convert $ImgFile to JPG"); | ||
| 1551 | } else { | ||
| 1552 | unlink($ImgFile); | ||
| 1553 | return $To; | ||
| 1554 | } | ||
| 1555 | } | ||
| 1556 | |||
| 1557 | =head1 LICENSE AND COPYRIGHT | ||
| 1558 | |||
| 1559 | Copyright (C) 2018 Litres.ru | ||
| 1560 | |||
| 1561 | The GNU Lesser General Public License version 3.0 | ||
| 1562 | |||
| 1563 | FB3::Convert is free software: you can redistribute it and/or modify it | ||
| 1564 | under the terms of the GNU Lesser General Public License as published by | ||
| 1565 | the Free Software Foundation, either version 3.0 of the License. | ||
| 1566 | |||
| 1567 | FB3::Convert is distributed in the hope that it will be useful, but | ||
| 1568 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 1569 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
| 1570 | License for more details. | ||
| 1571 | |||
| 1572 | Full text of License L<http://www.gnu.org/licenses/lgpl-3.0.en.html>. | ||
| 1573 | |||
| 1574 | =cut | ||
| 1575 | |||
| 1576 | |||
| 1577 | 1; | ||