Diff for /loncom/xml/londefdef.pm between versions 1.184 and 1.199

version 1.184, 2003/10/21 13:36:32 version 1.199, 2004/02/19 19:57:48
Line 50  use Apache::File(); Line 50  use Apache::File();
 use Image::Magick;  use Image::Magick;
 use Apache::lonmenu();  use Apache::lonmenu();
 use Apache::lonmeta();  use Apache::lonmeta();
   use Apache::Constants qw(:common);
   
 $Apache::londefdef::TD_redirection=0;  
   
 BEGIN {  BEGIN {
   
Line 59  BEGIN { Line 59  BEGIN {
   
 }  }
   
   sub initialize_londefdef {
       $Apache::londefdef::TD_redirection=0;
       @Apache::londefdef::table = ();
       $Apache::londefdef::select=0;
       @Apache::londefdef::description=();
       $Apache::londefdef::DD_redirection=0;
       $Apache::londefdef::DT_redirection=0;
   }
   
 #======================= TAG SUBROUTINES =====================  #======================= TAG SUBROUTINES =====================
 #-- <output>  #-- <output>
 sub start_output {  sub start_output {
Line 73  sub end_output { Line 82  sub end_output {
 }  }
 #-- <m> tag  #-- <m> tag
 sub start_m {  sub start_m {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
     my $currentstring = '';      my $currentstring = '';
     if ($target eq 'web') {      my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
       if ($target eq 'web' || $target eq 'analyze') {
  $Apache::lonxml::prevent_entity_encode++;   $Apache::lonxml::prevent_entity_encode++;
  my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);  
  $inside ='\\documentstyle{article}'.$inside;   $inside ='\\documentstyle{article}'.$inside;
  &Apache::lonxml::debug("M is starting with:$inside:");   &Apache::lonxml::debug("M is starting with:$inside:");
  my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);   my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
Line 94  sub start_m { Line 103  sub start_m {
  #&Apache::lonxml::debug("M is ends with:$currentstring:");   #&Apache::lonxml::debug("M is ends with:$currentstring:");
  $Apache::lonxml::post_evaluate=0;   $Apache::lonxml::post_evaluate=0;
     } elsif ($target eq 'tex') {      } elsif ($target eq 'tex') {
  $currentstring = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);   $currentstring = $inside;
  my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);   my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
  if ($eval eq 'on') {   if ($eval eq 'on') {
     $currentstring=&Apache::run::evaluate($currentstring,$safeeval,$$parstack[-1]);      $currentstring=&Apache::run::evaluate($currentstring,$safeeval,$$parstack[-1]);
  }   }
  if ($currentstring=~/^(\s*\\\\\s*)*$/) {$currentstring = ' \vskip 0 mm ';}   if ($currentstring=~/^(\s*\\\\\s*)*$/) {$currentstring = ' \vskip 0 mm ';}
  $Apache::lonxml::post_evaluate=0;   $Apache::lonxml::post_evaluate=0;
     } else {  
  my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);  
     }      }
     return $currentstring;      return $currentstring;
 }  }
Line 144  sub end_tthoption { Line 151  sub end_tthoption {
 sub start_html {  sub start_html {
     my ($target,$token) = @_;      my ($target,$token) = @_;
     my $currentstring = '';      my $currentstring = '';
     if ($ENV{'browser.mathml'}) {      my $options=$ENV{'course.'.$ENV{'request.course.id'}.'.tthoptions'};
  &tth::ttminit();      &Apache::lontexconvert::init_tth();
  if ($ENV{'browser.unicode'}) {      if ($target eq 'web' || $target eq 'edit') {
     &tth::ttmoptions('-L -u1');  
  } else {  
     &tth::ttmoptions('-L -u0');  
  }  
     } else {  
  &tth::tthinit();  
  if ($ENV{'browser.unicode'}) {  
     &tth::tthoptions('-L -u1');  
  } else {  
     &tth::tthoptions('-L -u0');  
  }  
     }  
     if ($target eq 'web') {  
  $currentstring = &Apache::lonxml::xmlbegin().   $currentstring = &Apache::lonxml::xmlbegin().
     &Apache::lonxml::fontsettings();           &Apache::lonxml::fontsettings();     
     } elsif ($target eq 'tex') {      } elsif ($target eq 'tex') {
Line 344  sub start_title { Line 338  sub start_title {
     }      }
     if ($target eq 'meta') {      if ($target eq 'meta') {
  $currentstring='<title>';   $currentstring='<title>';
  &start_output();   &start_output($target);
     }      }
     return $currentstring;      return $currentstring;
 }  }
Line 358  sub end_title { Line 352  sub end_title {
  $currentstring .= '}';   $currentstring .= '}';
     }        }  
     if ($target eq 'meta') {      if ($target eq 'meta') {
  &end_output();   &end_output($target);
  $currentstring='</title>';   $currentstring='</title>';
     }       } 
     return $currentstring;      return $currentstring;
Line 641  sub start_h1 { Line 635  sub start_h1 {
  $currentstring .= $token->[4];   $currentstring .= $token->[4];
     } elsif ($target eq 'tex') {      } elsif ($target eq 'tex') {
  my $pre;   my $pre;
  my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);   my $align=lc(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1));
  if (($align eq 'center') || (not defined $align)) {   if (($align eq 'center') || (not defined $align)) {
     $pre='\begin{center}';      $pre='\begin{center}';
  } elsif ($align eq 'left') {   } elsif ($align eq 'left') {
Line 654  sub start_h1 { Line 648  sub start_h1 {
  $currentstring .= $pre.'{\\'.$TeXsize.' \textbf{';    $currentstring .= $pre.'{\\'.$TeXsize.' \textbf{'; 
     } elsif ($target eq 'meta') {      } elsif ($target eq 'meta') {
  $currentstring='<subject>';   $currentstring='<subject>';
  &start_output();   &start_output($target);
     }      }
     return $currentstring;      return $currentstring;
 }  }
Line 676  sub end_h1 { Line 670  sub end_h1 {
  }   }
  $currentstring .= '}}'.$post;   $currentstring .= '}}'.$post;
     } elsif ($target eq 'meta') {      } elsif ($target eq 'meta') {
  &end_output();   &end_output($target);
  $currentstring='</subject>';   $currentstring='</subject>';
     }       } 
     return $currentstring;      return $currentstring;
Line 1105  sub end_q { Line 1099  sub end_q {
 }  }
   
 #-- <p> tag (end tag optional)  #-- <p> tag (end tag optional)
   #optional attribute - align="center|left|right"
 sub start_p {  sub start_p {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;      my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';      my $currentstring = '';
     if ($target eq 'web') {      if ($target eq 'web') {
  $currentstring .= $token->[4];   $currentstring .= $token->[4];
     } elsif ($target eq 'tex') {      } elsif ($target eq 'tex') {
  my $signal=1;   my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}}   if ($align eq 'center') {
  if ($signal) {$currentstring .= '\par ';}      $currentstring='\begin{center}\par';
    } elsif ($align eq 'right') {
       $currentstring='\makebox['.$ENV{'form.textwidth'}.']{\hfill\llap{';
    } elsif ($align eq 'left') {
       $currentstring='\noindent\makebox['.$ENV{'form.textwidth'}.']{\rlap{';
    }
    my $signal=1;#<p> does not work inside <b>...</b> 
    foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}
    if (!$signal) {$currentstring = '';}
    }
     }      }
     return $currentstring;      return $currentstring;
 }  }
   
 sub end_p {  sub end_p {
     my ($target,$token) = @_;      my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';      my $currentstring = '';
     if ($target eq 'web') {      if ($target eq 'web') {
  $currentstring .= $token->[2];   $currentstring .= $token->[2];
       } elsif ($target eq 'tex') {
    if ($$tagstack[-1] eq 'p') {
       my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
       if ($align eq 'center') {
    $currentstring .= '\end{center}';
       } elsif ($align eq 'right') {
    $currentstring .= '}}';
       } elsif ($align eq 'left') {
    $currentstring .= '}\hfill}';
       }
    }
     }      }
     return $currentstring;      return $currentstring;
 }  }
