adding scripts to build SVG path map in one step
[project-aon.git] / common / scripts / gbtosvg.pl
1 #!/usr/bin/env perl -w
2 #
3 # gbtosvg.pl
4
5 use strict;
6
7 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
8
9 my $PROGRAM_NAME    = 'gbtodot';
10 my $USAGE           = "$PROGRAM_NAME [options] book-code\n\t--meta=[metadata file]\n\t--xml=[book XML]\n\t--xsl=[XSL transformation]\n\t--baseURI=[URI of linked XHTML book]\n\t--language=[language area of input data (output determined by meta file)]\n\t--verbose\n";
11
12 my $FILENAME_SEPARATOR = '/';
13
14 my $RXP        = '/usr/bin/rxp';
15 my $JAVA       = '/usr/bin/java';
16 my $XALAN_JAR  = '/usr/share/java/xalan2.jar';
17 my $DOT        = '/usr/local/bin/dot';
18
19 ###
20
21 my $bookCode     = '';
22 my $metaFile     = '';
23 my $bookXML      = '';
24 my $dotXSL       = 'common/xsl/dot.xsl';
25 my $language     = 'en';
26 my $baseURI      = '';
27
28 my $verbose = 0;
29
30 while( $#ARGV > -1 ) {
31     my $cmdLineItem = shift @ARGV;
32     if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
33         $metaFile = $1;
34     }
35     elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
36         $bookXML = $1;
37     }
38     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
39         $dotXSL = $1;
40     }
41     elsif( $cmdLineItem =~ /^--base-uri=(.+)$/ ) {
42         $baseURI = $1;
43     }
44     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
45         $language = $1;
46     }
47     elsif( $cmdLineItem =~ /^--verbose/ ) {
48         $verbose = 1;
49     }
50     else { 
51         $bookCode = $cmdLineItem;
52     }
53 }
54
55 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
56 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
57 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
58
59 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
60     open( META, '<', $metaFile ) 
61         or die qq{Unable to open metadata file ($metaFile): $!\n};
62 }
63 else { 
64     die qq{Improper metadata file ($metaFile)\n}; 
65 }
66
67 my $meta = '';
68 while( my $line = <META> ) {
69     $meta .= $line if $line !~ /^[[:space:]]*#/;
70 }
71 close META;
72
73 my $rulesString = '';
74 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
75     $rulesString = $1;
76 }
77 else {
78     die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
79 }
80
81 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
82 my %rulesHash;
83 foreach my $rule (@rules) {
84     if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
85         $rulesHash{ $1 } = $2;
86     }
87     else {
88         die "Unrecognized rule syntax:\n$rule\n";
89     }
90 }
91
92 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
93     $bookXML = $1;
94     if( -e $bookXML && -f $bookXML && -r $bookXML ) {
95         system( $RXP, '-Vs', $bookXML ) == 0 
96             or die( "XML validation failed\n" );
97     }
98
99     unless( defined $rulesHash{'book-series'} ) { 
100         die "Metadata file leaves book series unspecified\n";
101     }
102     unless( defined $rulesHash{'language'} ) { 
103         die "Metadata file leaves language unspecified\n";
104     }
105
106     my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'dot' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
107     &make_path( $outPath );
108     unless( -e $outPath && -d $outPath ) {
109             die "Unknown error creating output directory\n";
110     }
111
112     $baseURI = "http://www.projectaon.org/$rulesHash{'language'}/xhtml/$rulesHash{'book-series'}/$bookCode/" if( $baseURI eq '' );
113     my $dotPath = "$outPath$FILENAME_SEPARATOR$bookCode.dot";
114     print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "$bookXML" -XSL "$dotXSL" -OUT "$dotPath" -PARAM base-URI "$baseURI" -PARAM language "$rulesHash{'language'}"};
115
116     my $svgPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'svg' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
117     &make_path( $svgPath );
118     unless( -e $svgPath && -d $svgPath ) {
119             die "Unknown error creating output directory\n";
120     }
121     $svgPath .= "$FILENAME_SEPARATOR$bookCode.svgz";
122     print qx{$DOT -Tsvgz -o $svgPath $dotPath};
123 }
124
125 print "Success\n" if $verbose;
126
127 ###
128
129 sub make_path {
130     my ( $path ) = ( @_ );
131
132         my @dirs = split ( /$FILENAME_SEPARATOR/, $path );
133         my $dirPath = '';
134         for( my $i = 0; $i <= $#dirs; ++$i ) {
135             $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
136             if( -e $dirPath && ! -d $dirPath ) { 
137             die "Output directory name exists and is not a directory\n";
138         }
139             unless( -e $dirPath ) {
140             mkdir $dirPath 
141                 or die( "Unable to create output directory ($path): $!\n" );
142             }
143         }
144 }