altered the script to list all formats in PmWiki page-text variable form
[project-aon.git] / common / scripts / gbtoxhtml-less-simple.pl
1 #!/usr/bin/perl -w
2 #
3 # gbtoxhtml-less-simple.xsl
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-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";
13
14 my $FILENAME_SEPARATOR = '/';
15
16 my $CP         = '/bin/cp';
17 my $MV         = '/bin/mv';
18 my $RM         = '/bin/rm';
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';
23
24 ###
25
26 my $bookCode     = '';
27 my $bookXML      = '';
28 my $metaFile     = '';
29 my $xhtmlXSL     = 'common/xsl/xhtml-less-simple.xsl';
30 my $language     = 'en';
31
32 my $verbose = 0;
33
34 while( $#ARGV > -1 ) {
35     my $cmdLineItem = shift @ARGV;
36     if( $cmdLineItem =~ /^--xml=(.+)$/ ) {
37         $bookXML = $1;
38     }
39     elsif( $cmdLineItem =~ /^--meta=(.+)$/ ) {
40         $metaFile = $1;
41     }
42     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
43         $xhtmlXSL = $1;
44     }
45     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
46         $language = $1;
47     }
48     elsif( $cmdLineItem =~ /^--verbose/ ) {
49         $verbose = 1;
50     }
51     else { 
52         $bookCode = $cmdLineItem;
53     }
54 }
55
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"; }
60
61 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
62     open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
63 }
64 else { die qq{Improper metadata file ($metaFile)\n}; }
65
66 my $meta = '';
67 while( my $line = <META> ) {
68     $meta .= $line if $line !~ /^[[:space:]]*#/;
69 }
70 close META;
71
72 my $rulesString = '';
73 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
74     $rulesString = $1;
75 }
76 else {
77     die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
78 }
79
80 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
81 my %rulesHash;
82 foreach my $rule (@rules) {
83     if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
84         $rulesHash{ $1 } = $2;
85     }
86     else {
87         die "Unrecognized rule syntax:\n$rule\n";
88     }
89 }
90
91 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
92     $bookXML = $1;
93     if( -e $bookXML && -f $bookXML && -r $bookXML ) {
94             system( $RXP, '-Vs', $bookXML ) == 0
95             or die( "XML validation failed\n" );
96     }
97     unless( defined $rulesHash{'language'} ) {
98         die "Metadata file leaves language unspecified\n";
99     }
100     unless( defined $rulesHash{'book-series'} ) {
101         die "Metadata file leaves book series unspecified\n";
102     }
103     unless( defined $rulesHash{'images'} ) { 
104         die "Metadata file leaves image directories unspecified\n";
105     }
106
107     my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR .
108                  'xhtml-less-simple' . $FILENAME_SEPARATOR .
109                  $rulesHash{'book-series'} . $FILENAME_SEPARATOR .
110                  $bookCode;
111     unless( -e $outPath && -d $outPath ) {
112             my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
113         my $dirPath = '';
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";
118             }
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
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'}"};
132
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";
136             }
137         print qx{$CP $imagePath${FILENAME_SEPARATOR}* $bookPath};
138     }
139
140     print qx{$ZIP -8 -q ${bookCode}.zip ${bookPath}*};
141     print qx{$MV ${bookCode}* $bookPath};
142 }
143
144 print "Success\n" if $verbose;