13 @EXPORT = qw(initLyxStack checkLyxLine closeLyxStack diestack);
17 sub initLyxStack($$$);
22 sub checkForEndBlock($);
29 sub checkForHeader($$);
30 sub checkForPreamble($);
31 sub checkForLayoutStart($);
32 sub checkForInsetStart($);
33 sub checkForLatexCommand($);
36 my @stack = (); # list of HASH-Arrays
38 my $useNonTexFont = "true";
39 my $inputEncoding = undef;
43 # type (layout, inset, header, preamble, ...)
45 # matching list of matching spes
46 # search: regular expression
47 # ext: list of extensions needed for the full path of the file spec
48 # filetype: one of prefix_only,replace_only,copy_only,prefix_for_list,interpret
49 # fileidx: index into the resulting array, defining the filename
50 # result: conatenation of the elements should reflect the parsed line
51 # but first set the modified value into $result->[$fileidx]
52 # numerical value will be replaced with appropriate matching group value
59 if ($_[1] eq "systemF") {
60 $useNonTexFont = "true";
62 elsif ($_[1] eq "dontChange") {
63 $useNonTexFont = "dontChange";
66 $useNonTexFont = "false";
67 $inputEncoding = $_[2];
69 $stack[0] = { type => "Starting"};
70 my $p = abs_path( __FILE__ );
71 $p =~ s/\/development\/autotests\/.*$/\/lib/;
72 # Save the value to be used as replacement for systemlyxdir in \origin statements
74 # print "Sysdir set to $sysdir\n";
81 print "Called stack\n";
83 for my $depth ( 0 .. 100) {
84 #my ($pkg, $file, $line, $subname, $hasargs, $wantarray) = caller($depth)
85 my @stack = caller($depth);
86 last if ($stack[0] ne "main");
87 push(@call_stack, \@stack);
89 for my $depth ( 0 .. 100) {
90 last if (! defined($call_stack[$depth]));
91 my $subname = $call_stack[$depth]->[3];
92 my $line = $call_stack[$depth]->[2];
93 print "($depth) $subname()";
95 my $oldline = $call_stack[$depth-1]->[2];
98 print " called from ";
99 if (defined($call_stack[$depth+1])) {
100 my $parent = $call_stack[$depth+1]->[3];
101 print "$parent():$line\n";
104 my $file = $call_stack[$depth]->[1];
105 print "\"$file\":$line\n";
113 diestack("Stack not OK") if ($stack[0]->{type} ne "Starting");
120 $stack[0]->{"matching"} = $match;
125 return($stack[0]->{"matching"});
128 ###########################################################
130 sub checkForEndBlock($)
134 for my $et ( qw( layout inset preamble header)) {
135 if ($l =~ /^\\end_$et$/) {
136 diestack("Not in $et") if ($stack[0]->{type} ne "$et");
149 if (! defined($elem{"ext"})) {
152 if (! defined($elem{"filetype"})) {
153 $elem{"filetype"} = "prefix_only";
155 if (! defined($elem{"fileidx"})) {
156 $elem{"fileidx"} = 1;
158 if (exists($elem{"search"})) {
159 my $ref = ref($elem{"search"});
160 diestack("Wrong or invalid regex (ref == $ref) specified") if ($ref ne "Regexp");
163 diestack("No search defined");
165 diestack("No result defined") if (! defined($elem{"result"}));
173 return($m->{"search"});
180 return($m->{"filetype"});
187 return($m->{"fileidx"});
201 return($m->{"result"});
204 sub checkForHeader($$)
206 my ($l, $sourcedir) = @_;
208 if ($l =~ /^\\begin_header\s*$/) {
210 $selem{type} = "header";
212 unshift(@stack, \%selem);
214 $rElems[0] = newMatch("search" => qr/^\\master\s+(.*\.lyx)/,
215 "filetype" => "prefix_only",
216 "result" => ["\\master ", ""]);
217 if (keys %{$rFont}) {
218 for my $ff ( keys %{$rFont}) {
219 # fontentry of type '\font_roman default'
220 my $elem = newMatch("search" => qr/^\\font_$ff\s+[^\"]*\s*$/,
221 "filetype" => "replace_only",
222 "result" => ["\\font_$ff ", $rFont->{$ff}]);
223 # fontentry of type '\font_roman "default"'
224 my $elem1 = newMatch("search" => qr/^\\font_$ff\s+\"[^\"]*\"\s*$/,
225 "filetype" => "replace_only",
226 "result" => ["\\font_$ff \"", $rFont->{$ff}, '"']);
227 # fontentry of type '\font_roman "default" "default"'
228 my $elem2 = newMatch("search" => qr/^\\font_$ff\s+\"(.*)\"\s+\"default\"\s*$/,
229 "filetype" => "replace_only",
230 "result" => ["\\font_$ff ", '"', "1", '" "', $rFont->{$ff}, '"']);
231 push(@rElems, $elem, $elem1, $elem2);
234 if ($useNonTexFont ne "dontChange") {
235 my $elemntf = newMatch("search" => qr/^\\use_non_tex_fonts\s+(false|true)/,
236 "filetype" => "replace_only",
237 "result" => ["\\use_non_tex_fonts $useNonTexFont"]);
238 push(@rElems, $elemntf);
240 if (defined($inputEncoding)) {
241 my $inputenc = newMatch("search" => qr/^\\inputencoding\s+($inputEncoding->{search})/,
242 "filetype" => "replace_only",
243 "result" => ["\\inputencoding " . $inputEncoding->{out}]);
244 push(@rElems, $inputenc);
246 my $origin = newMatch("search" => qr/^\\origin\s+(\/systemlyxdir)(.*)$/,
247 "filetype" => "replace_only",
248 "result" => ["\\origin $sysdir", "2"]);
249 push(@rElems, $origin);
250 my $originu = newMatch("search" => qr/^\\origin\s+unavailable/,
251 "filetype" => "replace_only",
252 "result" => ["\\origin $sourcedir"]);
253 push(@rElems, $originu);
254 setMatching(\@rElems);
260 sub checkForPreamble($)
264 if ($l =~ /^\\begin_preamble\s*$/) {
266 $selem{type} = "preamble";
268 unshift(@stack, \%selem);
269 my $rElem = newMatch("ext" => [".eps", ".png"],
270 "search" => qr/^\\(photo|ecvpicture)(.*\{)(.*)\}/,
272 "result" => ["\\", "1", "2", "3", "}"]);
274 # Remove comments from preamble
275 my $comments = newMatch("search" => qr/^([^%]*)([%]+)([^%]*)$/,
276 "filetype" => "replace_only",
277 "result" => ["1", "2"]);
278 setMatching([$rElem, $comments]);
284 sub checkForLayoutStart($)
288 if ($l =~ /^\\begin_layout\s+(.*)$/) {
289 #print "started layout\n";
291 $selem{type} = "layout";
293 unshift(@stack, \%selem);
294 if ($selem{name} =~ /^(Picture|Photo)$/ ) {
295 my $rElem = newMatch("ext" => [".eps", ".png", ""],
296 "filetype" => "copy_only",
297 "search" => qr/^(.+)/,
298 "result" => ["", "", ""]);
299 setMatching([$rElem]);
306 sub checkForInsetStart($)
310 if ($l =~ /^\\begin_inset\s+(.*)$/) {
311 #print "started inset\n";
313 $selem{type} = "inset";
315 unshift(@stack, \%selem);
316 if ($selem{name} =~ /^(Graphics|External)$/) {
317 my $rElem = newMatch("search" => qr/^\s+filename\s+(.+)$/,
318 "filetype" => "copy_only",
319 "result" => ["\tfilename ", "", ""]);
320 setMatching([$rElem]);
327 sub checkForLatexCommand($)
331 if ($stack[0]->{type} eq "inset") {
332 if ($l =~ /^LatexCommand\s+([^\s]+)\s*$/) {
334 if ($stack[0]->{name} =~ /^CommandInset\s+bibtex$/) {
335 if ($param eq "bibtex") {
336 my $rElem1 = newMatch("ext" => ".bib",
337 "filetype" => "prefix_for_list",
338 "search" => qr/^bibfiles\s+\"(.+)\"/,
339 "result" => ["bibfiles \"", "1", "\""]);
340 my $rElem2 = newMatch("ext" => ".bst",
341 "filetype" => "prefix_for_list",
342 "search" => qr/^options\s+\"(.+)\"/,
343 "result" => ["options \"", "1", "\""]);
344 setMatching([$rElem1, $rElem2]);
347 elsif ($stack[0]->{name} =~ /^CommandInset\s+include$/) {
348 if ($param =~ /^(verbatiminput\*?|lstinputlisting|inputminted)$/) {
349 my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
350 "filetype" => "copy_only",
351 "result" => ["filename \"", "", "\""]);
352 setMatching([$rElem]);
354 elsif ($param =~ /^(include|input)$/) {
355 my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
356 "filetype" => "interpret",
357 "result" => ["filename \"", "", "\""]);
358 setMatching([$rElem]);
367 # parse the given line
368 # returns a hash with folloving values
369 # found: 1 if line matched some regex
370 # fileidx: index into result
371 # ext: list of possible extensions to use for a valid file
372 # filelist: list of found file-pathes (may be more then one, e.g. in bibfiles spec)
373 # separator: to be used while concatenating the filenames
374 # filetype: prefix_only,replace_only,copy_only,interpret
375 # same as before, but without 'prefix_for_list'
378 my ($l, $sourcedir) = @_;
380 return({"found" => 0}) if (checkForHeader($l, $sourcedir));
381 return({"found" => 0}) if (checkForPreamble($l));
382 return({"found" => 0}) if (checkForEndBlock($l));
383 return({"found" => 0}) if (checkForLayoutStart($l));
384 return({"found" => 0}) if (checkForInsetStart($l));
385 return({"found" => 0}) if (checkForLatexCommand($l));
386 if (defined($stack[0])) {
387 my $rMatch = getMatching();
388 for my $m ( @{$rMatch}) {
389 my $search = getSearch($m);
391 my @matches = ($1, $2, $3, $4);
392 my $filetype = getFileType($m);
393 my @result2 = @{getResult($m)};
395 for my $r (@result2) {
400 if ($filetype eq "replace_only") {
402 my %result = ("found" => 1,
403 "filetype" => $filetype,
404 "result" => \@result2);
408 my $fileidx = getFileIdx($m);
409 my $filename = $matches[$fileidx-1];
410 if ($filename !~ /^\.*$/) {
411 my %result = ("found" => 1,
412 "fileidx" => $fileidx,
414 "result" => \@result2);
415 if ($filetype eq "prefix_for_list") {
416 # bibfiles|options in CommandInset bibtex
417 my @filenames = split(',', $filename);
418 $result{"separator"} = ",";
419 $result{"filelist"} = \@filenames;
420 $result{"filetype"} = "prefix_only";
423 $result{"separator"} = "";
424 $result{"filelist"} = [$filename];
425 $result{"filetype"} = $filetype;
433 return({"found" => 0});