Hi,

Jose, here is the config_xml script updated, that detects the available
and needed things by using the catalog mechanisms. The constraint is
that it needs an extra java archive, the advantage is that this method
is 100% xml :-). The script is expected to be in the db2lyx package. I
attach the db2lyx doc updated, that explains how to use it

It is still in perl, since I'm not sure it's so easy to do the same
thing in bash. Besides, it can always be translated later (maybe in
python ;-), when the configuration steps are fixed.

Please give me your comments.

BG

#!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if $running_under_some_shell;

use File::Basename;
use Cwd;

BEGIN {
%xslt_command = (
  'xt.jar'    => 'com.jclark.xsl.sax.Driver $i $sheet > $o' ,
  'xalan.jar' => 'org.apache.xalan.xslt.Process -in $i -xsl $sheet -out $o' ,
);

%public_id = (
  'base-nwalsh'   => "-//DB2LyX//Norman Walsh's XSL Stylesheet Directory//EN",
  'base-mmlents'  => "-//DB2LyX//MathML ISO Entities Directory//EN",
  'dbxdtd-4.1.2'  => "-//OASIS//DTD DocBook XML V4.1.2//EN",
  'dbxdtd-4.0'    => "-//OASIS//DTD DocBook XML V4.0//EN"
);

@xmldtd_version = (
  "4.1.2",
  "4.0"
);
}

#
# Resolves the file paths with the catalog jar, with the SGML_CATALOG_FILES
# variable that lists the catalogs available.
#
sub resolve
{
  local($pubid_name) = $_[0];

  if (not(exists($public_id{$pubid_name}))) {
    return "";
  }

  $pubid = $public_id{$pubid_name};


  $cmd = "java -Dxml.catalog.files=$user_catalog\$SGML_CATALOG_FILES";
  $cmd = "$cmd catalog -s public \"$pubid\" | grep Resolved";

  $path = `$cmd`;

  $path =~ s/Resolved: *//g;
  $path =~ s/file://g;
  $path =~ s/^null//g;
  chomp $path;

#  print "path resolved = $path\n";
  return $path;
}

#
# Changes the command template with the specified XML file, stylesheet,
# and output.
#
sub set_xslt
{
  local($command) = $_[0];
  local($xml) = $_[1];
  local($xsl) = $_[2];
  local($out) = $_[3];

  $command =~ s/\$i/$xml/;
  $command =~ s/\$o/$out/;
  $command =~ s/\$sheet/$xsl/;
  
  return $command;
}

#
# Run the XSLT, for a specified XML file, stylesheet, etc.
#
sub run_xslt
{
  local($command) = $_[0];
  local($xml) = $_[1];
  local($xsl) = $_[2];
  local($out) = $_[3];

  $command = set_xslt($command, $xml, $xsl, $out);

  print "$command\n";
  system($command);
}

#
# Updates the lyxrc file with the XML information.
#
sub add_xml_info
{
  local($lyxrc) = $_[0];
  local($xml_to_lyx_cmd) = $_[1];
  local($sgml_to_xml) = $_[2];

  open(IN, "<$lyxrc") || die "Cannot open $lyxrc\n";
  open(OUT, ">$lyxrc.new") || die "Cannot open $lyxrc.new\n";

  my $done_format = 0;
  my $done_conv = 0;
  my $done_stox = 0;

  # The 

  while (<IN>) {
    # Copy of the original file, unless it is the previous XML config.
    if (not(/\\Format xml/ || 
            /\\converter xml/ ||
            /\\converter docbook xml/)) {
      print OUT ;
    }
    if (/\\Format docbook/ && $done_format == 0) {
      # Add the XML format
      print OUT "\\Format xml xml XML \"\"\n";
      $done_format++;
    }
    if (/\\converter docbook/ && $done_conv == 0) {
      # Add the XML->LyX converter
      print OUT "\\converter xml lyx \"$xml_to_lyx_cmd\" \"\"\n";
      $done_conv++;
    }
    if (/\\converter docbook/ && $done_stox == 0) {
      # Add the SGML->XML converter
      print OUT "\\converter docbook xml \"$sgml_to_xml\" \"\"\n";
      $done_stox++;
    }
  }

  close(IN);
  close(OUT);
  system("mv $lyxrc.new $lyxrc");
}

