From 74c5762fd3e4d1cf8d9562b35dbf9f90f13d4f48 Mon Sep 17 00:00:00 2001 From: Jonathan Blake Date: Sat, 27 Oct 2012 00:11:58 +0000 Subject: [PATCH] adding scripts to build SVG path map in one step git-svn-id: https://projectaon.org/data/trunk@2150 f6f3e2d7-ff33-0410-aaf5-b4bee2cdac11 --- common/scripts/build-svg.sh | 58 +++++++++++++++ common/scripts/gbtosvg.pl | 144 ++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100755 common/scripts/build-svg.sh create mode 100755 common/scripts/gbtosvg.pl diff --git a/common/scripts/build-svg.sh b/common/scripts/build-svg.sh new file mode 100755 index 0000000..00ca904 --- /dev/null +++ b/common/scripts/build-svg.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# +# Build all the SVG files for Project Aon in a single run +# +# (c) 2011 Javier Fernandez-Sanguino +# +# This script is provided with the same license as that one +# used in the Aon Project + +set -e + +CURDIR=`pwd` +[ -z "$AONDIR" ] && AONDIR="../../" +[ -z "$LANGS" ] && LANGS="en es" +LOGDIR="$CURDIR/logs/" +[ ! -e "$LOGDIR" ] && mkdir $LOGDIR + +if [ -n "$1" ] ; then + BOOKS="$1" +else + BOOKS='*' +fi + +generate_files() { + ls ${AONDIR}/${lang}/xml/$1.xml | + while read file ; do + if [ -r "$file" ]; then + xml=`basename $file | sed -e 's/\.xml//'` + # Look for the XML name in the metadata file + if grep -q ^$xml $METADATA; then + echo -n "Generating SVG for $xml ('$lang' language)..." + LOGFILE=$LOGDIR/$xml.svg.log + cd $AONDIR + set +e + perl common/scripts/gbtosvg.pl --language=$lang $xml >$LOGFILE 2>&1 + if [ $? -ne 0 ]; then + echo " ERROR building SVG file (review $LOGFILE)" + fi + echo "...done" + else + echo "WARN: Do not find metadata for $xml in $METADATA" + fi + else + echo "ERROR: Cannot generate SVG file for $xml (not readable)" + fi + done +} + +for lang in $LANGS; do + METADATA=${AONDIR}/${lang}/.publisher/rules/simple + if [ -e "$METADATA" ] ; then + generate_files "$BOOKS" + else + echo "ERROR: Cannot find the publisher rules at $METADATA for $lang" + fi +done + +exit 0 diff --git a/common/scripts/gbtosvg.pl b/common/scripts/gbtosvg.pl new file mode 100755 index 0000000..f996ccb --- /dev/null +++ b/common/scripts/gbtosvg.pl @@ -0,0 +1,144 @@ +#!/usr/bin/env perl -w +# +# gbtosvg.pl + +use strict; + +delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode + +my $PROGRAM_NAME = 'gbtodot'; +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"; + +my $FILENAME_SEPARATOR = '/'; + +my $RXP = '/usr/bin/rxp'; +my $JAVA = '/usr/bin/java'; +my $XALAN_JAR = '/usr/share/java/xalan2.jar'; +my $DOT = '/usr/local/bin/dot'; + +### + +my $bookCode = ''; +my $metaFile = ''; +my $bookXML = ''; +my $dotXSL = 'common/xsl/dot.xsl'; +my $language = 'en'; +my $baseURI = ''; + +my $verbose = 0; + +while( $#ARGV > -1 ) { + my $cmdLineItem = shift @ARGV; + if( $cmdLineItem =~ /^--meta=(.+)$/ ) { + $metaFile = $1; + } + elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) { + $bookXML = $1; + } + elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) { + $dotXSL = $1; + } + elsif( $cmdLineItem =~ /^--base-uri=(.+)$/ ) { + $baseURI = $1; + } + elsif( $cmdLineItem =~ /^--language=(.+)$/ ) { + $language = $1; + } + elsif( $cmdLineItem =~ /^--verbose/ ) { + $verbose = 1; + } + else { + $bookCode = $cmdLineItem; + } +} + +if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; } +if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; } +if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; } + +if( -e $metaFile && -f $metaFile && -r $metaFile ) { + open( META, '<', $metaFile ) + or die qq{Unable to open metadata file ($metaFile): $!\n}; +} +else { + die qq{Improper metadata file ($metaFile)\n}; +} + +my $meta = ''; +while( my $line = ) { + $meta .= $line if $line !~ /^[[:space:]]*#/; +} +close META; + +my $rulesString = ''; +if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) { + $rulesString = $1; +} +else { + die "Book code ($bookCode) not found in metadata file or invalid file syntax\n"; +} + +my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString ); +my %rulesHash; +foreach my $rule (@rules) { + if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) { + $rulesHash{ $1 } = $2; + } + else { + die "Unrecognized rule syntax:\n$rule\n"; + } +} + +if( $bookXML =~ m{^([-\w\@./]+)$} ) { + $bookXML = $1; + if( -e $bookXML && -f $bookXML && -r $bookXML ) { + system( $RXP, '-Vs', $bookXML ) == 0 + or die( "XML validation failed\n" ); + } + + unless( defined $rulesHash{'book-series'} ) { + die "Metadata file leaves book series unspecified\n"; + } + unless( defined $rulesHash{'language'} ) { + die "Metadata file leaves language unspecified\n"; + } + + my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'dot' . $FILENAME_SEPARATOR . $rulesHash{'book-series'}; + &make_path( $outPath ); + unless( -e $outPath && -d $outPath ) { + die "Unknown error creating output directory\n"; + } + + $baseURI = "http://www.projectaon.org/$rulesHash{'language'}/xhtml/$rulesHash{'book-series'}/$bookCode/" if( $baseURI eq '' ); + my $dotPath = "$outPath$FILENAME_SEPARATOR$bookCode.dot"; + 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'}"}; + + my $svgPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'svg' . $FILENAME_SEPARATOR . $rulesHash{'book-series'}; + &make_path( $svgPath ); + unless( -e $svgPath && -d $svgPath ) { + die "Unknown error creating output directory\n"; + } + $svgPath .= "$FILENAME_SEPARATOR$bookCode.svgz"; + print qx{$DOT -Tsvgz -o $svgPath $dotPath}; +} + +print "Success\n" if $verbose; + +### + +sub make_path { + my ( $path ) = ( @_ ); + + my @dirs = split ( /$FILENAME_SEPARATOR/, $path ); + my $dirPath = ''; + for( my $i = 0; $i <= $#dirs; ++$i ) { + $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR; + if( -e $dirPath && ! -d $dirPath ) { + die "Output directory name exists and is not a directory\n"; + } + unless( -e $dirPath ) { + mkdir $dirPath + or die( "Unable to create output directory ($path): $!\n" ); + } + } +} -- 2.34.1