5 # Creates XHTML gamebook from XML source.
10 # Revision 2.0 2006/02/28 23:50:54 jonathan.blake
11 # Extensive overhaul and changed command line interface
13 # Revision 1.2 2005/10/13 00:48:47 angantyr
14 # Put Paul Bonner as illustrator of the GS books.
16 # Revision 1.1.1.1 2005/04/26 04:47:33 jonathan.blake
19 # Revision 1.8 2003/07/14 17:20:09 jblake
20 # Modified for Xalan 2.5.1, added support for AONPATH
21 # environment variable, commented out RXP validation.
23 # Revision 1.7 2002/11/10 07:52:15 jblake
26 # Revision 1.6 2002/11/10 03:57:50 jblake
27 # Added some missing semicolons.
29 # Revision 1.5 2002/11/07 18:43:14 jblake
30 # Added books (02fotw, 13tplor, and 15tdc) and added an illustrator to the list.
32 # Revision 1.4 2002/10/23 18:58:34 jblake
33 # Added Flight from the Dark to the valid book list, and made
34 # a change to the working directory in order to work with
37 # Revision 1.3 2002/10/20 05:47:50 jblake
38 # Added Highway Holocaust to the book list.
40 # Revision 1.2 2002/10/18 15:42:25 jblake
41 # Added Grey Star the Wizard to the book list.
43 # Revision 1.1 2002/10/18 15:38:41 jblake
47 # * make the transformation more generic by using an xslt-params rule
53 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
55 my $PROGRAM_NAME = 'gbtoxhtml';
56 my $USAGE = "$PROGRAM_NAME [options]\n\t--book=[book code]\n\t--meta=[metadata file]\n\t--xml=[book XML]\n\t--xsl=[XSL transformation]\n";
58 my $FILENAME_SEPARATOR = '/';
60 my $RXP = '/home/projectaon/bin/rxp';
64 my $ZIP = '/usr/bin/zip';
65 my $BZIP2 = '/usr/bin/bzip2';
66 my $JAVA = '/usr/bin/java';
67 my $XALAN_JAR = '/home/projectaon/bin/xalan.jar';
69 my $CHMOD = '/bin/chmod';
80 while( $#ARGV > -1 ) {
81 my $cmdLineItem = shift @ARGV;
82 if( $cmdLineItem =~ /^--book=(.+)$/ ) {
85 elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
88 elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
91 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
94 elsif( $cmdLineItem =~ /^--verbose/ ) {
100 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
101 if( $metaFile eq '' ) { die "Unspecified metadata file\n$USAGE"; }
102 if( $bookXML eq '' ) { die "Unspecified book XML file\n$USAGE"; }
103 if( $xhtmlXSL eq '' ) { die "Unspecified XSL transformation file\n$USAGE"; }
105 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
106 open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
108 else { die qq{Improper metadata file ($metaFile)\n}; }
111 while( my $line = <META> ) {
112 $meta .= $line if $line !~ /^[[:space:]]*#/;
116 my $rulesString = '';
117 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
121 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
124 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
126 foreach my $rule (@rules) {
127 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
128 $rulesHash{ $1 } = $2;
131 die "Unrecognized rule syntax:\n$rule\n";
135 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
137 if( -e $bookXML && -f $bookXML && -r $bookXML ) {
138 system( $RXP, '-Vs', $bookXML ) == 0 or die( "XML validation failed\n" );
140 unless( defined $rulesHash{'book-path'} ) { die "Metadata file leaves output path unspecified\n"; }
141 unless( -e $rulesHash{'book-path'} && -d $rulesHash{'book-path'} ) {
142 my @dirs = split ( /$FILENAME_SEPARATOR/, $rulesHash{'book-path'} );
144 for( my $i = 0; $i <= $#dirs; ++$i ) {
145 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
146 if( -e $dirPath && ! -d $dirPath ) { die "Output directory name exists and is not a directory\n"; }
147 unless( -e $dirPath ) {
148 mkdir $dirPath or die( "Unable to create output directory ($rulesHash{'book-path'}): $!\n" );
152 unless( -e $rulesHash{'book-path'} && -d $rulesHash{'book-path'} ) {
153 die "Unknown error creating output directory\n";
156 print qx{$RM $rulesHash{'book-path'}$FILENAME_SEPARATOR*};
157 print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "$bookXML" -XSL "$xhtmlXSL" -OUT "$rulesHash{'book-path'}${FILENAME_SEPARATOR}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'}"};
158 print qx{$RM $rulesHash{'book-path'}${FILENAME_SEPARATOR}foo.xml};
160 foreach my $cssTemplate (split( /:/, $rulesHash{'csst'} )) {
161 $cssTemplate =~ m/([^${FILENAME_SEPARATOR}]+)t$/;
162 my $templateFilename = $1;
163 open( TEMPLATE, '<', $cssTemplate ) or die "Unable to open CSS template file ($cssTemplate): $!\n";
164 open( STYLESHEET, '>', "$rulesHash{'book-path'}${FILENAME_SEPARATOR}${templateFilename}" ) or die "Unable to open stylesheet file ($rulesHash{'book-path'}${FILENAME_SEPARATOR}${templateFilename}) for writing: $!\n";
165 while( my $templateLine = <TEMPLATE> ) {
166 while( $templateLine =~ /%%([^%[:space:]]+)%%/ ) {
168 $templateLine =~ s/%%${name}%%/$rulesHash{$name}/g;
170 print STYLESHEET $templateLine;
176 foreach my $imagePath (split( /:/, $rulesHash{'images'} )) {
177 unless( -e $imagePath && -d $imagePath ) {
178 die "Image path ($imagePath) does not exist or is not a directory\n";
180 print qx{$CP $imagePath${FILENAME_SEPARATOR}* $rulesHash{'book-path'}};
183 print qx{$TAR cf ${bookCode}.tar $rulesHash{'book-path'}${FILENAME_SEPARATOR}*};
184 print qx{$ZIP -8 -q ${bookCode}.zip $rulesHash{'book-path'}${FILENAME_SEPARATOR}*};
185 print qx{$BZIP2 -9 ${bookCode}.tar};
186 print qx{$MV ${bookCode}* $rulesHash{'book-path'}${FILENAME_SEPARATOR}};
189 print "Success\n" if $verbose;