Make it possible to generate 29tsoc ebooks
[project-aon.git] / common / scripts / gbtoxhtml-simple.pl
1 #!/usr/bin/perl -w
2 #
3 # gbtoxhtml-simple.xsl
4 #
5 ######################################################################
6
7 use strict;
8
9 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
10
11 my $PROGRAM_NAME    = 'gbtoxhtml-simple.pl';
12 my $USAGE           = "$PROGRAM_NAME [options] book-code\n\t--xml=[book XML]\n\t--meta=[metadata file]\n\t--xsl=[XSL transformation]\n\t--language=[language area of input data (output determined by meta file)]\n\t--verbose\n";
13
14 my $FILENAME_SEPARATOR = '/';
15
16 my $RXP        = '/usr/bin/rxp';
17 unless( -e $RXP && -x $RXP ) { 
18     # try somewhere else
19     $RXP = '/usr/local/bin/rxp';
20 }
21 my $ZIP        = '/usr/bin/zip';
22 my $JAVA       = '/usr/bin/java';
23 # latest binary download names the relevant jar "xalan.jar"
24 # older installations may have "xalan2.jar"
25 my $XALAN_JAR  = '/usr/share/java/xalan.jar';
26 unless( -e $XALAN_JAR ) { 
27     # try somewhere else
28     $XALAN_JAR = '/usr/share/java/xalan2.jar';
29 }
30
31 # Check that all the binaries are were want them
32
33 my @BINARIES;
34 push @BINARIES, ($RXP, $ZIP, $JAVA, $XALAN_JAR);
35
36 foreach (@BINARIES) {
37     if ( ! -e $_ ) {
38             die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
39     }
40 }
41
42 ###
43
44 my $bookCode     = '';
45 my $bookXML      = '';
46 my $metaFile     = '';
47 my $xhtmlXSL     = 'common/xsl/xhtml-simple.xsl';
48 my $language     = 'en';
49
50 my $verbose = 0;
51
52 while( $#ARGV > -1 ) {
53     my $cmdLineItem = shift @ARGV;
54     if( $cmdLineItem =~ /^--xml=(.+)$/ ) {
55         $bookXML = $1;
56     }
57     elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
58         $metaFile = $1;
59     }
60     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
61         $xhtmlXSL = $1;
62     }
63     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
64         $language = $1;
65     }
66     elsif( $cmdLineItem =~ /^--verbose/ ) {
67         $verbose = 1;
68     }
69     else { 
70         $bookCode = $cmdLineItem;
71     }
72 }
73
74 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
75 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/dever"; }
76 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
77 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
78
79 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
80     open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
81 }
82 else { die qq{Improper metadata file ($metaFile)\n}; }
83
84 my $meta = '';
85 while( my $line = <META> ) {
86     $meta .= $line if $line !~ /^[[:space:]]*#/;
87 }
88 close META;
89
90 my $rulesString = '';
91 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
92     $rulesString = $1;
93 }
94 else {
95     die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
96 }
97
98 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
99 my %rulesHash;
100 foreach my $rule (@rules) {
101     if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
102         $rulesHash{ $1 } = $2;
103     }
104     else {
105         die "Unrecognized rule syntax:\n$rule\n";
106     }
107 }
108
109 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
110     $bookXML = $1;
111     if( -e $bookXML && -f $bookXML && -r $bookXML ) {
112         system( $RXP, '-Vs', $bookXML ) == 0 or die( "XML validation failed\n" );
113     }
114     unless( defined $rulesHash{'language'} ) { die "Metadata file leaves language unspecified\n"; }
115     unless( defined $rulesHash{'book-series'} ) { die "Metadata file leaves book series unspecified\n"; }
116
117     my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'xhtml-simple' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
118     unless( -e $outPath && -d $outPath ) {
119         my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
120         my $dirPath = '';
121         for( my $i = 0; $i <= $#dirs; ++$i ) {
122             $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
123             if( -e $dirPath && ! -d $dirPath ) { die "Output directory name exists and is not a directory\n"; }
124             unless( -e $dirPath ) {
125                 mkdir $dirPath or die( "Unable to create output directory ($outPath): $!\n" );
126             }
127         }
128     }
129     unless( -e $outPath && -d $outPath ) {
130         die "Unknown error creating output directory\n";
131     }
132
133     my $bookPath = "$outPath${FILENAME_SEPARATOR}${bookCode}";
134
135     print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "${bookXML}" -XSL "${xhtmlXSL}" -OUT "${bookPath}.htm" -PARAM use-illustrators "$rulesHash{'use-illustrators'}"};
136     print qx{$ZIP -q $bookPath.zip $bookPath.htm};
137 }
138
139 print "Success\n" if $verbose;