Line 2084  sub start_img { Line 2099  sub start_img {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;      my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,      my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
  undef,1);   undef,1);
       if (not $src and ($target eq 'web' or $target eq 'tex')) { 
    my $inside = &Apache::lonxml::get_all_text("/img",$parser);
    &Apache::lonnet::logthis("inside was $inside");
    return '';
       }
     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$src;      $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$src;
     my $currentstring = '';      my $currentstring = '';
     my $width_param = '';  
     my $height_param = '';  
     my $scaling = .3;      my $scaling = .3;
     if ($target eq 'web') {      if ($target eq 'web') {
  if ($ENV{'browser.imagesuppress'} ne 'on') {   if ($ENV{'browser.imagesuppress'} ne 'on') {
Line 2103  sub start_img { Line 2121  sub start_img {
  }   }
     } elsif ($target eq 'tex') {      } elsif ($target eq 'tex') {
  $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);   $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
  &image_replication($src);  
  #if original gif/jpg/png file exist do following:   #if original gif/jpg/png file exist do following:
  if (-e $src) {             if (-e $src) {
     #defines the default size of image      #what is the image size?
     my $image = Image::Magick->new;      my $width_param=&image_size($src,$scaling,$parstack,$safeeval);
     my $current_figure = $image->Read($src);              my ($file,$path)=&file_path($src); 
     $width_param = $image->Get('width') * $scaling;;  
     $height_param = $image->Get('height') * $scaling;;  
     undef $image;  
     #do we have any specified size of the picture?  
     my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);  
     my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);  
     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,  
    undef,1);  
     if ($TeXwidth ne '') {    
  if ($TeXwidth=~/(\d+)\s*\%/) {  
     $width_param = $1*$ENV{'form.textwidth'}/100;  
  } else {   
     $width_param = $TeXwidth;  
  }  
     } elsif ($TeXheight ne '') {  
  $width_param = $TeXheight/$height_param*$width_param;  
     } elsif ($width ne '') {  
  $width_param = $width*$scaling;        
     }  
     if ($width_param > $ENV{'form.textwidth'}) {$width_param =0.95*$ENV{'form.textwidth'}}  
     my $file;  
     my $path;  
     if ($src =~ m!(.*)/([^/]*)$!) {  
  $file = $2;   
  $path = $1.'/';   
     }   
     my $newsrc = $src;      my $newsrc = $src;
     $newsrc =~ s/\.(gif|jpg|png)$/.eps/i;      $newsrc =~ s/\.(gif|jpg|png)$/.eps/i;
     $file=~s/\.(gif|jpg|png)$/.eps/i;      $file=~s/\.(gif|jpg|png)$/.eps/i;
Line 2154  sub start_img { Line 2145  sub start_img {
  $currentstring .= '\vskip 1 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';   $currentstring .= '\vskip 1 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
     }      }
  } else {   } else {
     #there aren't eps or ps - so create eps       #care about eps dynamical generation
     my $temp_file;      $currentstring.='\vskip 1 mm '.&eps_generation($src,$file,$width_param);
     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";  
     $temp_file = Apache::File->new('>>'.$filename);   
     print $temp_file "$src\n";  
     $newsrc=~s/\/home\/httpd\/html\/res//;  
     $newsrc=~s/\/([^\/]+)\.(ps|eps)/\//;  
     $currentstring .= '\vskip 1 mm \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';  
  }   }
     }      }
  } else {   } else {
Line 2178  sub start_img { Line 2163  sub start_img {
  #<allow> tag will care about replication    #<allow> tag will care about replication 
     }      }
  }   }
       } elsif ($target eq 'edit') {
    $currentstring .=&Apache::edit::tag_start($target,$token);
    $currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
       &Apache::edit::browse('src',undef,'alt').' '.
       &Apache::edit::search('src',undef,'alt').'<br />';
    $currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
    $currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
    $currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
    $currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
    $currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
    $currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
    my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval);
    my $alt=&Apache::lonxml::get_param('alt',$parstack,$safeeval);
    my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
    my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
    $currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
    if ($width) { $currentstring.=' width="'.$width.'" '; }
    if ($height) { $currentstring.=' height="'.$height.'" '; }
    $currentstring .= ' />';
       } elsif ($target eq 'modified') {
    my $constructtag=&Apache::edit::get_new_args($token,$parstack,
        $safeeval,'src','alt',
        'TeXwidth','TeXheight',
        'width','height');
    $src=$token->[2]{'src'};
    if (!$token->[2]{'width'} && !$token->[2]{'height'}) {
       $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
       &image_replication($src);
       if (-e $src) {
    my $image = Image::Magick->new;
    my ($width, $height, $size, $format) = $image->Ping($src);
    if ($width && $height) {
       $token->[2]{'width'} =$width;
       $token->[2]{'height'}=$height;
       $constructtag=1;
    }
       }
    }
    if ($constructtag) {$currentstring=&Apache::edit::rebuild_tag($token);}
     }      }
     return $currentstring;      return $currentstring;
 }  }
