13 @EXPORT = qw(initLyxStack checkLyxLine closeLyxStack diestack);
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";
41 # type (layout, inset, header, preamble, ...)
43 # matching list of matching spes
44 # search: regular expression
45 # ext: list of extensions needed for the full path of the file spec
46 # filetype: one of prefix_only,replace_only,copy_only,prefix_for_list,interpret
47 # fileidx: index into the resulting array, defining the filename
48 # result: conatenation of the elements should reflect the parsed line
49 # but first set the modified value into $result->[$fileidx]
50 # numerical value will be replaced with appropriate matching group value
55 if ($_[1] eq "systemF") {
56 $useNonTexFont = "true";
59 $useNonTexFont = "false";
61 $stack[0] = { type => "Starting"};
68 print "Called stack\n";
70 for my $depth ( 0 .. 100) {
71 #my ($pkg, $file, $line, $subname, $hasargs, $wantarray) = caller($depth)
72 my @stack = caller($depth);
73 last if ($stack[0] ne "main");
74 push(@call_stack, \@stack);
76 for my $depth ( 0 .. 100) {
77 last if (! defined($call_stack[$depth]));
78 my $subname = $call_stack[$depth]->[3];
79 my $line = $call_stack[$depth]->[2];
80 print "($depth) $subname()";
82 my $oldline = $call_stack[$depth-1]->[2];
85 print " called from ";
86 if (defined($call_stack[$depth+1])) {
87 my $parent = $call_stack[$depth+1]->[3];
88 print "$parent():$line\n";
91 my $file = $call_stack[$depth]->[1];
92 print "\"$file\":$line\n";
100 diestack("Stack not OK") if ($stack[0]->{type} ne "Starting");
107 $stack[0]->{"matching"} = $match;
112 return($stack[0]->{"matching"});
115 ###########################################################
117 sub checkForEndBlock($)
121 for my $et ( qw( layout inset preamble header)) {
122 if ($l =~ /^\\end_$et$/) {
123 diestack("Not in $et") if ($stack[0]->{type} ne "$et");
136 if (! defined($elem{"ext"})) {
139 if (! defined($elem{"filetype"})) {
140 $elem{"filetype"} = "prefix_only";
142 if (! defined($elem{"fileidx"})) {
143 $elem{"fileidx"} = 1;
145 diestack("No result defined") if (! defined($elem{"result"}));
153 return($m->{"search"});
160 return($m->{"filetype"});
167 return($m->{"fileidx"});
181 return($m->{"result"});
184 sub checkForHeader($)
188 if ($l =~ /^\\begin_header\s*$/) {
190 $selem{type} = "header";
192 unshift(@stack, \%selem);
194 $rElems[0] = newMatch("search" => '^\\\\master\s+(.*\.lyx)',
195 "filetype" => "prefix_only",
196 "result" => ["\\master ", ""]);
197 if (keys %{$rFont}) {
198 for my $ff ( keys %{$rFont}) {
199 my $elem = newMatch("search" => '^\\\\font_' . $ff . '\s+',
200 "filetype" => "replace_only",
201 "result" => ["\\font_$ff ", $rFont->{$ff}]);
202 push(@rElems, $elem);
205 my $elemntf = newMatch("search" => '^\\\\use_non_tex_fonts\s+(false|true)',
206 "filetype" => "replace_only",
207 "result" => ["\\use_non_tex_fonts $useNonTexFont"]);
208 push(@rElems, $elemntf);
209 setMatching(\@rElems);
215 sub checkForPreamble($)
219 if ($l =~ /^\\begin_preamble\s*$/) {
221 $selem{type} = "preamble";
223 unshift(@stack, \%selem);
224 my $rElem = newMatch("ext" => [".eps", ".png"],
225 "search" => '^\\\\(photo|ecvpicture)(.*\{)(.*)\}',
227 "result" => ["\\", "1", "2", "3", "}"]);
228 setMatching([$rElem]);
234 sub checkForLayoutStart($)
238 if ($l =~ /^\\begin_layout\s+(.+)$/) {
239 #print "started layout\n";
241 $selem{type} = "layout";
243 unshift(@stack, \%selem);
244 if ($selem{name} =~ /^(Picture|Photo)$/ ) {
245 my $rElem = newMatch("ext" => [".eps", ".png"],
247 "result" => ["", "", ""]);
248 setMatching([$rElem]);
255 sub checkForInsetStart($)
259 if ($l =~ /^\\begin_inset\s+(.*)$/) {
260 #print "started inset\n";
262 $selem{type} = "inset";
264 unshift(@stack, \%selem);
265 if ($selem{name} =~ /^(Graphics|External)$/) {
266 my $rElem = newMatch("search" => '^\s+filename\s+(.+)$',
267 "filetype" => "copy_only",
268 "result" => ["\tfilename ", "", ""]);
269 setMatching([$rElem]);
276 sub checkForLatexCommand($)
280 if ($stack[0]->{type} eq "inset") {
281 if ($l =~ /^LatexCommand\s+([^\s]+)\s*$/) {
283 if ($stack[0]->{name} =~ /^CommandInset\s+bibtex$/) {
284 if ($param eq "bibtex") {
285 my $rElem1 = newMatch("ext" => ".bib",
286 "filetype" => "prefix_for_list",
287 "search" => '^bibfiles\s+\"(.+)\"',
288 "result" => ["bibfiles \"", "1", "\""]);
289 my $rElem2 = newMatch("ext" => ".bst",
290 "filetype" => "prefix_for_list",
291 "search" => '^options\s+\"(.+)\"',
292 "result" => ["options \"", "1", "\""]);
293 setMatching([$rElem1, $rElem2]);
296 elsif ($stack[0]->{name} =~ /^CommandInset\s+include$/) {
297 if ($param =~ /^(verbatiminput\*?|lstinputlisting)$/) {
298 my $rElem = newMatch("search" => '^filename\s+\"(.+)\"',
299 "filetype" => "copy_only",
300 "result" => ["filename \"", "", "\""]);
301 setMatching([$rElem]);
303 elsif ($param =~ /^(include|input)$/) {
304 my $rElem = newMatch("search" => '^filename\s+\"(.+)\"',
305 "filetype" => "interpret",
306 "result" => ["filename \"", "", "\""]);
307 setMatching([$rElem]);
316 # parse the given line
317 # returns a hash with folloving values
318 # found: 1 if line matched some regex
319 # fileidx: index into result
320 # ext: list of possible extensions to use for a valid file
321 # filelist: list of found file-pathes (may be more then one, e.g. in bibfiles spec)
322 # separator: to be used while concatenating the filenames
323 # filetype: prefix_only,replace_only,copy_only,interpret
324 # same as before, but without 'prefix_for_list'
329 return({"found" => 0}) if (checkForHeader($l));
330 return({"found" => 0}) if (checkForPreamble($l));
331 return({"found" => 0}) if (checkForEndBlock($l));
332 return({"found" => 0}) if (checkForLayoutStart($l));
333 return({"found" => 0}) if (checkForInsetStart($l));
334 return({"found" => 0}) if (checkForLatexCommand($l));
335 if (defined($stack[0])) {
336 my $rMatch = getMatching();
337 for my $m ( @{$rMatch}) {
338 my $search = getSearch($m);
339 if ($l =~ /$search/) {
340 my @matches = ($1, $2, $3, $4);
341 my $filetype = getFileType($m);
342 my @result2 = @{getResult($m)};
344 for my $r (@result2) {
349 if ($filetype eq "replace_only") {
351 my %result = ("found" => 1,
352 "filetype" => $filetype,
353 "result" => \@result2);
357 my $fileidx = getFileIdx($m);
358 my $filename = $matches[$fileidx-1];
359 if ($filename !~ /^\.*$/) {
360 my %result = ("found" => 1,
361 "fileidx" => $fileidx,
363 "result" => \@result2);
364 if ($filetype eq "prefix_for_list") {
365 # bibfiles|options in CommandInset bibtex
366 my @filenames = split(',', $filename);
367 $result{"separator"} = ",";
368 $result{"filelist"} = \@filenames;
369 $result{"filetype"} = "prefix_only";
372 $result{"separator"} = "";
373 $result{"filelist"} = [$filename];
374 $result{"filetype"} = $filetype;
382 return({"found" => 0});