home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / lyx21041.zip / XFree86 / lib / X11 / lyx / reLyX / LastLyX.pm (.txt) < prev    next >
LaTeX Document  |  1999-03-21  |  5KB  |  112 lines

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