#
# Patch the configure script, so that reconfiguring will call this script too.
#
sub patch_configure
{
  local($xml_config) = $_[0];

  $ispatched = `grep $xml_config $base_db2lyx/../configure`;

  if ($ispatched eq '') {
    system("cat <<eof >> $base_db2lyx/../configure
 
# Specific XML config
if test -d \\\$srcdir/db2lyx ; then
  \\\$srcdir/db2lyx/$xml_config
fi
eof");
  }
}

# Get the DB2LyX path
$base_db2lyx = dirname($0);

# We want an absolute path
$pwd = getcwd();
if (not($base_db2lyx =~ /^\//)) {
  $base_db2lyx = "$pwd/$base_db2lyx";
}

# Get the script name, and include it in the configure script
patch_configure(basename($0));

#
# We look for the available XSLT, hoping that they are declared in the
# java CLASSPATH. Besides, catalog.jar is expected too.
#
$classpath = $ENV{'CLASSPATH'};

print "+checking for catalog.jar... ";
if (not($classpath =~ /catalog.jar/)) {
  print "no\n";
  print "*** Cannot configure: missing catalog.jar in CLASSPATH\n";
  exit 1;
} else {
  print "yes\n";
}

# Let's try the user directory
$user_catalog="$pwd/db2lyx/catalog";
print "+checking for $user_catalog... ";
if (-f "$user_catalog") {
  print "yes\n";
  $user_catalog .= ":";
} else {
  print "no\n";
  $user_catalog = "";
}

@jar = split(':', $classpath);

foreach $file (@jar) {
  $f = basename($file);
  if (exists $xslt_command{$f}) {
    @xslt = (@xslt, $f);
  }
}

if ($#xslt > 0) {
  print "+checking for XSLT... (@xslt)\n";

  # We take the first available XSLT
  $xslt_cmd = "java $xslt_command{$xslt[0]}";

  # File path resolution
  $base_nwalsh = resolve("base-nwalsh");
  $base_mmlents = resolve("base-mmlents");

  # Check that it's valid
  if ($base_nwalsh eq '' || $base_mmlents eq '') {
    print "*** Cannot configure DB2LyX:\n";
    if ($base_nwalsh eq '') {
      print "*** Unresolved ID $public_id{'base-nwalsh'}\n";
    }
    if ($base_mmlents eq '') {
      print "*** Unresolved ID $public_id{'base-mmlents'}\n";
    }
    print "*** Check your catalogs\n";
    exit 1;
  }

  # Now, configure the package
  $params = "base-dbcommon=$base_nwalsh/common base-mmlents=$base_mmlents";

  run_xslt($xslt_cmd,
           "$base_db2lyx/template/xslpath.xml",
           "$base_db2lyx/template/xslpath.xsl $params",
           "$base_db2lyx/xslpath.ent");

  # Set the converter command
  $xml_to_lyx_cmd = set_xslt($xslt_cmd,
                             "\$\$i",
                             "$base_db2lyx/docbook.xsl",
                             "\$\$o");
}
else {
  print "+checking for XSLT... no\n";
  $xml_to_lyx_cmd = 'none';
}

#
# Prepare the SGML->XML script
# The principle is to find the Docbook XML DTD version available, according to
# a preference list, and then configure the SGML->XML script with it.
#

$xmldtd_path = '';

for ($i = 0; $i <= $#xmldtd_version && ($xmldtd_path eq ''); $i++) {
  $version = $xmldtd_version[$i];
  $xmldtd_path = resolve("dbxdtd-$version");
  print "+checking for DTD DocBook XML V$version... ";
  print "no\n" if ($xmldtd_path eq '') ;
}

if ($xmldtd_path eq '') {
  $sgml_to_xml = 'none';
} else {
  print "yes\n";
  $sgml_to_xml = "$base_db2lyx/scripts/sgml2xml.pl $xmldtd_path \$\$i";
}

# print "$sgml_to_xml\n";

#
# Patch the lyxrc.default file, in the user lyx dir
#
if (not(-f "lyxrc.defaults")) {
  system("cp $base_db2lyx/../lyxrc.defaults lyxrc.defaults");
}

add_xml_info("lyxrc.defaults", $xml_to_lyx_cmd, $sgml_to_xml);

manual.lyx.gz

Reply via email to