updated git and svn scripts
[xrzperl.git] / epub_convert_text
blob6546084e63179b85fb904724d589c48e28c4b519
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 = ();
40 my $source_base = $input;
41 $source_base =~ s/\.[^.]+$//;
42 my $target_base = $output || $input;
43 $target_base =~ s/\.[^.]+$//;
44 my $input_file_xhtml = "content.xhtml";
45 $output = $target_base . ".epub" unless($output);
46 my $title = $target_base;
47 $title =~ s/^.*\///;
49 print STDERR "Convert $input to $input_file_xhtml...\n";
50 unlink $input_file_xhtml if(-f $input_file_xhtml);
51 system(
52 "epub_text2xhtml",
53 "-t",$title,
54 "-i",$input,
55 "-o",$input_file_xhtml,
57 die("Failed!") unless(-f $input_file_xhtml);
58 $def_arg{"--title"}=$title;
59 foreach my $img_ext qw/.jpg .png .jpeg/
61 if (-f "${source_base}${img_ext}")
63 $def_arg{"--cover"}="$source_base$img_ext";
65 elsif(-f "cover$img_ext")
67 $def_arg{"--cover"}="cover$img_ext";
71 my $last_arg="";
72 foreach(@ARGV)
74 if(m/^-/) {
75 $def_arg{$_} = "!EMPTY!_!MARK!";
76 $last_arg = $_;
78 elsif($last_arg) {
79 $def_arg{$last_arg} = $_;
83 print STDERR "Convert $input_file_xhtml to $output...\n";
84 unlink $output if(-f $output);
85 my @calibre_args = ();
86 foreach (keys %def_arg) {
87 if($def_arg{$_} eq '!EMPTY!_!MARK!') {
88 push @calibre_args,$_;
90 else {
91 push @calibre_args,$_,$def_arg{$_};
95 &run("epub_convert",$input_file_xhtml,$output,@calibre_args);
97 __END__
99 =pod
101 =head1 NAME
103 epub_convert_text - Covert plain text file to EPUB book
105 =head1 SYNOPSIS
107 epub_convert_text input_file [[output_file] [options...]]
109 =head1 OPTIONS
111 options are passing to calibre 'B<ebook-convert>'
113 =head1 DESCRIPTION
115 Covert plain text file to EPUB, Implemented
116 using "epub_text2xhtml" and "epub_convert"
117 which depends on "ebook-convert" privided
118 by "calibre".
120 =head1 CHANGELOG
122 2010-06-07 xiaoranzzz <xiaoranzzz@myplace.hell>
124 * file created.
126 =head1 AUTHOR
128 xiaoranzzz <xiaoranzzz@myplace.hell>
130 =cut