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 "search" => qr/^(.+)/,
281 "result" => ["", "", ""]);
282 setMatching([$rElem]);
289 sub checkForInsetStart($)
293 if ($l =~ /^\\begin_inset\s+(.*)$/) {
294 #print "started inset\n";
296 $selem{type} = "inset";
298 unshift(@stack, \%selem);
299 if ($selem{name} =~ /^(Graphics|External)$/) {
300 my $rElem = newMatch("search" => qr/^\s+filename\s+(.+)$/,
301 "filetype" => "copy_only",
302 "result" => ["\tfilename ", "", ""]);
303 setMatching([$rElem]);
310 sub checkForLatexCommand($)
314 if ($stack[0]->{type} eq "inset") {
315 if ($l =~ /^LatexCommand\s+([^\s]+)\s*$/) {
317 if ($stack[0]->{name} =~ /^CommandInset\s+bibtex$/) {
318 if ($param eq "bibtex") {
319 my $rElem1 = newMatch("ext" => ".bib",
320 "filetype" => "prefix_for_list",
321 "search" => qr/^bibfiles\s+\"(.+)\"/,
322 "result" => ["bibfiles \"", "1", "\""]);
323 my $rElem2 = newMatch("ext" => ".bst",
324 "filetype" => "prefix_for_list",
325 "search" => qr/^options\s+\"(.+)\"/,
326 "result" => ["options \"", "1", "\""]);
327 setMatching([$rElem1, $rElem2]);
330 elsif ($stack[0]->{name} =~ /^CommandInset\s+include$/) {
331 if ($param =~ /^(verbatiminput\*?|lstinputlisting|inputminted)$/) {
332 my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
333 "filetype" => "copy_only",
334 "result" => ["filename \"", "", "\""]);
335 setMatching([$rElem]);
337 elsif ($param =~ /^(include|input)$/) {
338 my $rElem = newMatch("search" => qr/^filename\s+\"(.+)\"/,
339 "filetype" => "interpret",
340 "result" => ["filename \"", "", "\""]);
341 setMatching([$rElem]);
350 # parse the given line
351 # returns a hash with folloving values
352 # found: 1 if line matched some regex
353 # fileidx: index into result
354 # ext: list of possible extensions to use for a valid file
355 # filelist: list of found file-pathes (may be more then one, e.g. in bibfiles spec)
356 # separator: to be used while concatenating the filenames
357 # filetype: prefix_only,replace_only,copy_only,interpret
358 # same as before, but without 'prefix_for_list'
363 return({"found" => 0}) if (checkForHeader($l));
364 return({"found" => 0}) if (checkForPreamble($l));
365 return({"found" => 0}) if (checkForEndBlock($l));
366 return({"found" => 0}) if (checkForLayoutStart($l));
367 return({"found" => 0}) if (checkForInsetStart($l));
368 return({"found" => 0}) if (checkForLatexCommand($l));
369 if (defined($stack[0])) {
370 my $rMatch = getMatching();
371 for my $m ( @{$rMatch}) {
372 my $search = getSearch($m);
374 my @matches = ($1, $2, $3, $4);
375 my $filetype = getFileType($m);
376 my @result2 = @{getResult($m)};
378 for my $r (@result2) {
383 if ($filetype eq "replace_only") {
385 my %result = ("found" => 1,
386 "filetype" => $filetype,
387 "result" => \@result2);
391 my $fileidx = getFileIdx($m);
392 my $filename = $matches[$fileidx-1];
393 if ($filename !~ /^\.*$/) {
394 my %result = ("found" => 1,
395 "fileidx" => $fileidx,
397 "result" => \@result2);
398 if ($filetype eq "prefix_for_list") {
399 # bibfiles|options in CommandInset bibtex
400 my @filenames = split(',', $filename);
401 $result{"separator"} = ",";
402 $result{"filelist"} = \@filenames;
403 $result{"filetype"} = "prefix_only";
406 $result{"separator"} = "";
407 $result{"filelist"} = [$filename];
408 $result{"filetype"} = $filetype;
416 return({"found" => 0});