updated git and svn scripts
[xrzperl.git] / epub_convert
blob0a1b8b37ec333565232358d8c1f66fce5ddff0d1
1 #!/usr/bin/perl -w
2 # $Id$
3 use strict;
4 require v5.10.0;
5 our $VERSION = 'v0.1';
7 BEGIN
9 my $PROGRAM_DIR = $0;
10 $PROGRAM_DIR =~ s/[^\/\\]+$//;
11 $PROGRAM_DIR = "./" unless($PROGRAM_DIR);
12 unshift @INC,
13 map "$PROGRAM_DIR$_",qw{modules lib ../modules ..lib};
16 unless(@ARGV)
18 require MyPlace::Usage;
19 MyPlace::Usage::PrintHelp();
22 sub run
24 my $cmd = shift;
25 print STDERR $cmd," ",join(" ",map "\'$_\'",@_),"\n";
26 system($cmd,@_) == 0;
30 my $input = shift;
31 my $output = shift;
32 if($output =~ m/^--/) {
33 unshift @ARGV,$output;
34 $output = undef;
36 die("Usage: input_file [[output_file] [options...]]\n") unless($input);
38 my %def_arg =
39 #--verbose !EMPTY!_!MARK!
40 qw/
41 --margin-top 0
42 --margin-bottom 0
43 --margin-left 0
44 --margin-right 0
45 --language zh
46 --authors xiaoranzzz
47 --publisher xiaoranzzz
48 --tags xiaoranzzz
49 --no-svg-cover
50 --pretty-print
52 $def_arg{"--level1-toc"} = "//h:h2";
53 $def_arg{"--level2-toc"} = "//h:h3";
54 $def_arg{"--pubdate"} = localtime();
56 my $source_base = $input;
57 $source_base =~ s/\.[^.]+$//;
58 my $target_base = $output || $input;
59 $target_base =~ s/\.[^.]+$//;
60 $output = $target_base . ".epub" unless($output);
61 my $title = $target_base;
62 $title =~ s/^.*\///;
64 $def_arg{"--title"}=$title;
65 $def_arg{"--extract-to"}=$target_base;
66 foreach my $img_ext qw/.jpg .png .jpeg/
68 if (-f "${source_base}${img_ext}")
70 $def_arg{"--cover"}="$source_base$img_ext";
72 elsif(-f "cover$img_ext")
74 $def_arg{"--cover"}="cover$img_ext";
78 my $last_arg="";
79 foreach(@ARGV)
81 if(m/^-/) {
82 $def_arg{$_} = "!EMPTY!_!MARK!";
83 $last_arg = $_;
85 elsif($last_arg) {
86 $def_arg{$last_arg} = $_;
90 print STDERR "Convert $input to $output...\n";
91 unlink $output if(-f $output);
92 my @calibre_args = ();
93 foreach (keys %def_arg) {
94 if($def_arg{$_} eq '!EMPTY!_!MARK!') {
95 push @calibre_args,$_;
97 else {
98 push @calibre_args,$_,$def_arg{$_};
101 &run("ebook-convert",$input,$output,@calibre_args);
103 __END__
105 =pod
107 =head1 NAME
109 epub_convert - Covert to EPUB book
111 =head1 SYNOPSIS
113 epub_convert input [[output_file] [options...]]
115 =head1 OPTIONS
117 options are passing to calibre 'B<ebook-convert>'
119 =head1 DESCRIPTION
121 Covert input to EPUB, Implemented
122 using "calibre ebook-convert".
124 =head1 CHANGELOG
126 2010-06-07 xiaoranzzz <xiaoranzzz@myplace.hell>
128 * file created.
130 =head1 AUTHOR
132 xiaoranzzz <xiaoranzzz@myplace.hell>
134 =cut