13 @EXPORT = qw(initLyxStack checkLyxLine closeLyxStack diestack);
16 my @stack = (); # list of HASH-Arrays
19 # type (layout, inset, header, preamble, ...)
21 # matching list of matching spes
22 # search: regular expression
23 # ext: list of extensions needed for the full path of the file spec
24 # filetype: one of prefix_only,replace_only,copy_only,prefix_for_list,interpret
25 # fileidx: index into the resulting array, defining the filename
26 # result: conatenation of the elements should reflect the parsed line
27 # but first set the modified value into $result->[$fileidx]
28 # numerical value will be replaced with appropriate matching group value
33 $stack[0] = { type => "Starting"};
40 print "Called stack\n";
42 for my $depth ( 0 .. 100) {
43 #my ($pkg, $file, $line, $subname, $hasargs, $wantarray) = caller($depth)
44 my @stack = caller($depth);
45 last if ($stack[0] ne "main");
46 push(@call_stack, \@stack);
48 for my $depth ( 0 .. 100) {
49 last if (! defined($call_stack[$depth]));
50 my $subname = $call_stack[$depth]->[3];
51 my $line = $call_stack[$depth]->[2];
52 print "($depth) $subname()";
54 my $oldline = $call_stack[$depth-1]->[2];
57 print " called from ";
58 if (defined($call_stack[$depth+1])) {
59 my $parent = $call_stack[$depth+1]->[3];
60 print "$parent():$line\n";
63 my $file = $call_stack[$depth]->[1];
64 print "\"$file\":$line\n";
72 &diestack("Stack not OK") if ($stack[0]->{type} ne "Starting");
79 $stack[0]->{"matching"} = $match;
84 return($stack[0]->{"matching"});
87 ###########################################################
89 sub checkForEndBlock($)
93 for my $et ( qw( layout inset preamble header)) {
94 if ($l =~ /^\\end_$et$/) {
95 &diestack("Not in $et") if ($stack[0]->{type} ne "$et");
108 if (! defined($elem{"ext"})) {
111 if (! defined($elem{"filetype"})) {
112 $elem{"filetype"} = "prefix_only";
114 if (! defined($elem{"fileidx"})) {
115 $elem{"fileidx"} = 1;
117 &diestack("No result defined") if (! defined($elem{"result"}));
125 return($m->{"search"});
132 return($m->{"filetype"});
139 return($m->{"fileidx"});
153 return($m->{"result"});
156 sub checkForHeader($)
160 if ($l =~ /^\\begin_header\s*$/) {
162 $selem{type} = "header";
164 unshift(@stack, \%selem);
166 $rElems[0] = &newMatch("search" => '^\\\\master\s+(.*\.lyx)',
167 "filetype" => "prefix_only",
168 "result" => ["\\master ", ""]);
169 if (keys %{$rFont}) {
170 for my $ff ( keys %{$rFont}) {
171 my $elem = &newMatch("search" => '^\\\\font_' . $ff . '\s+default',
172 "filetype" => "replace_only",
173 "result" => ["\\font_$ff ", $rFont->{$ff}]);
174 push(@rElems, $elem);
176 my $elemntf = &newMatch("search" => '^\\\\use_non_tex_fonts\s+false',
177 "filetype" => "replace_only",
178 "result" => ["\\use_non_tex_fonts true"]);
179 push(@rElems, $elemntf);
181 &setMatching(\@rElems);
187 sub checkForPreamble($)
191 if ($l =~ /^\\begin_preamble\s*$/) {
193 $selem{type} = "preamble";
195 unshift(@stack, \%selem);
196 my $rElem = &newMatch("ext" => [".eps", ".png"],
197 "search" => '^\\\\photo(.*\{)(.*)\}',
199 "result" => ["\\photo", "1", "2", "}"]);
200 &setMatching([$rElem]);
206 sub checkForLayoutStart($)
210 if ($l =~ /^\\begin_layout\s+(.+)$/) {
211 #print "started layout\n";
213 $selem{type} = "layout";
215 unshift(@stack, \%selem);
216 if ($selem{name} =~ /^(Picture|Photo)$/ ) {
217 my $rElem = &newMatch("ext" => [".eps", ".png"],
219 "result" => ["", "", ""]);
220 &setMatching([$rElem]);
227 sub checkForInsetStart($)
231 if ($l =~ /^\\begin_inset\s+(.*)$/) {
232 #print "started inset\n";
234 $selem{type} = "inset";
236 unshift(@stack, \%selem);
237 if ($selem{name} =~ /^(Graphics|External)$/) {
238 my $rElem = &newMatch("search" => '^\s+filename\s+(.+)$',
239 "filetype" => "copy_only",
240 "result" => ["\tfilename ", "", ""]);
241 &setMatching([$rElem]);
248 sub checkForLatexCommand($)
252 if ($stack[0]->{type} eq "inset") {
253 if ($l =~ /^LatexCommand\s+([^\s]+)\s*$/) {
255 if ($stack[0]->{name} =~ /^CommandInset\s+bibtex$/) {
256 if ($param eq "bibtex") {
257 my $rElem1 = &newMatch("ext" => ".bib",
258 "filetype" => "prefix_for_list",
259 "search" => '^bibfiles\s+\"(.+)\"',
260 "result" => ["bibfiles \"", "1", "\""]);
261 my $rElem2 = &newMatch("ext" => ".bst",
262 "filetype" => "prefix_for_list",
263 "search" => '^options\s+\"(.+)\"',
264 "result" => ["options \"", "1", "\""]);
265 &setMatching([$rElem1, $rElem2]);
268 elsif ($stack[0]->{name} =~ /^CommandInset\s+include$/) {
269 if ($param =~ /^(verbatiminput\*?|lstinputlisting)$/) {
270 my $rElem = &newMatch("search" => '^filename\s+\"(.+)\"',
271 "filetype" => "copy_only",
272 "result" => ["filename \"", "", "\""]);
273 &setMatching([$rElem]);
275 elsif ($param =~ /^(include|input)$/) {
276 my $rElem = &newMatch("search" => '^filename\s+\"(.+)\"',
277 "filetype" => "interpret",
278 "result" => ["filename \"", "", "\""]);
279 &setMatching([$rElem]);
288 # parse the given line
289 # returns a hash with folloving values
290 # found: 1 if line matched some regex
291 # fileidx: index into result
292 # ext: list of possible extensions to use for a valid file
293 # filelist: list of found file-pathes (may be more then one, e.g. in bibfiles spec)
294 # separator: to be used while concatenating the filenames
295 # filetype: prefix_only,replace_only,copy_only,interpret
296 # same as before, but without 'prefix_for_list'
301 return({"found" => 0}) if (&checkForHeader($l));
302 return({"found" => 0}) if (&checkForPreamble($l));
303 return({"found" => 0}) if (&checkForEndBlock($l));
304 return({"found" => 0}) if (&checkForLayoutStart($l));
305 return({"found" => 0}) if (&checkForInsetStart($l));
306 return({"found" => 0}) if (&checkForLatexCommand($l));
307 if (defined($stack[0])) {
308 my $rMatch = &getMatching();
309 for my $m ( @{$rMatch}) {
310 my $search = &getSearch($m);
311 if ($l =~ /$search/) {
312 my @matches = ($1, $2, $3, $4);
313 my $filetype = &getFileType($m);
314 my @result2 = @{&getResult($m)};
316 for my $r (@result2) {
321 if ($filetype eq "replace_only") {
323 my %result = ("found" => 1,
324 "filetype" => $filetype,
325 "result" => \@result2);
329 my $fileidx = &getFileIdx($m);
330 my $filename = $matches[$fileidx-1];
331 if ($filename !~ /^\.*$/) {
332 my %result = ("found" => 1,
333 "fileidx" => $fileidx,
334 "ext" => &getExt($m),
335 "result" => \@result2);
336 if ($filetype eq "prefix_for_list") {
337 # bibfiles|options in CommandInset bibtex
338 my @filenames = split(',', $filename);
339 $result{"separator"} = ",";
340 $result{"filelist"} = \@filenames;
341 $result{"filetype"} = "prefix_only";
344 $result{"separator"} = "";
345 $result{"filelist"} = [$filename];
346 $result{"filetype"} = $filetype;
354 return({"found" => 0});