5 ######################################################################
9 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
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";
14 my $FILENAME_SEPARATOR = '/';
16 my $RXP = '/usr/bin/rxp';
17 unless( -e $RXP && -x $RXP ) {
19 $RXP = '/usr/local/bin/rxp';
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 ) {
28 $XALAN_JAR = '/usr/share/java/xalan2.jar';
31 # Check that all the binaries are were want them
34 push @BINARIES, ($RXP, $ZIP, $JAVA, $XALAN_JAR);
38 die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
47 my $xhtmlXSL = 'common/xsl/xhtml-simple.xsl';
52 while( $#ARGV > -1 ) {
53 my $cmdLineItem = shift @ARGV;
54 if( $cmdLineItem =~ /^--xml=(.+)$/ ) {
57 elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
60 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
63 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
66 elsif( $cmdLineItem =~ /^--verbose/ ) {
70 $bookCode = $cmdLineItem;
74 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
75 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
76 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
77 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
79 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
80 open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
82 else { die qq{Improper metadata file ($metaFile)\n}; }
85 while( my $line = <META> ) {
86 $meta .= $line if $line !~ /^[[:space:]]*#/;
91 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
95 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
98 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
100 foreach my $rule (@rules) {
101 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
102 $rulesHash{ $1 } = $2;
105 die "Unrecognized rule syntax:\n$rule\n";
109 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
111 if( -e $bookXML && -f $bookXML && -r $bookXML ) {
112 system( $RXP, '-Vs', $bookXML ) == 0 or die( "XML validation failed\n" );
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"; }
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 );
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" );
129 unless( -e $outPath && -d $outPath ) {
130 die "Unknown error creating output directory\n";
133 my $bookPath = "$outPath${FILENAME_SEPARATOR}${bookCode}";
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};
139 print "Success\n" if $verbose;