9 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
11 my $PROGRAM_NAME = 'gbtoxhtml';
12 my $USAGE = "$PROGRAM_NAME [options] book-code\n\t--meta=[metadata file]\n\t--xml=[book XML]\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';
20 my $ZIP = '/usr/bin/zip';
21 my $BZIP2 = '/bin/bzip2';
22 my $JAVA = '/usr/bin/java';
23 my $XALAN_JAR = '/usr/share/java/xalan2.jar';
25 my $CHMOD = '/bin/chmod';
27 # Check that all the binaries are were want them
30 push @BINARIES, ($RXP, $CP, $MV, $TAR, $ZIP, $BZIP2, $JAVA, $XALAN_JAR, $RM, $CHMOD);
34 die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
43 my $xhtmlXSL = 'common/xsl/xhtml.xsl';
48 while( $#ARGV > -1 ) {
49 my $cmdLineItem = shift @ARGV;
50 if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
53 elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
56 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
59 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
62 elsif( $cmdLineItem =~ /^--verbose/ ) {
66 $bookCode = $cmdLineItem;
70 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
71 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
72 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
73 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
75 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
76 open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
78 else { die qq{Improper metadata file ($metaFile)\n}; }
81 while( my $line = <META> ) {
82 $meta .= $line if $line !~ /^[[:space:]]*#/;
87 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
91 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
94 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
96 foreach my $rule (@rules) {
97 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
98 $rulesHash{ $1 } = $2;
101 die "Unrecognized rule syntax:\n$rule\n";
105 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
107 unless( -e $bookXML && -f $bookXML && -r $bookXML ) {
108 die "XML does not exist or is not readable\n";
111 system( $RXP, '-Vs', $bookXML ) == 0
112 or die "XML validation failed\n";
115 warn "XML Validator ($RXP) not installed - validate before publication\n";
118 unless( defined $rulesHash{'language'} ) { die "Metadata file leaves language unspecified\n"; }
119 unless( defined $rulesHash{'book-series'} ) { die "Metadata file leaves book series unspecified\n"; }
120 unless( defined $rulesHash{'images'} ) { die "Metadata file leaves image directories unspecified\n"; }
121 unless( defined $rulesHash{'csst'} ) { die "Metadata file leaves CSS templates unspecified\n"; }
123 my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'xhtml' . $FILENAME_SEPARATOR . $rulesHash{'book-series'} .$FILENAME_SEPARATOR . $bookCode;
124 unless( -e $outPath && -d $outPath ) {
125 my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
127 for( my $i = 0; $i <= $#dirs; ++$i ) {
128 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
129 if( -e $dirPath && ! -d $dirPath ) { 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}";
140 print qx{$RM ${bookPath}*} if -e $bookPath."/toc.htm";
141 print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "$bookXML" -XSL "$xhtmlXSL" -OUT "${bookPath}foo.xml" -PARAM background-color "$rulesHash{'background-color'}" -PARAM text-color "$rulesHash{'text-color'}" -PARAM link-color "$rulesHash{'link-color'}" -PARAM use-illustrators "$rulesHash{'use-illustrators'}" -PARAM language "$rulesHash{'language'}"};
142 print qx{$RM ${bookPath}foo.xml};
144 foreach my $cssTemplate (split( /:/, $rulesHash{'csst'} )) {
145 $cssTemplate =~ m/([^${FILENAME_SEPARATOR}]+)t$/;
146 my $templateFilename = $1;
147 open( TEMPLATE, '<', $cssTemplate ) or die "Unable to open CSS template file ($cssTemplate): $!\n";
148 open( STYLESHEET, '>', "$bookPath${templateFilename}" ) or die "Unable to open stylesheet file ($bookPath${templateFilename}) for writing: $!\n";
149 while( my $templateLine = <TEMPLATE> ) {
150 while( $templateLine =~ /%%([^%[:space:]]+)%%/ ) {
152 $templateLine =~ s/%%${name}%%/$rulesHash{$name}/g;
154 print STYLESHEET $templateLine;
160 foreach my $imagePath (split( /:/, $rulesHash{'images'} )) {
161 unless( -e $imagePath && -d $imagePath ) {
162 die "Image path ($imagePath) does not exist or is not a directory\n";
164 print qx{$CP $imagePath${FILENAME_SEPARATOR}* $bookPath};
167 #print qx{$TAR cf ${bookCode}.tar ${bookPath}*};
168 print qx{$ZIP -8 -q ${bookCode}.zip ${bookPath}*};
169 #print qx{$BZIP2 -9 ${bookCode}.tar};
170 print qx{$MV ${bookCode}* $bookPath};
173 print "Success\n" if $verbose;