--- loncom/xml/londefdef.pm 2006/03/29 22:41:39 1.325 +++ loncom/xml/londefdef.pm 2006/11/07 02:39:39 1.344 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Tags Default Definition Module # -# $Id: londefdef.pm,v 1.325 2006/03/29 22:41:39 albertel Exp $ +# $Id: londefdef.pm,v 1.344 2006/11/07 02:39:39 albertel Exp $ # # # Copyright Michigan State University Board of Trustees @@ -79,6 +79,8 @@ sub initialize_londefdef { @Apache::londefdef::DT=(0); @Apache::londefdef::seenDT=(0); $Apache::londefdef::list_index=0; + undef($Apache::londefdef::head); + undef($Apache::londefdef::title); } #======================= TAG SUBROUTINES ===================== @@ -178,12 +180,11 @@ sub end_tthoption { sub start_html { my ($target,$token) = @_; my $currentstring = ''; - my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'}; - &Apache::lontexconvert::init_tth(); if ($target eq 'web' || $target eq 'edit' || $target eq 'webgrade' ) { - $currentstring = &Apache::lonxml::xmlbegin(); + # start_body() takes care of emitting the } elsif ($target eq 'tex') { - $currentstring .= '\documentclass[letterpaper,twoside]{article}'; + $currentstring .= + '\documentclass[letterpaper,twoside]{article}\raggedbottom'; if (($env{'form.latex_type'}=~'batchmode') || (!$env{'request.role.adv'})) {$currentstring .='\batchmode';} $currentstring .= '\newcommand{\keephidden}[1]{}'. @@ -212,7 +213,7 @@ sub end_html { my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_; my $currentstring = ''; if ($target eq 'web' || $target eq 'webgrade') { - $currentstring = ''; + # end_body takes care of the } return $currentstring; } @@ -222,7 +223,7 @@ sub start_head { my ($target,$token) = @_; my $currentstring = ''; if ($target eq 'web' || $target eq 'webgrade') { - $currentstring = $token->[4].&Apache::lonxml::fontsettings(); + &Apache::lonxml::startredirection(); } return $currentstring; } @@ -232,8 +233,10 @@ sub end_head { my $currentstring = ''; if (($target eq 'web' && $env{'request.state'} eq 'published') || ($target eq 'webgrade' && $env{'request.state'} eq 'published')) { - $currentstring = &Apache::lonmenu::registerurl(undef,$target). - $token->[2]; + # in case there is a but no + if ($Apache::lonxml::redirection) { + $Apache::londefdef::head = &Apache::lonxml::endredirection(); + } } return $currentstring; } @@ -365,10 +368,11 @@ sub end_form { #-- tag (end tag required) sub start_title { - my ($target,$token) = @_; + my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_; my $currentstring = ''; if ($target eq 'web' || $target eq 'webgrade') { - $currentstring = $token->[4]; + $Apache::londefdef::title = + &Apache::lonxml::get_all_text('/title',$parser,$style); } elsif ($target eq 'tex') { $currentstring .= '\keephidden{Title of the document: ' } @@ -383,7 +387,7 @@ sub end_title { my ($target,$token) = @_; my $currentstring = ''; if ($target eq 'web' || $target eq 'webgrade') { - $currentstring = $token->[2]; + # start_title takes care of swallowing the title } elsif ($target eq 'tex') { $currentstring .= '}'; } @@ -518,74 +522,25 @@ sub start_body { &Apache::lonxml::warning("<body> tag found inside of <problem> tag this can cause problems."); return ''; } - if (!$Apache::lonxml::registered && - $env{'request.state'} eq 'published') { - $currentstring.='<head>'. - &Apache::lonmenu::registerurl(undef,$target).'</head>'; - } -# Accessibility - if ($env{'browser.imagesuppress'} eq 'on') { - delete($token->[2]->{'background'}); - } - if ($env{'browser.fontenhance'} eq 'on') { - my $style=''; - foreach my $key (keys(%{$token->[2]})) { - if ($key =~ /^style$/i) { - $style.=$token->[2]->{$key}.';'; - delete($token->[2]->{$key}); - } - } - $token->[2]->{'style'}=$style.'; font-size: x-large;'; - } - if ($env{'browser.blackwhite'} eq 'on') { - delete($token->[2]->{'font'}); - delete($token->[2]->{'link'}); - delete($token->[2]->{'alink'}); - delete($token->[2]->{'vlink'}); - delete($token->[2]->{'bgcolor'}); - delete($token->[2]->{'background'}); - } -# Overload loads - my $onLoad=''; - foreach my $key (keys(%{$token->[2]})) { - if ($key =~ /^onload$/i) { - $onLoad.=$token->[2]->{$key}.';'; - delete($token->[2]->{$key}); - } - } - $token->[2]->{'onload'}=&Apache::lonmenu::loadevents().';'.$onLoad; - my $onUnload=''; - foreach my $key (keys(%{$token->[2]})) { - if ($key =~ /^onunload$/i) { - $onUnload.=$token->[2]->{$key}.';'; - delete($token->[2]->{$key}); - } - } - $token->[2]->{'onunload'}=&Apache::lonmenu::unloadevents(). - ';'.$onUnload; - $currentstring .= '<'.$token->[1]; - foreach (keys %{$token->[2]}) { - $currentstring.=' '.$_.'="'.$token->[2]->{$_}.'"'; - } - $currentstring.='>'; - &Apache::lontexconvert::jsMath_reset(); - if ($env{'environment.texengine'} eq 'jsMath') { - $currentstring.=&Apache::lontexconvert::jsMath_header(); + if (&is_inside_of($tagstack, "head")) { + &end_head(@_); } + $currentstring = + &Apache::loncommon::start_page($Apache::londefdef::title, + $Apache::londefdef::head, + {'add_entries' => $token->[2], + 'no_title' => 1, + 'force_register' => 1}); + if ($env{'request.state'} ne 'published') { - if ($env{'environment.remote'} eq 'off') { - $currentstring.= - &Apache::lonmenu::constspaceform(). - &Apache::lonmenu::menubuttons(1,'web',1); - } + $currentstring.=&Apache::lonmenu::constspaceform(); $currentstring.=(<<EDITBUTTON); <form method="post"> <input type="submit" name="editmode" accesskey="e" value="Edit" /> </form> +<br /> EDITBUTTON - } else { - $currentstring.=&Apache::lonmenu::menubuttons(undef,$target,1); } $currentstring.=&Apache::lonxml::message_location(); } elsif ($target eq 'tex') { @@ -598,7 +553,7 @@ sub end_body { my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_; my $currentstring = &end_p(); # Close off unclosed <p> if ($target eq 'web' || $target eq 'webgrade') { - $currentstring .= &Apache::lonxml::xmlend($target,$parser); + $currentstring .= &Apache::loncommon::end_page({'discussion' => 1}); } elsif ($target eq 'tex') { $currentstring .= '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}'; } @@ -606,8 +561,12 @@ sub end_body { } # \begin{center} causes a new paragprah spacing that looks odd inside -# of a table cell -sub center_correction { return '\vspace*{-6 mm}'; } +# of a table cell. Same at the end of a \center but with a slightly +# larger space .. hence center_correction and center_end_correction. +# +sub center_correction { return '\vspace*{-6 mm}'; } +sub center_end_correction { return '\vspace*{-7 mm}'; } + #-- <center> tag (end tag required) sub start_center { my ($target,$token,$tagstack) = @_; @@ -630,6 +589,9 @@ sub end_center { $currentstring = $token->[2]; } elsif ($target eq 'tex') { $currentstring = '\end{center}'; + if (&is_inside_of($tagstack, "table")) { + $currentstring .= ¢er_end_correction(); + } } return $currentstring; } @@ -1199,7 +1161,7 @@ sub start_p { $currentstring .= &end_p(); # close off prior para if in progress. my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1); if ($align eq 'center') { - $currentstring .='\begin{center}\par'; + $currentstring .='\begin{center}\par '; $closing_string = '\end{center}'; if (&is_inside_of($tagstack, "table")) { $currentstring = ¢er_correction().$currentstring; @@ -1255,16 +1217,19 @@ sub start_br { # for (my $i=$#tempo;$i>=0;$i--) { if (($tempo[$i] eq 'b') || ($tempo[$i] eq 'strong') || - ($tempo[$i] eq 'ol') || ($tempo[$i] eq 'ul') || - ($tempo[$i] eq 'td') || ($tempo[$i] eq 'th')) { + ($tempo[$i] eq 'ol') || ($tempo[$i] eq 'ul')) { $signal=1; - last; + } + if (($tempo[$i] eq 'td') || ($tempo[$i] eq 'th')) { + $signal = 1; } } - if ($signal) { + if ($signal eq 1) { $currentstring .= ' \vskip 0 mm '; } elsif ($$tagstack[-2] ne 'sub' && $$tagstack[-2] ne 'sup') { $currentstring .= '\strut \\\\ \strut '; + } else { # Honor break in simple <sup></sup> + $currentstring .= '}} \strut \\\\ \strut \ensuremath{^{'; } } return $currentstring; @@ -1951,8 +1916,7 @@ sub start_table { if ($target eq 'web' || $target eq 'webgrade') { $currentstring .= $token->[4]; } elsif ($target eq 'tex') { - my $aa = {}; - push @Apache::londefdef::table, $aa; + push(@Apache::londefdef::table, {}); $Apache::londefdef::table[-1]{'row_number'} = -1; #maximum table's width (default coincides with text line length) if ($#Apache::londefdef::table==0) { @@ -2026,7 +1990,13 @@ sub start_table { $Apache::londefdef::table[-1]{'minlen'}=[]; $Apache::londefdef::table[-1]{'content'}=[]; $Apache::londefdef::table[-1]{'align'}=[]; - $currentstring.='\keephidden{NEW TABLE ENTRY}'; + if (&is_inside_of($tagstack, 'sup')) { + $currentstring .= '}} \\\\ \ensuremath{^{ '; + } + if (&is_inside_of($tagstack, 'sub')) { + $currentstring .= '}} \\\\ \ensuremath{_{ '; + } + $currentstring.=' \keephidden{NEW TABLE ENTRY}'; } @@ -2244,35 +2214,81 @@ sub end_table { # if ($colspan > 1) { + my $spanwidth = 0; + for (my $spancol = $jn; $spancol < $jn + $colspan; $spancol++) { + $spanwidth += $fwidth[$spancol]; + } $output .= '\multicolumn{'. $colspan - .'}{|l|}{'; + ."}"; + if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') { + $output .= '{|c|}{'; + } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') { + $output .= '{|r|}{'; + } + else { + $output .= "{|p{$spanwidth mm}|}{"; + } + } + # Rowspan... if colspan is 1, and there's an alignment we'll need + # to kick in a multicolumn in order to get the alignment spec. + # this must precede the multirow or LaTex gets quite upset. + # Naturally if colspan > 1 we've already done that above ^ + # + my $multirow_aligned = 0; if ($rowspan > 1) { + if ($colspan == 1) { + if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') { + $output .= '\multicolumn{1}{|c|}{'; + $multirow_aligned = 1; + } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') { + $output .= '\multicolumn{1}{|r|}{'; + $multirow_aligned = 1; + } + } $have_rowspan++; - $output .= '\multirow{'.$rowspan.'}[0]{'.$fwidth[$jn].'mm}{'; + $output .= '\multirow{'.$rowspan.'}[0]{*}{'; + # + # If we did not throw in a multicolumn to align, then add + # an extra { + # so we close correctly without having to keep additional state + # around + # + if (!$multirow_aligned) { + $output .= '{'; + } } if (($rowspan eq '^') || ($rowspan eq '_')) { $have_rowspan++; } #-------------------------------------------------------------- - if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') { - $output.=¢er_correction().'\begin{center}'; - } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') { - $output.=' \hfill \llap{' + + # For right and center alignment of single cells. + # we are going to use a multicolumn with a span of 1 to specify alignment. + # + if ($colspan == 1 && $rowspan == 1) { + if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') { + $output .= '\multicolumn{1}{|c|}{'; + } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') { + $output .= '\multicolumn{1}{|r|}{'; + } } + $output.=$Apache::londefdef::table[-1]{'content'}[$in][$jn]; - if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') { - $output.='\end{center}'; - } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') { - $output.='} '; + + if (($colspan == 1 && $rowspan == 1) && + (($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') || + ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r'))) { + $output .= '}'; } + # Close off any open multirow: if ($rowspan > 1) { - $output .= '}'; + $output .= '}}'; } # Close off the colspan... # @@ -2835,23 +2851,24 @@ sub start_img { #if original gif/jpg/png file exist do following: my $origsrc=$src; my ($path,$file) = &get_eps_image($src); + # &Apache::lonnet::logthis("Image source: $src result: $path $file"); $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src); &Apache::lonxml::debug("path = $path file = $file src = $src"); if (-e $src) { &Apache::lonxml::debug("$src exists"); my ($height_param,$width_param)= &image_size($origsrc,0.3,$parstack,$safeeval); - my $destpath = $path; - $destpath =~ s/ /\_/g; # Spaces in path cause LaTex to vomit. - my $destfile = $file; - $destfile =~ s/ /\_/g; my $size; if ($width_param) { $size.='width='.$width_param.' mm,'; } if ($height_param) { $size.='height='.$height_param.' mm]'; } + # Default size if not able to extract that (e.g. eps image). + + # &Apache::lonnet::logthis("Size = $size"); + $size='['.$size; $size=~s/,$/]/; - $currentstring .= '\graphicspath{{'.$destpath.'}}' - .'\includegraphics'.$size.'{'.$destfile.'} '; + $currentstring .= '\graphicspath{{'.$path.'}}' + .'\includegraphics'.$size.'{'.$file.'} '; # If there's an alignment specification we need to honor it here. # For the horizontal alignments, we will also honor the @@ -2912,7 +2929,7 @@ sub start_img { ['','bottom','middle','top','left','right'],$token,5); $currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap', ['', 'parbox', 'parpic'], $token, 2); - $currentstring .=&Apache::edit::select_arg('Encyrpt URL:','encrypturl', + $currentstring .=&Apache::edit::select_arg('Encrypt URL:','encrypturl', ['no','yes'], $token, 2); $currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row(); my $src= &Apache::lonxml::get_param('src',$parstack,$safeeval); @@ -3134,34 +3151,14 @@ sub start_frameset { my ($target,$token) = @_; my $currentstring = ''; # Close any pending para. if ($target eq 'web' || $target eq 'webgrade') { - if (!$Apache::lonxml::registered && - $env{'request.state'} eq 'published') { - $currentstring.='<head>'. - &Apache::lonmenu::registerurl(undef,$target).'</head>'; - } - my $onLoad=''; - foreach my $key (keys(%{$token->[2]})) { - if ($key =~ /^onload$/i) { - $onLoad.=$token->[2]->{$key}.';'; - delete($token->[2]->{$key}); - } - } - $token->[2]->{'onload'}=&Apache::lonmenu::loadevents().';'.$onLoad; - my $onUnload=''; - foreach my $key (keys(%{$token->[2]})) { - if ($key =~ /^onunload$/i) { - $onUnload.=$token->[2]->{$key}.';'; - delete($token->[2]->{$key}); - } - } - $token->[2]->{'onunload'}=&Apache::lonmenu::unloadevents(). - ';'.$onUnload; - - $currentstring .= '<'.$token->[1]; - foreach (keys %{$token->[2]}) { - $currentstring.=' '.$_.'="'.$token->[2]->{$_}.'"'; - } - $currentstring.='>'; + $currentstring = + &Apache::loncommon::start_page($Apache::londefdef::title, + $Apache::londefdef::head, + {'add_entries' => $token->[2], + 'no_title' => 1, + 'force_register' => 1, + 'frameset' => 1,}); + } return $currentstring; } @@ -3423,6 +3420,9 @@ sub start_blockquote { if ($target eq 'web' || $target eq 'webgrade') { $currentstring .= $token->[4]; } + if ($target eq 'tex') { + $currentstring .= '\begin{quote}'; + } return $currentstring; } @@ -3432,6 +3432,9 @@ sub end_blockquote { if ($target eq 'web' || $target eq 'webgrade') { $currentstring = $token->[2]; } + if ($target eq 'tex') { + $currentstring = '\end{quote}'; + } return $currentstring; } @@ -4232,7 +4235,7 @@ sub get_eps_image { &Apache::lonnet::repcopy($orig_src); # Failure is not completely fatal. } &Apache::lonxml::debug("get_eps_image: Original image: $orig_src"); - my ($spath, $sname, $sext) = fileparse($src, qr/\.(gif|png|jpg|jpeg)/i); + my ($spath, $sname, $sext) = &fileparse($src, qr/\.(gif|png|jpg|jpeg)/i); $src=~s/\.(gif|png|jpg|jpeg)$/\.eps/i; $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src); &Apache::lonxml::debug("Filelocation gives: $src"); @@ -4257,17 +4260,33 @@ sub get_eps_image { my $newsrc=$orig_src; $newsrc =~ s|(.*)/res/|/home/httpd/html/res/|; &Apache::lonxml::debug("queueing $newsrc for dynamic eps production."); - print FILE "$newsrc\n"; - close FILE; + print FILE ("$newsrc\n"); + close(FILE); $src=~s|/home/httpd/html/res|/home/httpd/prtspool|; $src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|; if ($sext ne "") { # Put the ext. back in to uniquify. $src =~ s/\.eps$/$sext.eps/; } + } + } + } else { + # If the postscript file has spaces in its name, + # LaTeX will gratuitiously vomit. Therefore + # queue such files for copy with " " replaced by "_". + # printout.pm will know them by their .ps or .eps extensions. + my $newsrc = $orig_src; + $newsrc =~ s|(.*)/res/|/home/httpd/html/res/|; + open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat"); + print FILE "$src\n"; + close FILE; + $src=~s|/home/httpd/html/res|/home/httpd/prtspool|; + $src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|; } my ($path,$file)=($src=~m|(.*)/([^/]*)$|); + $path =~ s/ /\_/g; + $file =~ s/ /\_/g; &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />"); return ($path.'/',$file); }