]> git.lyx.org Git - lyx.git/blob - lib/reLyX/LastLyX.pm
fix compatability reading of floats
[lyx.git] / lib / reLyX / LastLyX.pm
1 # This file is part of reLyX
2 # Copyright (c) 1998-9 Amir Karger karger@post.harvard.edu
3 # You are free to use and modify this code under the terms of
4 # the GNU General Public Licence version 2 or later.
5
6 package LastLyX;
7 # This package is the last step in creating a LyX file. It:
8 #  - rejoins the lyx preamble to the rest of the file
9 #  - adds some information (see below)
10 #  - determines whether reLyX needs to translate any other files (e.g.,
11 #    files included in an \include command)
12 #  - creates a '.lyx' file
13 #
14 # reLyX may not have enough information during the previous pass. In that case,
15 # it puts a "marker" in the temporary file it writes, and stores the missing
16 # information---when it does come upon it---in a global variable.
17 # So during this pass, if we see any such markers, we replace them with the
18 # necessary information. Examples of this include:
19 # - header information for tables
20 # - name of the bibliography style file to use
21
22 use strict;
23 use RelyxTable; # handle LaTeX tables
24 use File::Basename;
25
26 my $debug_on; # was -d option given?
27
28 sub last_lyx {
29 # Arg0 is input file name
30 # Arg1 is output file name (foo.lyx)
31 # Arg2 is a string containing the entire preamble
32
33     my ($InFileName, $OutFileName, $LyXPreamble) = (shift, shift, shift);
34     $debug_on = (defined($main::opt_d) && $main::opt_d);
35     my $zzz=$debug_on ? " LyX file ($InFileName --> $OutFileName)\n" :"... ";
36     print STDERR "Writing$zzz";
37     open (INFILE, "<$InFileName") or die "problem opening $InFileName: $!\n";
38     open (OUTFILE,">$OutFileName") or die "problem opening $OutFileName: $!\n";
39
40     # Print the preamble
41     print OUTFILE $LyXPreamble;
42
43     # Now print out the rest of the LyX file
44     # Some lines have to be changed somewhat
45     # Otherwise just print all lines as they appear.
46     #    TODO In the future, we could buffer text, and then get rid of extra
47     # '\latex default \latex latex' or '\end_deeper \begin_deeper' pieces
48     # created by the translator
49     while (<INFILE>) {
50         if (/$RelyxTable::TableBeginString/o) {
51             # Write out the header information for the table
52             $_ = &print_table;
53
54         } elsif (/$BasicLyX::bibstyle_insert_string/o) {
55             # Replace the "insert bibstyle file here" with the actual file name
56             
57             my $ins = $BasicLyX::bibstyle_insert_string;
58             my $fil = $BasicLyX::bibstyle_file;
59             if ($fil) {
60                 s/$ins/$fil/;
61             } else {
62                 warn("Don't know which bibliographystyle file to use!\n".
63                  "Replace '$ins' in the LyX file with the bibstyle file\n");
64             }
65
66         } elsif (/^\Q$BasicLyX::Begin_Inset_Include\E/o) {
67             # tell main:: we need to translate an included (or inputted) file
68             m/\{(.*)\}\s*$/ or warn "weird Include command $_";
69             my $fil = $1;
70             # Change relative path to absolute path if necessary
71             my $abs_fil = &main::abs_file_name($fil);
72             print "Adding $abs_fil to file list\n" if $debug_on;
73             push @main::File_List, $abs_fil;
74
75             # include file.lyx, not file.tex!
76             my ($basename, $path, $suffix)=fileparse($fil, @main::Suffix_List);
77             $suffix = "" unless defined $suffix;
78             $path .= '/' unless $path =~ /\/$/;
79             my $newfile;
80             if ($main::opt_o) { # all files go to outputdir; no path nec.
81                 $newfile = "$basename.lyx";
82             } else { # keep relative path, e.g. Just change/add suffix
83                 ($newfile = $fil) =~ s/$suffix$/.lyx/;
84             }
85             s/\Q{$fil}\E/{$newfile}/;
86         } # end special if for table, bibstyle, include
87
88         print OUTFILE $_;
89     }
90
91     close INFILE; close OUTFILE;
92     #warn "Done writing LyX file!\n";
93 } # end sub last_lyx
94
95 sub print_table {
96 # Print a table, from TableBeginString to TableEndString
97 # Also (kind of a hack) remove the last \newline in a table, if any,
98 #    since it causes LyX to seg fault.
99     my $to_print=""; # string to collect the table in
100     my $thistable = shift(@RelyxTable::table_array);
101
102     # Collect the whole table in $to_print
103     my $line;
104     while (($line = <INFILE>) !~ /$RelyxTable::TableEndString/o) {
105         $to_print .= $line;
106     }
107
108     # Remove the last \newline, if it was created by a \\ \hline
109     # at the end of a table. (Note: according to Lamport, \\ at the end of
110     # a table is *illegal* unless followed by an \hline)
111     #    If it was created due to an empty cell at the end of the table, though
112     # (latex table "a & b \\ c &", e.g.) then we need to keep it!
113     # HACK HACK HACK. If it's a one-column table, then we couldn't
114     # know while reading the table that the last row was empty, so we
115     # couldn't pop the last row then. So do it now. Yuck.
116     if ($thistable->numcols==1) {
117         $to_print =~ s/\\newline(?=\s*$)// && pop @{$thistable->{"rows"}} 
118     } elsif ($thistable->{"rows"}[$thistable->numrows -1]->{"bottom_line"}) {
119         $to_print =~ s/\\newline(?=\s*$)//;
120     }
121
122     # Put the header information at the top
123     # We have to do this *after* reading the table because of the
124     # one-column table hack. Yuck.
125     $to_print = $thistable->print_info . $to_print;
126
127     return $to_print;
128 } # end sub print_table
129
130 1; # return true to main package