2017-06-18 10:13:57 +00:00
|
|
|
# -*- mode: perl; -*-
|
|
|
|
package prefTest;
|
|
|
|
|
|
|
|
our(@EXPORT, @ISA);
|
|
|
|
BEGIN {
|
|
|
|
use Exporter ();
|
|
|
|
@ISA = qw(Exporter);
|
|
|
|
@EXPORT = qw(getSubstitutes getConverters applyChanges);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getSubstitutes($$@);
|
|
|
|
sub getConverters($$$);
|
|
|
|
sub getConverter($$);
|
|
|
|
sub getNext($);
|
|
|
|
sub writeConverters($$);
|
2017-06-20 22:09:50 +00:00
|
|
|
sub createConvLine($$);
|
2017-06-18 10:13:57 +00:00
|
|
|
|
|
|
|
sub getSubstitutes($$@)
|
|
|
|
{
|
|
|
|
my ($rAllowedKeys, $rSubst, @args) = @_;
|
|
|
|
|
|
|
|
my @ctestpars = ();
|
|
|
|
my $ctestparams = 0;
|
|
|
|
for my $arg (@args) {
|
|
|
|
if ($ctestparams) {
|
|
|
|
push(@ctestpars, $arg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($arg =~ /^([^=]+)=(.*)$/) {
|
|
|
|
my $key = $1;
|
|
|
|
my $value = $2;
|
|
|
|
my $valid = 0;
|
|
|
|
if (defined($rAllowedKeys->{$key})) {
|
2020-08-19 20:28:50 +00:00
|
|
|
if (ref($rAllowedKeys->{$key}) eq "ARRAY") {
|
|
|
|
for my $val (@{$rAllowedKeys->{$key}}) {
|
|
|
|
if ($val eq $value) {
|
|
|
|
$valid = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($rAllowedKeys->{$key} eq "integer") {
|
|
|
|
if ($value =~ /^\d+$/) {
|
2017-06-18 10:13:57 +00:00
|
|
|
$valid = 1;
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 20:28:50 +00:00
|
|
|
elsif ($rAllowedKeys->{$key} eq "string") {
|
|
|
|
$valid = 1;
|
|
|
|
}
|
2017-06-18 10:13:57 +00:00
|
|
|
}
|
|
|
|
if ($valid) {
|
|
|
|
$rSubst->{$key} = [$value, 0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die("invalid key or value specified in \"$arg\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ctestparams = 1;
|
|
|
|
push(@ctestpars, $arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(@ctestpars);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getConverters($$$)
|
|
|
|
{
|
|
|
|
my ($userdir, $rConverter, $add) = @_;
|
|
|
|
|
|
|
|
if (open(FI, "$userdir/lyxrc.defaults")) {
|
|
|
|
while (my $l = <FI>) {
|
|
|
|
if ($l =~ s/^\s*\\converter\s+//) {
|
|
|
|
my $entry = &getConverter($l, $add);
|
|
|
|
if (defined($entry)) {
|
|
|
|
$rConverter->{$entry->[0]} = $entry->[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(FI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getConverter($$)
|
|
|
|
{
|
|
|
|
my ($l, $add) = @_;
|
|
|
|
chomp($l);
|
|
|
|
my ($from, $to, $cmd, $par);
|
|
|
|
($l, $from) = getNext($l);
|
2021-05-30 18:24:42 +00:00
|
|
|
return undef if ($from !~ /(ps|tex|dvi)$/);
|
2017-06-18 10:13:57 +00:00
|
|
|
($l, $to) = getNext($l);
|
2017-06-20 22:09:50 +00:00
|
|
|
return undef if ($to !~ /^((dvi3?|pdf[23456]?)(log)?)$/);
|
2021-05-30 18:24:42 +00:00
|
|
|
my ($checkfor, $substitute, $extrapar);
|
2021-05-23 08:05:13 +00:00
|
|
|
if ($from =~ /tex/) {
|
|
|
|
$checkfor = qr/\s+\-shell\-(escape|restricted)/;
|
|
|
|
$substitute = "-shell-escape";
|
2021-05-30 18:24:42 +00:00
|
|
|
$extrapar = qr/^latex/;
|
|
|
|
}
|
|
|
|
elsif ($from =~ /ps$/) {
|
|
|
|
$checkfor = qr/\s+\-dALLOWPSTRANSPARENCY/;
|
|
|
|
$substitute = "-dALLOWPSTRANSPARENCY";
|
|
|
|
$extrapar = qr/hyperref-driver=dvips/;
|
2021-05-23 08:05:13 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$checkfor = qr/\s+-i\s+dvipdfmx-unsafe.cfg/;
|
|
|
|
$substitute = "-i dvipdfmx-unsafe.cfg";
|
2021-05-30 18:24:42 +00:00
|
|
|
$extrapar = qr/^hyperref-driver=dvipdfm/;
|
2021-05-23 08:05:13 +00:00
|
|
|
}
|
2017-06-18 10:13:57 +00:00
|
|
|
($l, $cmd) = getNext($l);
|
|
|
|
if ($add) {
|
2021-05-23 08:05:13 +00:00
|
|
|
if ($cmd !~ $checkfor) {
|
2017-06-18 10:13:57 +00:00
|
|
|
if ($cmd =~ /^(\S+)\s*(.*)$/) {
|
2021-05-23 08:05:13 +00:00
|
|
|
$cmd = "$1 $substitute $2";
|
2017-06-18 10:13:57 +00:00
|
|
|
$cmd =~ s/\s+$//;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-05-23 08:05:13 +00:00
|
|
|
$cmd =~ s/$checkfor//;
|
2017-06-18 10:13:57 +00:00
|
|
|
}
|
|
|
|
($l, $par) = getNext($l);
|
2021-05-30 18:24:42 +00:00
|
|
|
return undef if ($par !~ $extrapar);
|
2017-06-18 10:13:57 +00:00
|
|
|
my $key = "\"$from\" \"$to\"";
|
|
|
|
if ($add) {
|
|
|
|
return([$key, [$cmd, $par]]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# Removes entry from prefs
|
|
|
|
return([$key, [$cmd, $par, 1]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getNext($)
|
|
|
|
{
|
|
|
|
my ($l) = @_;
|
|
|
|
my $val = undef;
|
|
|
|
|
|
|
|
if ($l =~ /^\s*\"([^\"]*)\"\s*(.*)$/) {
|
|
|
|
$val = $1;
|
|
|
|
$l = $2;
|
|
|
|
}
|
|
|
|
elsif ($l =~ /^\s*(\S+)\s*(.*)$/) {
|
|
|
|
$val = $1;
|
|
|
|
$l = $2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exit(7);
|
|
|
|
}
|
|
|
|
return($l, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub writeConverters($$)
|
|
|
|
{
|
|
|
|
my ($fo, $rConverter) = @_;
|
|
|
|
for my $key (sort keys %{$rConverter}) {
|
|
|
|
if (! defined($rConverter->{$key}->[2])) {
|
|
|
|
$rConverter->{$key}->[2] = 1;
|
|
|
|
my $l = &createConvLine($key, $rConverter);
|
|
|
|
print $fo $l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub createConvLine($$)
|
|
|
|
{
|
|
|
|
my ($key, $rConverter) = @_;
|
|
|
|
my $l = "\\converter $key \"$rConverter->{$key}->[0]\" \"$rConverter->{$key}->[1]\"\n";
|
|
|
|
return($l);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub applyChanges($$$$)
|
|
|
|
{
|
|
|
|
my ($userdir, $rSubst, $rConverter, $add) = @_;
|
|
|
|
|
|
|
|
if (open(FO, '>', "$userdir/preferences.tmp")) {
|
|
|
|
if (open(FI, "$userdir/preferences")) {
|
|
|
|
my $conv_section = -1;
|
|
|
|
while (my $l = <FI>) {
|
|
|
|
if ($conv_section-- == 0) {
|
|
|
|
&writeConverters(*FO, $rConverter);
|
|
|
|
}
|
|
|
|
if ($l =~ /^\# CONVERTERS SECTION/) {
|
|
|
|
$conv_section = 2; # converters begin 2 lines later
|
|
|
|
}
|
|
|
|
if ($l =~ /^\\converter\s+(.*)$/) {
|
|
|
|
my $entry = &getConverter($1, $add);
|
|
|
|
if (defined($entry)) {
|
|
|
|
my $key = $entry->[0];
|
|
|
|
if (defined($rConverter->{$key})) {
|
|
|
|
if (defined($rConverter->{$key}->[2])) {
|
|
|
|
$l = "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$rConverter->{$key}->[2] = 1;
|
2017-06-20 22:09:50 +00:00
|
|
|
$l = &createConvLine($key, $rConverter);
|
2017-06-18 10:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-20 22:09:50 +00:00
|
|
|
else {
|
|
|
|
# Converter defined only in preferences
|
|
|
|
$rConverter->{$key} = $entry->[1];
|
|
|
|
$rConverter->{$key}->[2] = 1;
|
|
|
|
$l = &createConvLine($key, $rConverter);
|
|
|
|
}
|
2017-06-18 10:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for my $k (keys %{$rSubst}) {
|
|
|
|
if ($l =~ /^\\$k\b/) {
|
|
|
|
$l = "\\$k $rSubst->{$k}->[0]\n";
|
|
|
|
$rSubst->{$k}->[1] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print FO $l;
|
|
|
|
}
|
|
|
|
close(FI);
|
|
|
|
}
|
|
|
|
for my $k (keys %{$rSubst}) {
|
|
|
|
if ($rSubst->{$k}->[1] == 0) {
|
|
|
|
print FO "\\$k $rSubst->{$k}->[0]\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&writeConverters(*FO, $rConverter);
|
|
|
|
close(FO);
|
|
|
|
rename("$userdir/preferences.tmp", "$userdir/preferences");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|