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';
32 my $xhtmlXSL = 'common/xsl/xhtml.xsl';
37 while( $#ARGV > -1 ) {
38 my $cmdLineItem = shift @ARGV;
39 if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
42 elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
45 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
48 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
51 elsif( $cmdLineItem =~ /^--verbose/ ) {
55 $bookCode = $cmdLineItem;
59 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
60 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
61 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
62 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
64 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
65 open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
67 else { die qq{Improper metadata file ($metaFile)\n}; }
70 while( my $line = <META> ) {
71 $meta .= $line if $line !~ /^[[:space:]]*#/;
76 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
80 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
83 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
85 foreach my $rule (@rules) {
86 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
87 $rulesHash{ $1 } = $2;
90 die "Unrecognized rule syntax:\n$rule\n";
94 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
96 unless( -e $bookXML && -f $bookXML && -r $bookXML ) {
97 die "XML does not exist or is not readable\n";
100 system( $RXP, '-Vs', $bookXML ) == 0
101 or die "XML validation failed\n";
104 warn "XML Validator ($RXP) not installed - validate before publication\n";
107 unless( defined $rulesHash{'language'} ) { die "Metadata file leaves language unspecified\n"; }
108 unless( defined $rulesHash{'book-series'} ) { die "Metadata file leaves book series unspecified\n"; }
109 unless( defined $rulesHash{'images'} ) { die "Metadata file leaves image directories unspecified\n"; }
110 unless( defined $rulesHash{'csst'} ) { die "Metadata file leaves CSS templates unspecified\n"; }
112 my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'xhtml' . $FILENAME_SEPARATOR . $rulesHash{'book-series'} .$FILENAME_SEPARATOR . $bookCode;
113 unless( -e $outPath && -d $outPath ) {
114 my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
116 for( my $i = 0; $i <= $#dirs; ++$i ) {
117 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
118 if( -e $dirPath && ! -d $dirPath ) { 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}";
129 print qx{$RM ${bookPath}*} if -e $bookPath."/toc.htm";
130 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'}"};
131 print qx{$RM ${bookPath}foo.xml};
133 foreach my $cssTemplate (split( /:/, $rulesHash{'csst'} )) {
134 $cssTemplate =~ m/([^${FILENAME_SEPARATOR}]+)t$/;
135 my $templateFilename = $1;
136 open( TEMPLATE, '<', $cssTemplate ) or die "Unable to open CSS template file ($cssTemplate): $!\n";
137 open( STYLESHEET, '>', "$bookPath${templateFilename}" ) or die "Unable to open stylesheet file ($bookPath${templateFilename}) for writing: $!\n";
138 while( my $templateLine = <TEMPLATE> ) {
139 while( $templateLine =~ /%%([^%[:space:]]+)%%/ ) {
141 $templateLine =~ s/%%${name}%%/$rulesHash{$name}/g;
143 print STYLESHEET $templateLine;
149 foreach my $imagePath (split( /:/, $rulesHash{'images'} )) {
150 unless( -e $imagePath && -d $imagePath ) {
151 die "Image path ($imagePath) does not exist or is not a directory\n";
153 print qx{$CP $imagePath${FILENAME_SEPARATOR}* $bookPath};
156 #print qx{$TAR cf ${bookCode}.tar ${bookPath}*};
157 print qx{$ZIP -8 -q ${bookCode}.zip ${bookPath}*};
158 #print qx{$BZIP2 -9 ${bookCode}.tar};
159 print qx{$MV ${bookCode}* $bookPath};
162 print "Success\n" if $verbose;