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';
29 my $xhtmlXSL = 'common/xsl/xhtml-less-simple.xsl';
34 while( $#ARGV > -1 ) {
35 my $cmdLineItem = shift @ARGV;
36 if( $cmdLineItem =~ /^--xml=(.+)$/ ) {
39 elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
42 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
45 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
48 elsif( $cmdLineItem =~ /^--verbose/ ) {
52 $bookCode = $cmdLineItem;
56 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
57 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/simple"; }
58 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
59 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
61 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
62 open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
64 else { die qq{Improper metadata file ($metaFile)\n}; }
67 while( my $line = <META> ) {
68 $meta .= $line if $line !~ /^[[:space:]]*#/;
73 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
77 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
80 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
82 foreach my $rule (@rules) {
83 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
84 $rulesHash{ $1 } = $2;
87 die "Unrecognized rule syntax:\n$rule\n";
91 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
93 if( -e $bookXML && -f $bookXML && -r $bookXML ) {
94 system( $RXP, '-Vs', $bookXML ) == 0
95 or die( "XML validation failed\n" );
97 unless( defined $rulesHash{'language'} ) {
98 die "Metadata file leaves language unspecified\n";
100 unless( defined $rulesHash{'book-series'} ) {
101 die "Metadata file leaves book series unspecified\n";
103 unless( defined $rulesHash{'images'} ) {
104 die "Metadata file leaves image directories unspecified\n";
107 my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR .
108 'xhtml-less-simple' . $FILENAME_SEPARATOR .
109 $rulesHash{'book-series'} . $FILENAME_SEPARATOR .
111 unless( -e $outPath && -d $outPath ) {
112 my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
114 for( my $i = 0; $i <= $#dirs; ++$i ) {
115 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
116 if( -e $dirPath && ! -d $dirPath ) {
117 die "Output directory name exists and is not a directory\n";
119 unless( -e $dirPath ) {
120 mkdir $dirPath or die( "Unable to create output directory ($outPath): $!\n" );
124 unless( -e $outPath && -d $outPath ) {
125 die "Unknown error creating output directory\n";
128 my $bookPath = "$outPath${FILENAME_SEPARATOR}";
130 print qx{$RM ${bookPath}*} if -e $bookPath."/title.htm";
131 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'}"};
133 foreach my $imagePath (split( /:/, $rulesHash{'images'} )) {
134 unless( -e $imagePath && -d $imagePath ) {
135 die "Image path ($imagePath) does not exist or is not a directory\n";
137 print qx{$CP $imagePath${FILENAME_SEPARATOR}* $bookPath};
140 print qx{$ZIP -8 -q ${bookCode}.zip ${bookPath}*};
141 print qx{$MV ${bookCode}* $bookPath};
144 print "Success\n" if $verbose;