reorganizing the repository
[project-aon.git] / common / scripts / gbtoxhtml.pl
1 #!/usr/bin/perl -w
2 #
3 # gbtoxhtml.pl
4 #
5 #####
6
7 use strict;
8
9 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
10
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";
13
14 my $FILENAME_SEPARATOR = '/';
15
16 my $RXP        = '/usr/bin/rxp';
17 my $CP         = '/bin/cp';
18 my $MV         = '/bin/mv';
19 my $TAR        = '/bin/tar';
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';
24 my $RM         = '/bin/rm';
25 my $CHMOD      = '/bin/chmod';
26
27 ###
28
29 my $bookCode     = '';
30 my $metaFile     = '';
31 my $bookXML      = '';
32 my $xhtmlXSL     = 'common/xsl/xhtml.xsl';
33 my $language     = 'en';
34
35 my $verbose = 0;
36
37 while( $#ARGV > -1 ) {
38     my $cmdLineItem = shift @ARGV;
39     if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
40         $metaFile = $1;
41     }
42     elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
43         $bookXML = $1;
44     }
45     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
46         $xhtmlXSL = $1;
47     }
48     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
49         $language = $1;
50     }
51     elsif( $cmdLineItem =~ /^--verbose/ ) {
52         $verbose = 1;
53     }
54     else { 
55         $bookCode = $cmdLineItem;
56     }
57 }
58
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"; }
63
64 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
65     open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
66 }
67 else { die qq{Improper metadata file ($metaFile)\n}; }
68
69 my $meta = '';
70 while( my $line = <META> ) {
71     $meta .= $line if $line !~ /^[[:space:]]*#/;
72 }
73 close META;
74
75 my $rulesString = '';
76 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
77     $rulesString = $1;
78 }
79 else {
80     die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
81 }
82
83 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
84 my %rulesHash;
85 foreach my $rule (@rules) {
86     if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
87         $rulesHash{ $1 } = $2;
88     }
89     else {
90         die "Unrecognized rule syntax:\n$rule\n";
91     }
92 }
93
94 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
95     $bookXML = $1;
96     unless( -e $bookXML && -f $bookXML && -r $bookXML ) {
97         die "XML does not exist or is not readable\n";
98     }
99     if( -e $RXP ) {
100             system( $RXP, '-Vs', $bookXML ) == 0
101             or die "XML validation failed\n";
102     }
103     else {
104         warn "XML Validator ($RXP) not installed - validate before publication\n";
105     }
106
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"; }
111
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 );
115         my $dirPath = '';
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" );
121             }
122         }
123     }
124     unless( -e $outPath && -d $outPath ) {
125         die "Unknown error creating output directory\n";
126     }
127
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};
132
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:]]+)%%/ ) {
140                 my $name = $1;
141                 $templateLine =~ s/%%${name}%%/$rulesHash{$name}/g;
142             }
143             print STYLESHEET $templateLine;
144         }
145     }
146     close TEMPLATE;
147     close STYLESHEET;
148
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";
152         }
153         print qx{$CP $imagePath${FILENAME_SEPARATOR}* $bookPath};
154     }
155
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};
160 }
161
162 print "Success\n" if $verbose;