1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
#! /usr/bin/env perl
# -*- mode: perl; -*-
package lyxStatus;
use strict;
our(@EXPORT, @ISA);
BEGIN {
use Exporter ();
@ISA = qw(Exporter);
@EXPORT = qw(initLyxStack checkLyxLine closeLyxStack diestack);
}
# Prototypes
sub initLyxStack($$$);
sub diestack($);
sub closeLyxStack();
sub setMatching($);
sub getMatching();
sub checkForEndBlock($);
sub newMatch(%);
sub getSearch($);
sub getFileType($);
sub getFileIdx($);
sub getExt($);
sub getResult($);
sub checkForHeader($$);
sub checkForPreamble($);
sub checkForLayoutStart($);
sub checkForInsetStart($);
sub checkForLatexCommand($);
sub checkLyxLine($$);
my @stack = (); # list of HASH-Arrays
my $rFont = {};
my $useNonTexFont = "true";
my $inputEncoding = undef;
my $sysdir = undef;
# The elements are:
# type (layout, inset, header, preamble, ...)
# name
# matching list of matching spes
# search: regular expression
# ext: list of extensions needed for the full path of the file spec
# filetype: one of prefix_only,replace_only,copy_only,prefix_for_list,interpret
# fileidx: index into the resulting array, defining the filename
# result: conatenation of the elements should reflect the parsed line
# but first set the modified value into $result->[$fileidx]
# numerical value will be replaced with appropriate matching group value
sub initLyxStack($$$)
{
use Cwd 'abs_path';
$rFont = $_[0];
if ($_[1] eq "systemF") {
$useNonTexFont = "true";
}
elsif ($_[1] eq "dontChange") {
$useNonTexFont = "dontChange";
}
else {
$useNonTexFont = "false";
$inputEncoding = $_[2];
}
$stack[0] = { type => "Starting"};
my $p = abs_path( __FILE__ );
$p =~ s/\/development\/autotests\/.*$/\/lib/;
# Save the value to be used as replacement for systemlyxdir in \origin statements
$sysdir = $p;
# print "Sysdir set to $sysdir\n";
}
sub diestack($)
{
my ($msg) = @_;
# Print stack
print "Called stack\n";
my @call_stack = ();
for my $depth ( 0 .. 100) {
#my ($pkg, $file, $line, $subname, $hasargs, $wantarray) = caller($depth)
my @stack = caller($depth);
last if ($stack[0] ne "main");
push(@call_stack, \@stack);
}
for my $depth ( 0 .. 100) {
last if (! defined($call_stack[$depth]));
my $subname = $call_stack[$depth]->[3];
my $line = $call_stack[$depth]->[2];
print "($depth) $subname()";
if ($depth > 0) {
my $oldline = $call_stack[$depth-1]->[2];
print ":$oldline";
}
print " called from ";
if (defined($call_stack[$depth+1])) {
my $parent = $call_stack[$depth+1]->[3];
print "$parent():$line\n";
}
else {
my $file = $call_stack[$depth]->[1];
print "\"$file\":$line\n";
}
}
die($msg);
}
sub closeLyxStack()
{
diestack("Stack not OK") if ($stack[0]->{type} ne "Starting");
}
sub setMatching($)
{
my ($match) = @_;
$stack[0]->{"matching"} = $match;
}
sub getMatching()
{
return($stack[0]->{"matching"});
}
###########################################################
#
sub checkForEndBlock($)
{
my ($l) = @_;
for my $et ( qw( layout inset preamble header)) {
if ($l =~ /^\\end_$et$/) {
diestack("Not in $et") if ($stack[0]->{type} ne "$et");
#print "End $et\n";
shift(@stack);
return(1);
}
}
return(0);
}
sub newMatch(%)
{
my %elem = @_;
if (! defined($elem{"ext"})) {
$elem{"ext"} = "";
}
if (! defined($elem{"filetype"})) {
$elem{"filetype"} = "prefix_only";
}
if (! defined($elem{"fileidx"})) {
$elem{"fileidx"} = 1;
}
if (exists($elem{"search"})) {
my $ref = ref($elem{"search"});
diestack("Wrong or invalid regex (ref == $ref) specified") if ($ref ne "Regexp");
}
else {
diestack("No search defined");
}
diestack("No result defined") if (! defined($elem{"result"}));
return(\%elem);
}
sub getSearch($)
{
my ($m) = @_;
return($m->{"search"});
}
sub getFileType($)
{
my ($m) = @_;
return($m->{"filetype"});
}
sub getFileIdx($)
{
my ($m) = @_;
return($m->{"fileidx"});
}
sub getExt($)
{
my ($m) = @_;
return($m->{"ext"});
}
sub getResult($)
{
my ($m) = @_;
return($m->{"result"});
}
sub checkForHeader($$)
{
my ($l, $sourcedir) = @_;
if ($l =~ /^\\begin_header\s*$/) {
my %selem = ();
$selem{type} = "header";
$selem{name} = $1;
unshift(@stack, \%selem);
my @rElems = ();
$rElems[0] = newMatch("search" => qr/^\\master\s+(.*\.lyx)/,
"filetype" => "prefix_only",
"result" => ["\\master ", ""]);
if (keys %{$rFont}) {
for my $ff ( keys %{$rFont}) {
# fontentry of type '\font_roman default'
my $elem = newMatch("search" => qr/^\\font_$ff\s+[^\"]*\s*$/,
"filetype" => "replace_only",
"result" => ["\\font_$ff ", $rFont->{$ff}]);
# fontentry of type '\font_roman "default"'
my $elem1 = newMatch("search" => qr/^\\font_$ff\s+\"[^\"]*\"\s*$/,
"filetype" => "replace_only",
"result" => ["\\font_$ff \"", $rFont->{$ff}, '"']);
# fontentry of type '\font_roman "default" "default"'
my $elem2 = newMatch("search" => qr/^\\font_$ff\s+\"(.*)\"\s+\"default\"\s*$/,
"filetype" => "replace_only",
"result" => ["\\font_$ff ", '"', "1", '" "', $rFont->{$ff}, '"']);
push(@rElems, $elem, $elem1, $elem2);
}
}
if ($useNonTexFont ne "dontChange") {
my $elemntf = newMatch("search" => qr/^\\use_non_tex_fonts\s+(false|true)/,
"filetype" => "replace_only",
"result" => ["\\use_non_tex_fonts $useNonTexFont"]);
push(@rElems, $elemntf);
}
if (defined($inputEncoding)) {
my $inputenc = newMatch("search" => qr/^\\inputencoding\s+($inputEncoding->{search})/,
"filetype" => "replace_only",
"result" => ["\\inputencoding " . $inputEncoding->{out}]);
push(@rElems, $inputenc);
}
my $origin = newMatch("search" => qr/^\\origin\s+(\/systemlyxdir)(.*)$/,
"filetype" => "replace_only",
"result" => ["\\origin $sysdir", "2"]);
push(@rElems, $origin);
my $originu = newMatch("search" => qr/^\\origin\s+unavailable/,
"filetype" => "replace_only",
"result" => ["\\origin $sourcedir"]);
push(@rElems, $originu);
setMatching(\@rElems);
return(1);
}
return(0);
}
sub checkForPreamble($)
{
my ($l) = @_;
if ($l =~ /^\\begin_preamble\s*$/) {
my %selem = ();
$selem{type} = "preamble";
$selem{name} = $1;
unshift(@stack, \%selem);
my $rElem = newMatch("ext" => [".eps", ".png"],
"search" => qr/^\\(photo|ecvpicture)(.*\{)(.*)\}/,
"fileidx" => 3,
"result" => ["\\", "1", "2", "3", "}"]);
#
# Remove comments from preamble
my $comments = newMatch("search" => qr/^([^%]*)([%]+)([^%]*)$/,
"filetype" => "replace_only",
"result" => ["1", "2"]);
setMatching([$rElem, $comments]);
return(1);
}
return(0);
}
sub checkForLayoutStart($)
{
my ($l) = @_;
if ($l =~ /^\\begin_layout\s+(.*)$/) {
#print "started layout\n";
my %selem = ();
$selem{type} = "layout";
$selem{name} = $1;
unshift(@stack, \%selem);
if ($selem{name} =~ /^(Picture|Photo)$/ ) {
my $rElem = newMatch("ext" => [".eps", ".png", ""],
"filetype" => "copy_only",
"search" => qr/^(.+)/,
"result" => ["", "", ""]);
setMatching([$rElem]);
}
return(1);
}
return(0);
}
sub checkForInsetStart($)
{
my ($l) = @_;
if ($l =~ /^\\begin_inset\s+(.*)$/) {
#print "started inset\n";
my %selem = ();
$selem{type} = "inset";
$selem{name} = $1;
unshift(@stack, \%selem);
if ($selem{name} =~ /^(Graphics|External)$/) {
my $rElem = newMatch("search" => qr/^\s+filename\s+(.+)$/,
"filetype" => "copy_only",
"result" => ["\tfilename ", "", ""]);
setMatching([$rElem]);
}
return(1);
}
return(0);
}
sub checkForLatexCommand($)
{
my ($l) = @_;
if ($stack[0]->{type} eq "inset") {
if ($l =~ /^LatexCommand\s+([^\s]+)\s*$/) {
my $param = $1;
if ($stack[0]->{name} =~ /^CommandInset\s+bibtex$/) {
if ($param eq "bibtex") {
my $rElem1 = newMatch("ext" => ".bib",
"filetype" => "prefix_for_list",
"search" => qr/^bibfiles\s+\"([^\"]+)\"/,
"result" => ["bibfiles \"", "1", "\""]);
my $rElem2 = newMatch("ext" => ".bst",
"filetype" => "prefix_for_list",
"search" => qr/^options\s+\"(.+)\"/,
"result" => ["options \"", "1", "\""]);
setMatching([$rElem1, $rElem2]);
}
}
elsif ($stack[0]->{name} =~ /^CommandInset\s+include$/) {
if ($param =~ /^(verbatiminput\*?|lstinputlisting|inputminted)$/) {
my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
"filetype" => "copy_only",
"result" => ["filename \"", "", "\""]);
setMatching([$rElem]);
}
elsif ($param =~ /^(include|input)$/) {
my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
"filetype" => "interpret",
"result" => ["filename \"", "", "\""]);
setMatching([$rElem]);
}
}
}
}
return(0);
}
#
# parse the given line
# returns a hash with folloving values
# found: 1 if line matched some regex
# fileidx: index into result
# ext: list of possible extensions to use for a valid file
# filelist: list of found file-pathes (may be more then one, e.g. in bibfiles spec)
# separator: to be used while concatenating the filenames
# filetype: prefix_only,replace_only,copy_only,interpret
# same as before, but without 'prefix_for_list'
sub checkLyxLine($$)
{
my ($l, $sourcedir) = @_;
return({"found" => 0}) if (checkForHeader($l, $sourcedir));
return({"found" => 0}) if (checkForPreamble($l));
return({"found" => 0}) if (checkForEndBlock($l));
return({"found" => 0}) if (checkForLayoutStart($l));
return({"found" => 0}) if (checkForInsetStart($l));
return({"found" => 0}) if (checkForLatexCommand($l));
if (defined($stack[0])) {
my $rMatch = getMatching();
for my $m ( @{$rMatch}) {
my $search = getSearch($m);
if ($l =~ $search) {
my @matches = ($1, $2, $3, $4);
my $filetype = getFileType($m);
my @result2 = @{getResult($m)};
for my $r (@result2) {
if ($r =~ /^\d$/) {
$r = $matches[$r-1];
}
}
if ($filetype eq "replace_only") {
# No filename needed
my %result = ("found" => 1,
"filetype" => $filetype,
"result" => \@result2);
return(\%result);
}
else {
my $fileidx = getFileIdx($m);
my $filename = $matches[$fileidx-1];
if ($filename !~ /^\.*$/) {
my %result = ("found" => 1,
"fileidx" => $fileidx,
"ext" => getExt($m),
"result" => \@result2);
if ($filetype eq "prefix_for_list") {
# bibfiles|options in CommandInset bibtex
my @filenames = split(',', $filename);
$result{"separator"} = ",";
$result{"filelist"} = \@filenames;
$result{"filetype"} = "prefix_only";
}
else {
$result{"separator"} = "";
$result{"filelist"} = [$filename];
$result{"filetype"} = $filetype;
}
return(\%result);
}
}
}
}
}
return({"found" => 0});
}
1;
|