| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Getopt::Long; | ||
| 5 | use FB3; | ||
| 6 | use FB3::Convert; | ||
| 7 | use utf8; | ||
| 8 | use File::ShareDir qw/dist_dir/; | ||
| 9 | |||
| 10 | my %OPT; | ||
| 11 | GetOptions( | ||
| 12 | 'verbose|v:1' => \$OPT{'verbose'}, | ||
| 13 | 'help|h' => \$OPT{'help'}, | ||
| 14 | 'force|f' => \$OPT{'force'}, | ||
| 15 | 'source|s=s' => \$OPT{'source'}, | ||
| 16 | 'src_type|st=s' => \$OPT{'src_type'}, | ||
| 17 | 'destination_dir|dd=s' => \$OPT{'dd'}, | ||
| 18 | 'destination_file|df=s' => \$OPT{'df'}, | ||
| 19 | 'xsd=s' => \$OPT{xsd_dir}, | ||
| 20 | 'metadata|md=s' => \$OPT{'md'}, | ||
| 21 | 'validate|vl=s' => \$OPT{'vl'}, | ||
| 22 | 'name|n:1' => \$OPT{'showname'}, | ||
| 23 | 'b:1' => \$OPT{'b'}, | ||
| 24 | 'bf:s' => \$OPT{'bf'}, | ||
| 25 | 'phantomjs|phjs=s' => \$OPT{'phjs'}, | ||
| 26 | 'euristic|e' => \$OPT{'eur'}, | ||
| 27 | 'euristic_debug|ed=s' => \$OPT{'eur_deb'}, | ||
| 28 | |||
| 29 | 'meta_id=s' => \$OPT{'meta_id'}, | ||
| 30 | 'meta_lang|meta_language=s' => \$OPT{'meta_lang'}, | ||
| 31 | 'meta_title=s' => \$OPT{'meta_title'}, | ||
| 32 | 'meta_annotation=s' => \$OPT{'meta_annotation'}, | ||
| 33 | 'meta_genres=s' => \$OPT{'meta_genres'}, | ||
| 34 | 'meta_authors=s' => \$OPT{'meta_authors'}, | ||
| 35 | 'meta_date=s' => \$OPT{'meta_date'}, | ||
| 36 | ); | ||
| 37 | help() if $OPT{'help'}; | ||
| 38 | |||
| 39 | my $XsdPath = $OPT{xsd_dir} || FB3::SchemasDirPath(); | ||
| 40 | my $XslPath = dist_dir('FB3-Convert'); | ||
| 41 | |||
| 42 | if ( $OPT{'vl'} ) { | ||
| 43 | my $Error = FB3::Convert->new('empty' => 1)->Validate('path' => $OPT{'vl'}, 'xsd' => $XsdPath); | ||
| 44 | unless ( $Error ) { | ||
| 45 | printf("\n%s is valid FB3 document\n\n", $OPT{'vl'}); | ||
| 46 | exit 0; | ||
| 47 | } | ||
| 48 | printf("\n%s is NOT valid FB3 document\n\n%s\n", $OPT{'vl'}, $Error); | ||
| 49 | exit 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | $OPT{'source'} = $ARGV[0] unless $OPT{'source'}; | ||
| 53 | $OPT{'df'} = $ARGV[1] unless $OPT{'df'}; | ||
| 54 | |||
| 55 | my $FName = $OPT{'source'}; | ||
| 56 | $FName =~ s/\.\w+$//; | ||
| 57 | $OPT{'df'} = $FName.'.fb3' if !$OPT{'dd'} && !$OPT{'df'}; | ||
| 58 | $OPT{'bf'} = $FName.'.bench' if defined $OPT{'bf'} && !$OPT{'bf'}; | ||
| 59 | |||
| 60 | unless ($OPT{'source'}) { | ||
| 61 | print "\nsource file not defined\n"; | ||
| 62 | help(); | ||
| 63 | } | ||
| 64 | |||
| 65 | my $Obj = new FB3::Convert( | ||
| 66 | 'source' => $OPT{'source'}, | ||
| 67 | 'src_type' => $OPT{'src_type'}, | ||
| 68 | 'destination_dir' => $OPT{'dd'}, | ||
| 69 | 'destination_file' => $OPT{'df'}, | ||
| 70 | 'verbose' => $OPT{'verbose'}, | ||
| 71 | 'metadata' => $OPT{'md'}, | ||
| 72 | 'showname' => $OPT{'showname'}, | ||
| 73 | 'bench' => $OPT{'b'}, | ||
| 74 | 'bench2file' => $OPT{'bf'}, | ||
| 75 | 'phantom_js_path' => $OPT{'phjs'}, | ||
| 76 | 'euristic' => $OPT{'eur'}, | ||
| 77 | 'euristic_debug' => $OPT{'eur_deb'}, | ||
| 78 | 'xsl_path' => $XslPath, | ||
| 79 | |||
| 80 | 'meta' => { | ||
| 81 | 'id' => $OPT{'meta_id'}, | ||
| 82 | 'language' => $OPT{'meta_lang'}, | ||
| 83 | 'title' => $OPT{'meta_title'}, | ||
| 84 | 'annotation' => $OPT{'meta_annotation'}, | ||
| 85 | 'genres' => $OPT{'meta_genres'}, | ||
| 86 | 'authors' => $OPT{'meta_authors'}, | ||
| 87 | 'date' => $OPT{'meta_date'}, | ||
| 88 | }, | ||
| 89 | ); | ||
| 90 | |||
| 91 | $Obj->_bs('ALL','Полная конвертация'); | ||
| 92 | |||
| 93 | $Obj->Reap(); | ||
| 94 | |||
| 95 | $Obj->_bs('fb3_create','Создание FB3 из данных, доводка до валидности'); | ||
| 96 | my $FB3Path = $Obj->FB3Create(); | ||
| 97 | $Obj->_be('fb3_create'); | ||
| 98 | $Obj->Msg("FB3: ".$FB3Path." created\n","w"); | ||
| 99 | |||
| 100 | $Obj->_bs('validate_fb3','Валидация полученного FB3'); | ||
| 101 | my $ValidErr = $Obj->Validate('xsd'=>$XsdPath); | ||
| 102 | $Obj->_be('validate_fb3'); | ||
| 103 | print $ValidErr if $ValidErr; | ||
| 104 | |||
| 105 | if ($OPT{'df'} && (!$ValidErr || $OPT{'force'}) ) { | ||
| 106 | $Obj->_bs('pack','Упаковка FB3 -> zip'); | ||
| 107 | $Obj->FB3_2_Zip(); | ||
| 108 | $Obj->_be('pack'); | ||
| 109 | } | ||
| 110 | |||
| 111 | $Obj->_bs('cleanup','Сборка мусора'); | ||
| 112 | $Obj->Cleanup($ValidErr?1:0); | ||
| 113 | $Obj->_be('cleanup'); | ||
| 114 | |||
| 115 | $Obj->_be('ALL'); | ||
| 116 | |||
| 117 | $Obj->_bf(); | ||
| 118 | |||
| 119 | sub help { | ||
| 120 | print <<_END | ||
| 121 | |||
| 122 | USAGE: convert2fb3.pl --source|s= <input.file> [--verbose|v] [--help|h] [--force|f] [--destination_dir|dd <dest.fb3> | --destination_file|df] [--src_type|st] [--name|n] [--metadata|md] [--validate|vl=] [--euristic|e] [--euristic_debug|ed] [--phantomjs|phjs] | ||
| 123 | |||
| 124 | --help|h : print this text | ||
| 125 | --verbose|v : print processing status. Show parsing warnings if Verbose > 1 | ||
| 126 | --force|f : ignore validation error | ||
| 127 | --src_type|st : source format (fb2|epub) | ||
| 128 | --source|s : path to source file | ||
| 129 | --destination_dir|dd : path for non zipped fb3 | ||
| 130 | --destination_file|df : path for zipped fb3 | ||
| 131 | --metadata|md : XML meta description file | ||
| 132 | --name|n : show name of reaped epub file | ||
| 133 | --validate|vl : don't convert, only validate fb3 file from path | ||
| 134 | --euristic|e : try euristic analize for detect strange titles | ||
| 135 | --euristic_debug|ed : path to dir for euristica debug | ||
| 136 | --phantomjs|phjs : path to binary 'phantomjs'. Must be installed for euristica analize titles <http://phantomjs.org/> (with --e opt) | ||
| 137 | |||
| 138 | META: | ||
| 139 | --meta_id | ||
| 140 | --meta_lang | ||
| 141 | --meta_title | ||
| 142 | --meta_annotation | ||
| 143 | --meta_date | ||
| 144 | --meta_genres : can be ',' separated | ||
| 145 | --meta_authors : "first middle last". full names can be ',' separated | ||
| 146 | |||
| 147 | _END | ||
| 148 | ; | ||
| 149 | exit; | ||
| 150 | } |