Line 3294  sub end_hideweboutput { Line 3318  sub end_hideweboutput {
   
 sub image_replication {  sub image_replication {
     my $src = shift;      my $src = shift;
     if (not -e $src) {      if (not -e $src) { &Apache::lonnet::repcopy($src); }
  #replicates image itself      #replicates eps or ps 
  &Apache::lonnet::repcopy($src);      my $epssrc = my $pssrc = $src;
  #replicates eps or ps       $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
  my $newsrc = $src;      $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
  $newsrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;      if (not -e $epssrc && not -e $pssrc) {
  if (not -e $newsrc) {   my $result=&Apache::lonnet::repcopy($epssrc);
     if (&Apache::lonnet::repcopy($newsrc) ne 'OK') {   if ($result ne OK) { &Apache::lonnet::repcopy($pssrc); }
  $newsrc =~ s/\.eps$/\.ps/;  
  &Apache::lonnet::repcopy($newsrc);  
     }  
  }  
     }      }
     return '';      return '';
 }  }
   
   sub image_size {
       my ($src,$scaling,$parstack,$safeeval)=@_;
       #size of image from gif/jpg/jpeg/png 
       my $image = Image::Magick->new;
       my $current_figure = $image->Read($src);
       my $width_param = $image->Get('width') * $scaling;;
       my $height_param = $image->Get('height') * $scaling;;
       undef $image;
       #do we have any specified LaTeX size of the picture?
       my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
       my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
       #do we have any specified web size of the picture?
       my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
      undef,1);
       if ($TeXwidth ne '') {  
    if ($TeXwidth=~/(\d+)\s*\%/) {
       $width_param = $1*$ENV{'form.textwidth'}/100;
    } else { 
       $width_param = $TeXwidth;
    }
       } elsif ($TeXheight ne '') {
    $width_param = $TeXheight/$height_param*$width_param;
       } elsif ($width ne '') {
    $width_param = $width*$scaling;      
       }
       if ($width_param > $ENV{'form.textwidth'}) {$width_param =0.95*$ENV{'form.textwidth'}}
       return $width_param;
   }
   
   sub eps_generation {
       my ($src,$file,$width_param) = @_;     
       my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
       my $temp_file = Apache::File->new('>>'.$filename); 
       print $temp_file "$src\n";
       my $newsrc = $src;
       $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
       $newsrc=~s/\/home\/httpd\/html\/res//;
       $newsrc=~s/\/home\/([^\/]*)\/public_html\//\/$1\//;
       $newsrc=~s/\/\.\//\//;
       $newsrc=~s/\/([^\/]+)\.(ps|eps)/\//;
       return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
   }
   
   sub file_path {     
       my $src=shift;
       my ($file,$path); 
       if ($src =~ m!(.*)/([^/]*)$!) {
    $file = $2; 
    $path = $1.'/'; 
       } 
       return $file,$path;
   }
   
 sub recalc {  sub recalc {
     my $argument = shift;      my $argument = shift;
     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}      if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}

Removed from v.1.184  
changed lines
  Added in v.1.199


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>