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;
42 # type (layout, inset, header, preamble, ...)
44 # matching list of matching spes
45 # search: regular expression
46 # ext: list of extensions needed for the full path of the file spec
47 # filetype: one of prefix_only,replace_only,copy_only,prefix_for_list,interpret
48 # fileidx: index into the resulting array, defining the filename
49 # result: conatenation of the elements should reflect the parsed line
50 # but first set the modified value into $result->[$fileidx]
51 # numerical value will be replaced with appropriate matching group value
56 if ($_[1] eq "systemF") {
57 $useNonTexFont = "true";
60 $useNonTexFont = "false";
61 $inputEncoding = $_[2];
63 $stack[0] = { type => "Starting"};
70 print "Called stack\n";
72 for my $depth ( 0 .. 100) {
73 #my ($pkg, $file, $line, $subname, $hasargs, $wantarray) = caller($depth)
74 my @stack = caller($depth);
75 last if ($stack[0] ne "main");
76 push(@call_stack, \@stack);
78 for my $depth ( 0 .. 100) {
79 last if (! defined($call_stack[$depth]));
80 my $subname = $call_stack[$depth]->[3];
81 my $line = $call_stack[$depth]->[2];
82 print "($depth) $subname()";
84 my $oldline = $call_stack[$depth-1]->[2];
87 print " called from ";
88 if (defined($call_stack[$depth+1])) {
89 my $parent = $call_stack[$depth+1]->[3];
90 print "$parent():$line\n";
93 my $file = $call_stack[$depth]->[1];
94 print "\"$file\":$line\n";
102 diestack("Stack not OK") if ($stack[0]->{type} ne "Starting");
109 $stack[0]->{"matching"} = $match;
114 return($stack[0]->{"matching"});
117 ###########################################################
119 sub checkForEndBlock($)
123 for my $et ( qw( layout inset preamble header)) {
124 if ($l =~ /^\\end_$et$/) {
125 diestack("Not in $et") if ($stack[0]->{type} ne "$et");
138 if (! defined($elem{"ext"})) {
141 if (! defined($elem{"filetype"})) {
142 $elem{"filetype"} = "prefix_only";
144 if (! defined($elem{"fileidx"})) {
145 $elem{"fileidx"} = 1;
147 if (exists($elem{"search"})) {
148 my $ref = ref($elem{"search"});
149 diestack("Wrong or invalid regex (ref == $ref) specified") if ($ref ne "Regexp");
152 diestack("No search defined");
154 diestack("No result defined") if (! defined($elem{"result"}));
162 return($m->{"search"});
169 return($m->{"filetype"});
176 return($m->{"fileidx"});
190 return($m->{"result"});
193 sub checkForHeader($)
197 if ($l =~ /^\\begin_header\s*$/) {
199 $selem{type} = "header";
201 unshift(@stack, \%selem);
203 $rElems[0] = newMatch("search" => qr/^\\master\s+(.*\.lyx)/,
204 "filetype" => "prefix_only",
205 "result" => ["\\master ", ""]);
206 if (keys %{$rFont}) {
207 for my $ff ( keys %{$rFont}) {
208 # fontentry of type '\font_roman default'
209 my $elem = newMatch("search" => qr/^\\font_$ff\s+[^\"]*\s*$/,
210 "filetype" => "replace_only",
211 "result" => ["\\font_$ff ", $rFont->{$ff}]);
212 # fontentry of type '\font_roman "default"'
213 my $elem1 = newMatch("search" => qr/^\\font_$ff\s+\"[^\"]*\"\s*$/,
214 "filetype" => "replace_only",
215 "result" => ["\\font_$ff \"", $rFont->{$ff}, '"']);
216 # fontentry of type '\font_roman "default" "default"'
217 my $elem2 = newMatch("search" => qr/^\\font_$ff\s+\"(.*)\"\s+\"default\"\s*$/,
218 "filetype" => "replace_only",
219 "result" => ["\\font_$ff ", '"', "1", '" "', $rFont->{$ff}, '"']);
220 push(@rElems, $elem, $elem1, $elem2);
223 my $elemntf = newMatch("search" => qr/^\\use_non_tex_fonts\s+(false|true)/,
224 "filetype" => "replace_only",
225 "result" => ["\\use_non_tex_fonts $useNonTexFont"]);
226 push(@rElems, $elemntf);
227 if (defined($inputEncoding)) {
228 my $inputenc = newMatch("search" => qr/^\\inputencoding\s+($inputEncoding->{search})/,
229 "filetype" => "replace_only",
230 "result" => ["\\inputencoding " . $inputEncoding->{out}]);
231 push(@rElems, $inputenc);
233 setMatching(\@rElems);
239 sub checkForPreamble($)
243 if ($l =~ /^\\begin_preamble\s*$/) {
245 $selem{type} = "preamble";
247 unshift(@stack, \%selem);
248 my $rElem = newMatch("ext" => [".eps", ".png"],
249 "search" => qr/^\\(photo|ecvpicture)(.*\{)(.*)\}/,
251 "result" => ["\\", "1", "2", "3", "}"]);
253 # Remove comments from preamble
254 my $comments = newMatch("search" => qr/^([^%]*)([%]+)([^%]*)$/,
255 "filetype" => "replace_only",
256 "result" => ["1", "2"]);
257 setMatching([$rElem, $comments]);
263 sub checkForLayoutStart($)
267 if ($l =~ /^\\begin_layout\s+(.*)$/) {
268 #print "started layout\n";
270 $selem{type} = "layout";
272 unshift(@stack, \%selem);
273 if ($selem{name} =~ /^(Picture|Photo)$/ ) {
274 my $rElem = newMatch("ext" => [".eps", ".png"],
275 "search" => qr/^(.+)/,
276 "result" => ["", "", ""]);
277 setMatching([$rElem]);
284 sub checkForInsetStart($)
288 if ($l =~ /^\\begin_inset\s+(.*)$/) {
289 #print "started inset\n";
291 $selem{type} = "inset";
293 unshift(@stack, \%selem);
294 if ($selem{name} =~ /^(Graphics|External)$/) {
295 my $rElem = newMatch("search" => qr/^\s+filename\s+(.+)$/,
296 "filetype" => "copy_only",
297 "result" => ["\tfilename ", "", ""]);
298 setMatching([$rElem]);
305 sub checkForLatexCommand($)
309 if ($stack[0]->{type} eq "inset") {
310 if ($l =~ /^LatexCommand\s+([^\s]+)\s*$/) {
312 if ($stack[0]->{name} =~ /^CommandInset\s+bibtex$/) {
313 if ($param eq "bibtex") {
314 my $rElem1 = newMatch("ext" => ".bib",
315 "filetype" => "prefix_for_list",
316 "search" => qr/^bibfiles\s+\"(.+)\"/,
317 "result" => ["bibfiles \"", "1", "\""]);
318 my $rElem2 = newMatch("ext" => ".bst",
319 "filetype" => "prefix_for_list",
320 "search" => qr/^options\s+\"(.+)\"/,
321 "result" => ["options \"", "1", "\""]);
322 setMatching([$rElem1, $rElem2]);
325 elsif ($stack[0]->{name} =~ /^CommandInset\s+include$/) {
326 if ($param =~ /^(verbatiminput\*?|lstinputlisting|inputminted)$/) {
327 my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
328 "filetype" => "copy_only",
329 "result" => ["filename \"", "", "\""]);
330 setMatching([$rElem]);
332 elsif ($param =~ /^(include|input)$/) {
333 my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
334 "filetype" => "interpret",
335 "result" => ["filename \"", "", "\""]);
336 setMatching([$rElem]);
345 # parse the given line
346 # returns a hash with folloving values
347 # found: 1 if line matched some regex
348 # fileidx: index into result
349 # ext: list of possible extensions to use for a valid file
350 # filelist: list of found file-pathes (may be more then one, e.g. in bibfiles spec)
351 # separator: to be used while concatenating the filenames
352 # filetype: prefix_only,replace_only,copy_only,interpret
353 # same as before, but without 'prefix_for_list'
358 return({"found" => 0}) if (checkForHeader($l));
359 return({"found" => 0}) if (checkForPreamble($l));
360 return({"found" => 0}) if (checkForEndBlock($l));
361 return({"found" => 0}) if (checkForLayoutStart($l));
362 return({"found" => 0}) if (checkForInsetStart($l));
363 return({"found" => 0}) if (checkForLatexCommand($l));
364 if (defined($stack[0])) {
365 my $rMatch = getMatching();
366 for my $m ( @{$rMatch}) {
367 my $search = getSearch($m);
369 my @matches = ($1, $2, $3, $4);
370 my $filetype = getFileType($m);
371 my @result2 = @{getResult($m)};
373 for my $r (@result2) {
378 if ($filetype eq "replace_only") {
380 my %result = ("found" => 1,
381 "filetype" => $filetype,
382 "result" => \@result2);
386 my $fileidx = getFileIdx($m);
387 my $filename = $matches[$fileidx-1];
388 if ($filename !~ /^\.*$/) {
389 my %result = ("found" => 1,
390 "fileidx" => $fileidx,
392 "result" => \@result2);
393 if ($filetype eq "prefix_for_list") {
394 # bibfiles|options in CommandInset bibtex
395 my @filenames = split(',', $filename);
396 $result{"separator"} = ",";
397 $result{"filelist"} = \@filenames;
398 $result{"filetype"} = "prefix_only";
401 $result{"separator"} = "";
402 $result{"filelist"} = [$filename];
403 $result{"filetype"} = $filetype;
411 return({"found" => 0});