]> git.lyx.org Git - lyx.git/blobdiff - lib/reLyX/BasicLyX.pm
The Box patch
[lyx.git] / lib / reLyX / BasicLyX.pm
index d704c34db07ddf31df12b3a4e62b7cc7674015a6..d5ddc5c1fa6f91805a5a9f932bce6531affd88d8 100644 (file)
@@ -352,6 +352,60 @@ sub ending_math {
     return 0;
 }
 
+
+sub regularizeLatexLength {
+    my $LatexLength = shift;
+
+    # Remove any whitespace
+    $LatexLength =~ s/\s//g;
+    # Remove a leading '+' as unnecessary
+    $LatexLength =~ s/^\+?(\d)/$1/;
+    # Split into value and unit parts
+    my $val;
+    my $unit;
+    if ($LatexLength =~ /(-?\d+[.,]?\d*)(\\?[a-zA-Z]*)$/) {
+       $val  = $1;
+       $unit = $2;
+    }
+    # If the input is invalid, return what we have.
+    return $LatexLength if ($val eq '' || $unit eq '');
+
+    # '4,5' is a valid LaTeX number. Change it to '4.5'
+    $val =~ s/,/./;
+    # If the unit is not a LaTeX macro, then ensure it is lower case
+    if (!($unit =~ /^\\/)) {
+       $unit =~ s/([a-z]*)/\L$1/i;
+    }
+
+    $LatexLength = $val . $unit;
+    return $LatexLength;
+}
+    
+
+sub getAsLyXLength {
+    # Straight translation of LaTeX lengths to LyX ones.
+    my %lengthAsLyXString = ('\textwidth'   => 'text%',
+                            '\columnwidth' => 'col%',
+                            '\paperwidth'  => 'page%',
+                            '\linewidth'   => 'line%',
+                            '\paperheight' => 'pheight%',
+                            '\textheight'  => 'theight%');
+
+    my $LatexLength = shift;
+    $LatexLength = regularizeLatexLength($LatexLength);
+
+    my $LyXLength = $LatexLength;
+    # If $LatexLength is something like '4.5\columnwidth', translate into
+    # LyXese.
+    if ($LatexLength =~ /([+-]?\d+\.?\d*)(\\[a-z]*)/) {
+       if (defined($lengthAsLyXString{$2})) {
+           $LyXLength = ($1 * 100) . $lengthAsLyXString{$2};
+       }
+    }
+
+    return $LyXLength;
+}
+
 ##########################   MAIN TRANSLATOR SUBROUTINE   #####################
 sub basic_lyx {
 # This subroutine is called by Text::TeX::process each time subroutine
@@ -1011,6 +1065,55 @@ sub basic_lyx {
                $tok = $fileobject->eatGroup;
                new RelyxTable::Table $tok;
 
+           # minipage
+           } elsif ($env eq "minipage") {
+               &CheckForNewParagraph;
+
+               print OUTFILE "\\begin_inset Minipage\n";
+
+               # The minipage environment is defined as:
+               # \begin{minipage}[pos][height][inner-pos]{width} <text>
+               # \end{minipage}
+
+               # Read the position optional argument, if it exists
+               $tok = $fileobject->eatOptionalArgument;
+               my $pos = $tok->print if defined($tok->print);
+
+               my %map = ('t' => '0', 'c' => '1', 'b' => '2');
+               if ($debug_on && $pos ne '' && !defined($map{$pos})) {
+                   print "\nIgnoring unknown positioning arg '$pos'\n";
+               }
+
+               # The minipage is centred by default.
+               $pos = '1' if (!defined($map{$pos}) ||
+                                 ($pos = $map{$pos}) eq '');
+
+               # Read the height optional argument, if it exists
+               my $height = '0pt';
+               $tok = $fileobject->eatOptionalArgument;
+               if (defined($tok->print)) {
+                   $height = getAsLyXLength($tok->print);
+               }
+
+               # Read the inner-pos optional argument, if it exists
+               my $innerpos = $pos;
+               $tok = $fileobject->eatOptionalArgument;
+               if (defined($tok->print)) {
+                   my $arg = $tok->print;
+                   print("\nMinipage inner-pos argument, $arg, is ",
+                         "currently ignored\n");
+               }
+
+               # Read the width as (a reference to) an array of tokens.
+               $tok = $fileobject->eatBalanced;
+               my $width = getAsLyXLength($tok->exact_print);
+
+               print OUTFILE "position $pos\n";
+               print OUTFILE "inner_position $innerpos\n";
+               print OUTFILE "height \"$height\"\n";
+               print OUTFILE "width \"$width\"\n";
+               print OUTFILE "collapsed false\n";
+
            # \begin document
            } elsif ($env eq "document") {
                # do nothing
@@ -1082,6 +1185,13 @@ sub basic_lyx {
                # Anything after a table will be a new paragraph
                $IsNewParagraph = 1; $MayBeDeeper = 1;
 
+           # minipage
+           } elsif ($env eq "minipage") {
+               print OUTFILE "\n\\end_inset \n\n";
+
+               # Next stuff will be new env.
+               # $IsNewParagraph = 1;
+
            } elsif ($env eq "document") {
                print "\nDone with document!" if $debug_on;