3 # gbtoxhtml-less-simple.xsl
5 ######################################################################
9 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
11 my $PROGRAM_NAME = 'gbtoxhtml-less-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";
14 my $FILENAME_SEPARATOR = '/';
19 my $RXP = '/usr/bin/rxp';
20 my $ZIP = '/usr/bin/zip';
21 my $JAVA = '/usr/bin/java';
22 my $XALAN_JAR = '/usr/share/java/xalan2.jar';
24 # Check that all the binaries are were want them
27 push @BINARIES, ($RXP, $CP, $MV, $ZIP, $JAVA, $XALAN_JAR, $RM);
31 die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
40 my $xhtmlXSL = 'common/xsl/xhtml-less-simple.xsl';
45 while( $#ARGV > -1 ) {
46 my $cmdLineItem = shift @ARGV;
47 if( $cmdLineItem =~ /^--xml=(.+)$/ ) {
50 elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
53 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
56 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
59 elsif( $cmdLineItem =~ /^--verbose/ ) {
63 $bookCode = $cmdLineItem;
67 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
68 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/simple"; }
69 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
70 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
72 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
73 open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
75 else { die qq{Improper metadata file ($metaFile)\n}; }
78 while( my $line = <META> ) {
79 $meta .= $line if $line !~ /^[[:space:]]*#/;
84 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
88 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
91 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
93 foreach my $rule (@rules) {
94 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
95 $rulesHash{ $1 } = $2;
98 die "Unrecognized rule syntax:\n$rule\n";
102 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
104 if( -e $bookXML && -f $bookXML && -r $bookXML ) {
105 system( $RXP, '-Vs', $bookXML ) == 0
106 or die( "XML validation failed\n" );
108 unless( defined $rulesHash{'language'} ) {
109 die "Metadata file leaves language unspecified\n";
111 unless( defined $rulesHash{'book-series'} ) {
112 die "Metadata file leaves book series unspecified\n";
114 unless( defined $rulesHash{'images'} ) {
115 die "Metadata file leaves image directories unspecified\n";
118 my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR .
119 'xhtml-less-simple' . $FILENAME_SEPARATOR .
120 $rulesHash{'book-series'} . $FILENAME_SEPARATOR .
122 unless( -e $outPath && -d $outPath ) {
123 my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
125 for( my $i = 0; $i <= $#dirs; ++$i ) {
126 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
127 if( -e $dirPath && ! -d $dirPath ) {
128 die "Output directory name exists and is not a directory\n";
130 unless( -e $dirPath ) {
131 mkdir $dirPath or die( "Unable to create output directory ($outPath): $!\n" );
135 unless( -e $outPath && -d $outPath ) {
136 die "Unknown error creating output directory\n";
139 my $bookPath = "$outPath${FILENAME_SEPARATOR}";
141 print qx{$RM ${bookPath}*} if -e $bookPath."/title.htm";
142 print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "${bookXML}" -XSL "${xhtmlXSL}" -OUT "${bookPath}title.htm" -PARAM use-illustrators "$rulesHash{'use-illustrators'}"};
144 foreach my $imagePath (split( /:/, $rulesHash{'images'} )) {
145 unless( -e $imagePath && -d $imagePath ) {
146 die "Image path ($imagePath) does not exist or is not a directory\n";
148 print qx{$CP $imagePath${FILENAME_SEPARATOR}* $bookPath};
151 print qx{$ZIP -8 -q ${bookCode}.zip ${bookPath}*};
152 print qx{$MV ${bookCode}* $bookPath};
155 print "Success\n" if $verbose;