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";
59 elsif ($_[1] eq "dontChange") {
60 $useNonTexFont = "dontChange";
63 $useNonTexFont = "false";
64 $inputEncoding = $_[2];
66 $stack[0] = { type => "Starting"};
73 print "Called stack\n";
75 for my $depth ( 0 .. 100) {
76 #my ($pkg, $file, $line, $subname, $hasargs, $wantarray) = caller($depth)
77 my @stack = caller($depth);
78 last if ($stack[0] ne "main");
79 push(@call_stack, \@stack);
81 for my $depth ( 0 .. 100) {
82 last if (! defined($call_stack[$depth]));
83 my $subname = $call_stack[$depth]->[3];
84 my $line = $call_stack[$depth]->[2];
85 print "($depth) $subname()";
87 my $oldline = $call_stack[$depth-1]->[2];
90 print " called from ";
91 if (defined($call_stack[$depth+1])) {
92 my $parent = $call_stack[$depth+1]->[3];
93 print "$parent():$line\n";
96 my $file = $call_stack[$depth]->[1];
97 print "\"$file\":$line\n";
105 diestack("Stack not OK") if ($stack[0]->{type} ne "Starting");
112 $stack[0]->{"matching"} = $match;
117 return($stack[0]->{"matching"});
120 ###########################################################
122 sub checkForEndBlock($)
126 for my $et ( qw( layout inset preamble header)) {
127 if ($l =~ /^\\end_$et$/) {
128 diestack("Not in $et") if ($stack[0]->{type} ne "$et");
141 if (! defined($elem{"ext"})) {
144 if (! defined($elem{"filetype"})) {
145 $elem{"filetype"} = "prefix_only";
147 if (! defined($elem{"fileidx"})) {
148 $elem{"fileidx"} = 1;
150 if (exists($elem{"search"})) {
151 my $ref = ref($elem{"search"});
152 diestack("Wrong or invalid regex (ref == $ref) specified") if ($ref ne "Regexp");
155 diestack("No search defined");
157 diestack("No result defined") if (! defined($elem{"result"}));
165 return($m->{"search"});
172 return($m->{"filetype"});
179 return($m->{"fileidx"});
193 return($m->{"result"});
196 sub checkForHeader($)
200 if ($l =~ /^\\begin_header\s*$/) {
202 $selem{type} = "header";
204 unshift(@stack, \%selem);
206 $rElems[0] = newMatch("search" => qr/^\\master\s+(.*\.lyx)/,
207 "filetype" => "prefix_only",
208 "result" => ["\\master ", ""]);
209 if (keys %{$rFont}) {
210 for my $ff ( keys %{$rFont}) {
211 # fontentry of type '\font_roman default'
212 my $elem = newMatch("search" => qr/^\\font_$ff\s+[^\"]*\s*$/,
213 "filetype" => "replace_only",
214 "result" => ["\\font_$ff ", $rFont->{$ff}]);
215 # fontentry of type '\font_roman "default"'
216 my $elem1 = newMatch("search" => qr/^\\font_$ff\s+\"[^\"]*\"\s*$/,
217 "filetype" => "replace_only",
218 "result" => ["\\font_$ff \"", $rFont->{$ff}, '"']);
219 # fontentry of type '\font_roman "default" "default"'
220 my $elem2 = newMatch("search" => qr/^\\font_$ff\s+\"(.*)\"\s+\"default\"\s*$/,
221 "filetype" => "replace_only",
222 "result" => ["\\font_$ff ", '"', "1", '" "', $rFont->{$ff}, '"']);
223 push(@rElems, $elem, $elem1, $elem2);
226 if ($useNonTexFont ne "dontChange") {
227 my $elemntf = newMatch("search" => qr/^\\use_non_tex_fonts\s+(false|true)/,
228 "filetype" => "replace_only",
229 "result" => ["\\use_non_tex_fonts $useNonTexFont"]);
230 push(@rElems, $elemntf);
232 if (defined($inputEncoding)) {
233 my $inputenc = newMatch("search" => qr/^\\inputencoding\s+($inputEncoding->{search})/,
234 "filetype" => "replace_only",
235 "result" => ["\\inputencoding " . $inputEncoding->{out}]);
236 push(@rElems, $inputenc);
238 setMatching(\@rElems);
244 sub checkForPreamble($)
248 if ($l =~ /^\\begin_preamble\s*$/) {
250 $selem{type} = "preamble";
252 unshift(@stack, \%selem);
253 my $rElem = newMatch("ext" => [".eps", ".png"],
254 "search" => qr/^\\(photo|ecvpicture)(.*\{)(.*)\}/,
256 "result" => ["\\", "1", "2", "3", "}"]);
258 # Remove comments from preamble
259 my $comments = newMatch("search" => qr/^([^%]*)([%]+)([^%]*)$/,
260 "filetype" => "replace_only",
261 "result" => ["1", "2"]);
262 setMatching([$rElem, $comments]);
268 sub checkForLayoutStart($)
272 if ($l =~ /^\\begin_layout\s+(.*)$/) {
273 #print "started layout\n";
275 $selem{type} = "layout";
277 unshift(@stack, \%selem);
278 if ($selem{name} =~ /^(Picture|Photo)$/ ) {
279 my $rElem = newMatch("ext" => [".eps", ".png", ""],
280 "filetype" => "copy_only",
281 "search" => qr/^(.+)/,
282 "result" => ["", "", ""]);
283 setMatching([$rElem]);
290 sub checkForInsetStart($)
294 if ($l =~ /^\\begin_inset\s+(.*)$/) {
295 #print "started inset\n";
297 $selem{type} = "inset";
299 unshift(@stack, \%selem);
300 if ($selem{name} =~ /^(Graphics|External)$/) {
301 my $rElem = newMatch("search" => qr/^\s+filename\s+(.+)$/,
302 "filetype" => "copy_only",
303 "result" => ["\tfilename ", "", ""]);
304 setMatching([$rElem]);
311 sub checkForLatexCommand($)
315 if ($stack[0]->{type} eq "inset") {
316 if ($l =~ /^LatexCommand\s+([^\s]+)\s*$/) {
318 if ($stack[0]->{name} =~ /^CommandInset\s+bibtex$/) {
319 if ($param eq "bibtex") {
320 my $rElem1 = newMatch("ext" => ".bib",
321 "filetype" => "prefix_for_list",
322 "search" => qr/^bibfiles\s+\"(.+)\"/,
323 "result" => ["bibfiles \"", "1", "\""]);
324 my $rElem2 = newMatch("ext" => ".bst",
325 "filetype" => "prefix_for_list",
326 "search" => qr/^options\s+\"(.+)\"/,
327 "result" => ["options \"", "1", "\""]);
328 setMatching([$rElem1, $rElem2]);
331 elsif ($stack[0]->{name} =~ /^CommandInset\s+include$/) {
332 if ($param =~ /^(verbatiminput\*?|lstinputlisting|inputminted)$/) {
333 my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
334 "filetype" => "copy_only",
335 "result" => ["filename \"", "", "\""]);
336 setMatching([$rElem]);
338 elsif ($param =~ /^(include|input)$/) {
339 my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
340 "filetype" => "interpret",
341 "result" => ["filename \"", "", "\""]);
342 setMatching([$rElem]);
351 # parse the given line
352 # returns a hash with folloving values
353 # found: 1 if line matched some regex
354 # fileidx: index into result
355 # ext: list of possible extensions to use for a valid file
356 # filelist: list of found file-pathes (may be more then one, e.g. in bibfiles spec)
357 # separator: to be used while concatenating the filenames
358 # filetype: prefix_only,replace_only,copy_only,interpret
359 # same as before, but without 'prefix_for_list'
364 return({"found" => 0}) if (checkForHeader($l));
365 return({"found" => 0}) if (checkForPreamble($l));
366 return({"found" => 0}) if (checkForEndBlock($l));
367 return({"found" => 0}) if (checkForLayoutStart($l));
368 return({"found" => 0}) if (checkForInsetStart($l));
369 return({"found" => 0}) if (checkForLatexCommand($l));
370 if (defined($stack[0])) {
371 my $rMatch = getMatching();
372 for my $m ( @{$rMatch}) {
373 my $search = getSearch($m);
375 my @matches = ($1, $2, $3, $4);
376 my $filetype = getFileType($m);
377 my @result2 = @{getResult($m)};
379 for my $r (@result2) {
384 if ($filetype eq "replace_only") {
386 my %result = ("found" => 1,
387 "filetype" => $filetype,
388 "result" => \@result2);
392 my $fileidx = getFileIdx($m);
393 my $filename = $matches[$fileidx-1];
394 if ($filename !~ /^\.*$/) {
395 my %result = ("found" => 1,
396 "fileidx" => $fileidx,
398 "result" => \@result2);
399 if ($filetype eq "prefix_for_list") {
400 # bibfiles|options in CommandInset bibtex
401 my @filenames = split(',', $filename);
402 $result{"separator"} = ",";
403 $result{"filelist"} = \@filenames;
404 $result{"filetype"} = "prefix_only";
407 $result{"separator"} = "";
408 $result{"filelist"} = [$filename];
409 $result{"filetype"} = $filetype;
417 return({"found" => 0});