]> git.lyx.org Git - lyx.git/blobdiff - lib/reLyX/MakePreamble.pm
clean up french language handling
[lyx.git] / lib / reLyX / MakePreamble.pm
index 7a5d6b045455aaf19d5248e307ca03c8c4f97ae9..4c16c294f581c7d680d9e4f711c80417cc7c3963 100644 (file)
@@ -1,4 +1,4 @@
-# This file is part of reLyX. 
+# This file is part of reLyX.
 # Copyright (c) 1998-9 Amir Karger karger@post.harvard.edu
 # You are free to use and modify this code under the terms of
 # the GNU General Public Licence version 2 or later.
@@ -26,8 +26,8 @@ sub split_preamble {
     $debug_on = (defined($main::opt_d) && $main::opt_d);
     # -c option?
     $true_class = defined($main::opt_c) ? $main::opt_c : "";
-    my $zzz = $debug_on 
-        ? " from LaTeX file $InFileName into $PreambleName and $OutFileName" 
+    my $zzz = $debug_on
+        ? " from LaTeX file $InFileName into $PreambleName and $OutFileName"
        : "";
     warn "Splitting Preamble$zzz\n";
 
@@ -95,9 +95,12 @@ sub translate_preamble {
        "letterpaper"    => "\\papersize letterpaper",
        "legalpaper"     => "\\papersize legalpaper",
        "executivepaper" => "\\papersize executivepaper",
+       "a3paper"        => "\\papersize a3paper",
        "a4paper"        => "\\papersize a4paper",
        "a5paper"        => "\\papersize a5paper",
-       "b5paper"        => "\\papersize b5paper",
+       "b3paper"        => "\\papersize b3paper",
+       "b4paper"        => "\\papersize b4paper",
+       "b5paper"        => "\\papersize b5paper",
 
        "twoside"        => "\\papersides 2",
        "oneside"        => "\\papersides 1",
@@ -108,6 +111,19 @@ sub translate_preamble {
        "twocolumn"      => "\\papercolumns 2",
     );
 
+    my %Languages_Table = ();
+    # if the language file is available then added it to the languages table
+    if(-e "$main::lyxdir/languages") {
+       open (LANGUAGES, "<$main::lyxdir/languages");
+
+       while(<LANGUAGES>) {
+           next if(/^\#/); #ignore comments
+           my @lang_field = split(/\s+/);
+           $Languages_Table{$lang_field[1]} = $lang_field[1];
+       }
+       close(LANGUAGES);
+    }
+
     # This is the string in which we're concatenating everything we translate
     my $LyX_Preamble = "";
 
@@ -131,15 +147,9 @@ sub translate_preamble {
 #      }
 #}
 
-    # Write first two lines of the lyx file
-    my ($nm, $dt);
-    # whoami is needed on Win32, because getlong/getpwuid aren't implemented
-    # I'd rather use internal Perl functions when possible, though
-    $nm = (eval {getlogin || getpwuid($<)}) || `whoami`;
-    $dt = localtime;
-    $LyX_Preamble .= "\#This file was created by <$nm> $dt\n";
-    $LyX_Preamble .= "\#LyX 1.0 (C) 1995-1999 Matthias Ettrich " .
-                    "and the LyX Team\n";
+    # Write first line of the lyx file
+    $LyX_Preamble .= "\# The reLyX bundled with LyX 1.3 created this file.\n" .
+       "# For more info see http://www.lyx.org/\n";
 
     # Print \lyxformat.
     $LyX_Preamble .= "\\lyxformat $Format\n";
@@ -155,7 +165,7 @@ sub translate_preamble {
     warn "Uncommented text before \\documentclass command ignored!\n"if $ignore;
     print "Ignored text was\n------\n$ignore------\n" if $debug_on && $ignore;
 
-    # concatenate all the extra options until the required argument to 
+    # concatenate all the extra options until the required argument to
     # \documentclass, which will be in braces
     until (eof(PREAMBLE) || /\{/) {
         my $instr = <PREAMBLE>;
@@ -178,7 +188,7 @@ sub translate_preamble {
 
     # Read the document class, in braces, then convert it to a textclass
     # However, if the user input a different class with the -c option, use that
-    s/\s*\{(\w+)\}//;
+    s/\s*\{(\S+)\s*\}//;
     my $class = $1;
     $class = $true_class if $true_class; # override from -c option
     die "no document class in file, no -c option given\n" unless $class;
@@ -195,6 +205,20 @@ sub translate_preamble {
            }
        }
        $extra_options =~ s/^,+|,+(?=,)|,+$//g; # extra commas
+       # Analyze further the extra options for languages, the last language
+       # is the document language, so the order matters.
+       my $language ="";
+       foreach $op (split(/\s*,\s*/,$extra_options)) {
+           if (exists $Languages_Table{$op}) {
+               $language = $op;
+               $extra_options =~ s/\b$op\b//;
+               print "Document language $op\n" if $debug_on;
+           }
+       }
+       if ($language) {
+           $LyX_Preamble .= "\\language $language\n";
+       }
+       $extra_options =~ s/^,+|,+(?=,)|,+$//g; # extra commas
     }
     # Convert any remaining options to an \options command
     if ($extra_options) {
@@ -206,22 +230,152 @@ sub translate_preamble {
     #     (unless there is no preamble)
     # Everything until the end of filehandle PREAMBLE is preamble matter
     my $Latex_Preamble = $_; # there COULD be a preamble command on same line
-    my $write_preamble = (! /^\s*$/ && ! /^\s*%/);
     while (<PREAMBLE>) {
        $Latex_Preamble .= $_;
-       # write an official preamble if we find anything that's not comment
-       $write_preamble ||= (! /^\s*$/ && ! /^\s*%/);
     }
 
-    if ($write_preamble) {
-       $Latex_Preamble =~ s/^\s*//;
-       print "LaTeX preamble, consists of:\n$Latex_Preamble" if $debug_on;
-       $Latex_Preamble = "\\begin_preamble\n$Latex_Preamble\\end_preamble\n";
-       print "End of LaTeX preamble\n" if $debug_on;
+    # Process $Latex_Preamble, and try to extract as much as possible to
+    # $Lyx_Preamble (jamatos 2001/07/21)
+
+    # Deal with counters, for now just "tocdepth" and "secnumdepth"
+    my %Counter_Table = (
+       "secnumdepth"   => "\\secnumdepth",
+       "tocdepth"      => "\\tocdepth"
+    );
+
+    my $ct;
+    foreach $ct (keys %Counter_Table) {
+       $Latex_Preamble =~ s/\\setcounter\{$ct\}\{(.*)\}\s*// && do {
+           $LyX_Preamble .= "$Counter_Table{$ct} $1\n";
+       }
+    }
+
+    if($Latex_Preamble =~ s/\\pagestyle\{(.*)\}\s*//) {
+       $LyX_Preamble .= "\\paperpagestyle $1\n";
+    }
+    if($Latex_Preamble =~ s/\\usepackage\[(.*)\]\{inputenc\}\s*//) {
+       $LyX_Preamble .= "\\inputencoding $1\n";
+    }
+    $Latex_Preamble =~ s/\\usepackage\[.*\]\{fontenc\}\s*//;
+
+    ## Deal with \usepackage{} cases, no optional argument
+    my %Usepackage_Table = (
+       "amsmath"       => "\\use_amsmath 1",
+       "amssymb"       => "",
+
+       "geometry"      => "\\use_geometry 1",
+
+       "babel"         => "",
+
+       "pslatex"       => "\\fontscheme pslatex",
+       "ae"            => "\\fontscheme ae",
+       "aecompl"       => "",
+       "times"         => "\\fontscheme times",
+       "palatino"      => "\\fontscheme palatino",
+       "helvet"        => "\\fontscheme helvet",
+       "avant"         => "\\fontscheme avant",
+       "newcent"       => "\\fontscheme newcent",
+       "bookman"       => "\\fontscheme bookman",
+
+       "a4wide"        => "\\paperpackage widemarginsa4",
+       "a4"            => "\\paperpackage a4wide",
+
+       "graphics"      => "\\graphics default",
+       "rotating"      => "",
+       "makeidx"       => ""
+    );
+
+    ## Babel with arguments specifing language
+    if($Latex_Preamble =~ s/\\usepackage\[(.*)\]\{babel\}\s*//) {
+       my @languages = split(',',$1);
+       my $lang = pop @languages;
+       $LyX_Preamble .= "\\language $lang\n";
+    }
+
+    my $up;
+    foreach $up (keys %Usepackage_Table) {
+       $Latex_Preamble =~ s/^\s*\\usepackage\{$up\}\s*// && do {
+           $LyX_Preamble .= "$Usepackage_Table{$up}";
+           $LyX_Preamble .= "\n" unless ($Usepackage_Table{$up} eq "");
+       }
+    }
+
+    # Natbib is a little more complex than that.
+    if ($Latex_Preamble =~ s/\\usepackage(.*)\{natbib\}\s*//) {
+       $LyX_Preamble .= "\\use_natbib 1\n\\use_numerical_citations ";
+       $LyX_Preamble .= ($1 =~ /numbers/) ? "1\n" : "0\n";
     } else {
-        $Latex_Preamble = ""; #just comments, whitespace. Ignore them
+       $LyX_Preamble .= "\\use_natbib 0\n\\use_numerical_citations 0\n";
+    }
+
+    ## Handle geometry options
+    ## The custom paper missing from the options list since it involves two parameters
+    my %Geometry_Options =(
+       'verbose'       => sub { return "" },
+
+       "letterpaper"   => sub { return "\\papersize letterpaper\n" },
+       "legalpaper"    => sub { return "\\papersize legalpaper\n" },
+       "executivepaper"=> sub { return "\\papersize executivepaper\n" },
+       "a3paper"       => sub { return "\\papersize a3paper\n" },
+       "a4paper"       => sub { return "\\papersize a4paper\n" },
+       "a5paper"       => sub { return "\\papersize a5paper\n" },
+       "b3paper"       => sub { return "\\papersize b3paper\n" },
+       "b4paper"       => sub { return "\\papersize b4paper\n" },
+       "b5paper"       => sub { return "\\papersize b5paper\n" },
+
+       'tmargin=(\w*)' => sub { return "\\topmargin $1\n" },
+       'bmargin=(\w*)' => sub { return "\\bottommargin $1\n" },
+       'lmargin=(\w*)' => sub { return "\\leftmargin $1\n" },
+       'rmargin=(\w*)' => sub { return "\\rightmargin $1\n" },
+       'headsep=(\w*)' => sub { return "\\headsep $1\n" },
+       'footskip=(\w*)'        => sub { return "\\footskip $1\n" },
+       'headheight=(\w*)'      => sub { return "\\headheight $1\n" }
+    );
+
+    if( $Latex_Preamble =~ /\\geometry\{(.*)\}/) {
+       my $geom_options = $1;
+       my $op;
+       foreach $op (keys %Geometry_Options) {
+           $geom_options =~ s/$op// && do {
+               $LyX_Preamble .= &{$Geometry_Options{$op}}();
+               print "Geometry option $op\n" if $debug_on;
+           }
+       }
+       $geom_options =~ s/^,+|,+(?=,)|,+$//g; # extra commas
+       if( $geom_options =~ /\s*/) {
+           $Latex_Preamble =~ s/\\geometry\{(.*)\}//;
+       }
+       else {
+           $Latex_Preamble =~ s/\\geometry\{(.*)\}/\\geometry\{$geom_options\}/;
+       }
+    }
+
+    ## Paragraph skip or indentation
+    if ( $Latex_Preamble =~
+        s/\\setlength\\parskip\{\\(.*)amount\}\s*\\setlength\\parindent\{0pt\}//) {
+       $LyX_Preamble .= "\\paragraph_separation skip\n";
+       $LyX_Preamble .= "\\defskip $1\n";
     }
 
+    ## Paragraph spacing
+    if ( $Latex_Preamble =~ s/\\(\w*)spacing//) {
+       $LyX_Preamble .= "\\spacing  $1\n";
+    }
+
+    ## remove LyX specific stuff
+    $Latex_Preamble =~ s/\\IfFileExists\{url.sty\}\{\\usepackage\{url\}\}\s*\{\\newcommand\{\\url\}\{\\texttt\}\}\s*//;
+
+    ##  this two need probably a more fine grained control
+    $Latex_Preamble =~ s/\\makeatletter\s*//;
+    $Latex_Preamble =~ s/\\makeatother\s*//;
+
+    $Latex_Preamble =~ s/^\s*//;
+    print "LaTeX preamble, consists of:\n$Latex_Preamble" if $debug_on;
+    if($Latex_Preamble) {
+       $Latex_Preamble = "\\begin_preamble\n$Latex_Preamble\\end_preamble\n";
+    } #just comments, whitespace. Ignore them
+    print "End of LaTeX preamble\n" if $debug_on;
+
     #warn "Done creating LyX Preamble\n";
     return ($class, $LyX_Preamble, $Latex_Preamble);
 }