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 -q';
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';
36 my $xhtmlXSL = 'common/xsl/xhtml-simple.xsl';
41 while( $#ARGV > -1 ) {
42 my $cmdLineItem = shift @ARGV;
43 if( $cmdLineItem =~ /^--xml=(.+)$/ ) {
46 elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
49 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
52 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
55 elsif( $cmdLineItem =~ /^--verbose/ ) {
59 $bookCode = $cmdLineItem;
63 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
64 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
65 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
66 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
68 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
69 open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
71 else { die qq{Improper metadata file ($metaFile)\n}; }
74 while( my $line = <META> ) {
75 $meta .= $line if $line !~ /^[[:space:]]*#/;
80 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
84 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
87 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
89 foreach my $rule (@rules) {
90 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
91 $rulesHash{ $1 } = $2;
94 die "Unrecognized rule syntax:\n$rule\n";
98 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
100 if( -e $bookXML && -f $bookXML && -r $bookXML ) {
101 system( $RXP, '-Vs', $bookXML ) == 0 or die( "XML validation failed\n" );
103 unless( defined $rulesHash{'language'} ) { die "Metadata file leaves language unspecified\n"; }
104 unless( defined $rulesHash{'book-series'} ) { die "Metadata file leaves book series unspecified\n"; }
106 my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'xhtml-simple' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
107 unless( -e $outPath && -d $outPath ) {
108 my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
110 for( my $i = 0; $i <= $#dirs; ++$i ) {
111 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
112 if( -e $dirPath && ! -d $dirPath ) { die "Output directory name exists and is not a directory\n"; }
113 unless( -e $dirPath ) {
114 mkdir $dirPath or die( "Unable to create output directory ($outPath): $!\n" );
118 unless( -e $outPath && -d $outPath ) {
119 die "Unknown error creating output directory\n";
122 my $bookPath = "$outPath${FILENAME_SEPARATOR}${bookCode}";
124 print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "${bookXML}" -XSL "${xhtmlXSL}" -OUT "${bookPath}.htm" -PARAM use-illustrators "$rulesHash{'use-illustrators'}"};
125 print qx{$ZIP $bookPath.zip $bookPath.htm};
128 print "Success\n" if $verbose;