File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.403: download - view: text, annotated - select for diffs
Sat May 2 22:16:58 2009 UTC (15 years ago) by foxr
Branches: MAIN
CVS tags: version_2_9_X, version_2_8_99_0, HEAD, GCI_2, BZ5434-fox
Prevent double \begin{document} from the cases when that happens.

    1: # The LearningOnline Network with CAPA
    2: # Tags Default Definition Module 
    3: #
    4: # $Id: londefdef.pm,v 1.403 2009/05/02 22:16:58 foxr Exp $
    5: # 
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: ## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
   29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
   30: # binary executable programs or libraries distributed by the 
   31: # Michigan State University (the "Licensee"), but any binaries so 
   32: # distributed are hereby licensed only for use in the context
   33: # of a program or computational system for which the Licensee is the 
   34: # primary author or distributor, and which performs substantial 
   35: # additional tasks beyond the translation of (La)TeX into HTML.
   36: # The C source of the Code may not be distributed by the Licensee
   37: # to any other parties under any circumstances.
   38: #
   39: 
   40: package Apache::londefdef; 
   41: 
   42: use Apache::lonnet;
   43: use strict;
   44: use Apache::lonxml;
   45: use Apache::lontable;
   46: use Apache::File();
   47: use Image::Magick;
   48: use Apache::lonmenu();
   49: use Apache::lonmeta();
   50: use Apache::lonlocal;
   51: use Apache::Constants qw(:common);
   52: use File::Basename;
   53: use LONCAPA();
   54: # use Data::Dumper;
   55: 
   56: BEGIN {
   57: 
   58:     &Apache::lonxml::register('Apache::londefdef',('a','abbr','acronym','accessrule','address','allow','applet','area','b','base','basefont','bgo','bgsound','big','blink','blockquote','blankspace','body','br','button','caption','center','cite','code','col','colgroup','dd','del','dfn','dir','div','dl','dt','em','embed','externallink','fieldset','font','form','frame','frameset','h1','h2','h3','h4','h5','h6','head','hr','html','i','iframe','img','input','ins','insert','isindex','kbd','keygen','label','layer','legend','li','link','m','map','marquee','menu','meta','multicol','nobr','noembed','noframes','nolayer','noscript','object','ol','optgroup','option','output','p','param','pre','q','s','samp','select','server','small','spacer','span','strike','strong','sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','tthoption','u','ul','var','wbr','hideweboutput'));
   59: 
   60: }
   61: 
   62: 
   63: sub initialize_londefdef {
   64:     $Apache::londefdef::TD_redirection=0;
   65:     @Apache::londefdef::table = ();
   66:     $Apache::londefdef::select=0;
   67:     undef(@Apache::londefdef::description);
   68:     @Apache::londefdef::DD=(0);
   69:     @Apache::londefdef::DT=(0);
   70:     @Apache::londefdef::seenDT=(0);
   71:     $Apache::londefdef::list_index=0;
   72:     undef($Apache::londefdef::head);
   73:     undef($Apache::londefdef::title);
   74: }
   75: 
   76: #======================= TAG SUBROUTINES =====================
   77: #-- <output>
   78: sub start_output {
   79:     my ($target) = @_;
   80:     if ($target eq 'meta') { $Apache::lonxml::metamode--; }
   81:     return '';
   82: }
   83: sub end_output {
   84:     my ($target) = @_;
   85:     if ($target eq 'meta') { $Apache::lonxml::metamode++; }
   86:     return '';
   87: }
   88: #-- <m> tag
   89: sub start_m {
   90:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   91:     my $currentstring = '';
   92:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
   93:     if ($target eq 'web' || $target eq 'analyze') {
   94: 	&Apache::lonxml::debug("M is starting with:$inside:");
   95: 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
   96: 	if ($eval eq 'on') {
   97: 	    $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
   98: 	    #&Apache::lonxml::debug("M is evaluated to:$inside:");
   99: 	}
  100: 	my $tex = $inside;
  101: 	my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
  102: 	$currentstring = &Apache::lontexconvert::converted(\$inside,$display);
  103: 	if ($Apache::lontexconvert::errorstring) {
  104: 	    my $errormsg='<pre>'.&HTML::Entities::encode($Apache::lontexconvert::errorstring,'<>&"').'</pre> occurred while attempting to convert this TeX: <pre>';
  105: 	    $tex = &HTML::Entities::encode($tex,'<>&"');
  106: 	    my ($linenumber) =
  107: 		($Apache::lontexconvert::errorstring =~ /Line (\d+)/);
  108: 	    if (defined($linenumber)) {
  109: 		my @tex=split("\n",$tex);
  110: 		$tex[$linenumber]='<b><font color="red">'.
  111: 		    $tex[$linenumber].'</font></b>';
  112: 		$tex=join("\n",@tex);
  113: 	    }
  114: 	    &Apache::lonxml::warning($errormsg.$tex.'</pre>');
  115: 	    $Apache::lontexconvert::errorstring='';
  116: 	}
  117: 	#&Apache::lonxml::debug("M is ends with:$currentstring:");
  118: 	$Apache::lonxml::post_evaluate=0;
  119:     } elsif ($target eq 'tex') {
  120: 
  121: 	$currentstring = $inside;
  122: 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
  123: 	if ($eval eq 'on') {
  124: 	    $currentstring=&Apache::run::evaluate($currentstring,$safeeval,$$parstack[-1]);
  125: 	}
  126: 	if ($currentstring=~/^(\s*\\\\\s*)*$/) {$currentstring = ' \vskip 0 mm ';}
  127: 	# detect simple math mode entry exits, and convert them
  128:         # to use \ensuremath ... unless there's a \verb inside.
  129: 	if (! ($currentstring=~/\\verb/)) {
  130: 	    if ($currentstring=~/^\s*\$[^\$].*\$\s*$/) {
  131: 		$currentstring=~s/^(\s*)\$/$1/;
  132: 		$currentstring=~s/\$(\s*)$/$1/;
  133: 		$currentstring='\ensuremath{'.$currentstring.'}';
  134: 	    }
  135: 	}
  136: 	$Apache::lonxml::post_evaluate=0;
  137:     }
  138:     return $currentstring;
  139: }
  140: 
  141: sub end_m {
  142:     my ($target,$token) = @_;
  143:     my $currentstring = '';
  144:     if ($target eq 'tex') {
  145: 	$currentstring = "";
  146:     }
  147:     return $currentstring;
  148: }
  149: 
  150: sub start_tthoption {
  151:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  152:     my $result;
  153:     if ($target eq 'web' || $target eq 'webgrade') {
  154: 	my $inside = &Apache::lonxml::get_all_text("/tthoption",$parser,
  155: 						   $style);
  156: 	$inside=~s/^\s*//;
  157: 	if ($env{'browser.mathml'}) {
  158: 	    &tth::ttmoptions($inside);
  159: 	} else {
  160: 	    &tth::tthoptions($inside);
  161: 	}
  162:     }
  163:     return $result;
  164: }
  165: 
  166: sub end_tthoption {
  167:     my ($target,$token) = @_;
  168:     my $result;
  169:     return $result;
  170: }
  171: 
  172: #-- <html> tag (end tag optional)
  173: sub start_html {
  174:     my ($target,$token) = @_;
  175:     my $currentstring = '';
  176:     if ($target eq 'web' || $target eq 'edit' || $target eq 'webgrade' ) {
  177: 	# start_body() takes care of emitting the <html> 
  178:     } elsif ($target eq 'tex') {
  179: 
  180: 	$currentstring .= &latex_header();
  181:     }
  182:     return $currentstring;
  183: }
  184: 
  185: sub end_html {
  186:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  187:     my $currentstring = '';
  188:     if ($target eq 'web' || $target eq 'webgrade') {
  189: 	# end_body takes care of the </html>
  190:     }
  191:     return $currentstring;
  192: }
  193: 
  194: #-- <head> tag (end tag optional)
  195: sub start_head {
  196:     my ($target,$token) = @_;
  197:     my $currentstring = '';
  198:     if ($target eq 'web' || $target eq 'webgrade') {
  199: 	&Apache::lonxml::startredirection();
  200:     } 
  201:     return $currentstring;
  202: }
  203: 
  204: sub end_head {
  205:     my ($target,$token) = @_;
  206:     my $currentstring = '';
  207:     if (($target eq 'web'      && $env{'request.state'} eq 'published') ||
  208: 	($target eq 'webgrade' && $env{'request.state'} eq 'published')) {
  209: 	# in case there is a </head> but no <head>
  210: 	if ($Apache::lonxml::redirection) {
  211: 	    $Apache::londefdef::head = &Apache::lonxml::endredirection();
  212: 	}
  213:     } 
  214:     return $currentstring;
  215: }
  216: 
  217: #-- <map> tag (end tag required)
  218: sub start_map {
  219:     my ($target,$token) = @_;
  220:     my $currentstring = '';
  221:     if ($target eq 'web' || $target eq 'webgrade') {
  222: 	$currentstring = $token->[4];     
  223:     } 
  224:     return $currentstring;
  225: }
  226: 
  227: sub end_map {
  228:     my ($target,$token) = @_;
  229:     my $currentstring = '';
  230:     if ($target eq 'web' || $target eq 'webgrade') {
  231: 	$currentstring = $token->[2];    
  232:     } 
  233:     return $currentstring;
  234: }
  235: 
  236: #-- <select> tag (end tag required)
  237: sub start_select {
  238:     my ($target,$token) = @_;
  239:     my $currentstring = '';
  240:     if ($target eq 'web' || $target eq 'webgrade') {
  241: 	$currentstring = $token->[4];     
  242:     }  elsif ($target eq 'tex') {
  243: 	$Apache::londefdef::select=0;
  244:     }
  245:     return $currentstring;
  246: }
  247: 
  248: sub end_select {
  249:     my ($target,$token) = @_;
  250:     my $currentstring = '';
  251:     if ($target eq 'web' || $target eq 'webgrade') {
  252: 	$currentstring = $token->[2];    
  253:     } 
  254:     return $currentstring;
  255: }
  256: 
  257: #-- <option> tag (end tag optional)
  258: sub start_option {
  259:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  260:     my $currentstring = '';
  261:     if ($target eq 'web' || $target eq 'webgrade') {
  262: 	$currentstring = $token->[4];     
  263:     } elsif ($target eq 'tex') {
  264: 	$Apache::londefdef::select++;
  265: 	if ($Apache::londefdef::select == 1) {
  266: 	    $currentstring='\noindent\fbox{'.&Apache::lonxml::get_param('value',$parstack,$safeeval).'}\keephidden{';
  267: 	} else {
  268: 	    $currentstring='\keephidden{';
  269: 	}
  270:     }
  271:     return $currentstring;
  272: }
  273: 
  274: sub end_option {
  275:     my ($target,$token) = @_;
  276:     my $currentstring = '';
  277:     if ($target eq 'web' || $target eq 'webgrade') {
  278: 	$currentstring = $token->[2];    
  279:     }  elsif ($target eq 'tex') {
  280: 	$currentstring='}';
  281:     }
  282:     return $currentstring;
  283: }
  284: 
  285: #-- <input> tag (end tag forbidden)
  286: sub start_input {
  287:     my ($target,$token) = @_;
  288:     my $currentstring = '';
  289:     if ($target eq 'web' || $target eq 'webgrade') {
  290: 	$currentstring = $token->[4];     
  291:     } 
  292:     return $currentstring;
  293: }
  294: 
  295: sub end_input {
  296:     my ($target,$token) = @_;
  297:     my $currentstring = '';
  298:     if ($target eq 'web' || $target eq 'webgrade') {
  299: 	$currentstring = $token->[2];    
  300:     } 
  301:     return $currentstring;
  302: }
  303: 
  304: #-- <textarea> tag (end tag required)
  305: sub start_textarea {
  306:     my ($target,$token) = @_;
  307:     my $currentstring = '';
  308:     if ($target eq 'web' || $target eq 'webgrade') {
  309: 	$currentstring = $token->[4];     
  310:     } 
  311:     return $currentstring;
  312: }
  313: 
  314: sub end_textarea {
  315:     my ($target,$token) = @_;
  316:     my $currentstring = '';
  317:     if ($target eq 'web' || $target eq 'webgrade') {
  318: 	$currentstring = $token->[2];    
  319:     } 
  320:     return $currentstring;
  321: }
  322: 
  323: #-- <form> tag (end tag required)
  324: sub start_form {
  325:     my ($target,$token) = @_;
  326:     my $currentstring = '';
  327:     if ($target eq 'web' || $target eq 'webgrade') {
  328: 	$currentstring = $token->[4];     
  329:     } 
  330:     return $currentstring;
  331: }
  332: 
  333: sub end_form {
  334:     my ($target,$token) = @_;
  335:     my $currentstring = '';
  336:     if ($target eq 'web' || $target eq 'webgrade') {
  337: 	$currentstring = $token->[2];    
  338:     } 
  339:     return $currentstring;
  340: }
  341: 
  342: #-- <title> tag (end tag required)
  343: sub start_title {
  344:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  345:     my $currentstring = '';
  346:     if ($target eq 'web' || $target eq 'webgrade') {
  347: 	$Apache::londefdef::title = 
  348: 	    &Apache::lonxml::get_all_text('/title',$parser,$style);
  349:     } elsif ($target eq 'tex') {
  350: 	$currentstring .= '\keephidden{Title of the document:  ' 
  351:     }
  352:     if ($target eq 'meta') {
  353: 	$currentstring='<title>';
  354: 	&start_output($target);
  355:     }
  356:     return $currentstring;
  357: }
  358: 
  359: sub end_title {
  360:     my ($target,$token) = @_;
  361:     my $currentstring = '';
  362:     if ($target eq 'web' || $target eq 'webgrade') {
  363: 	# start_title takes care of swallowing the title
  364:     } elsif ($target eq 'tex') {
  365: 	$currentstring .= '}';
  366:     }  
  367:     if ($target eq 'meta') {
  368: 	&end_output($target);
  369: 	$currentstring='</title>';
  370:     } 
  371:     return $currentstring;
  372: }
  373: 
  374: #-- <meta> tag (end tag forbidden)
  375: sub start_meta {
  376:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  377:     my $currentstring = '';
  378:     if ($target eq 'web' || $target eq 'webgrade') {
  379: 	my $args='';
  380: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  381: 	if ($args eq '') {
  382: 	    &Apache::lonxml::get_all_text("/meta",$parser,$style);
  383: 	} else {
  384: 	    $currentstring = $token->[4];
  385: 	}
  386:     } elsif ($target eq 'meta') {
  387: 	unless (&Apache::lonxml::get_param
  388: 		('http-equiv',$parstack,$safeeval,undef,1)) {
  389: 	    my $name=$token->[2]->{'name'};
  390: 	    $name=~tr/A-Z/a-z/;
  391: 	    $name=~s/\s/\_/gs;
  392: 	    $name=~s/\W//gs;
  393: 	    if ($name) {
  394: 		$currentstring='<'.$name;
  395:                  my $display=&Apache::lonxml::get_param
  396: 		('display',$parstack,$safeeval,undef,1);
  397:                 if ($display) {
  398:                     $display=~s/\"/\'/g;
  399: 		    $currentstring.=' display="'.$display.'"';
  400:                 }
  401: 		$currentstring.='>'.
  402: 		    &Apache::lonxml::get_param
  403: 			('content',$parstack,$safeeval,undef,1).
  404: 			'</'.$name.'>';
  405: 	    }
  406:             my $display=&Apache::lonxml::get_param
  407: 		('display',$parstack,$safeeval,undef,1);
  408:             if ($display) {
  409: 		$display=&HTML::Entities::encode($display,'<>&"');
  410: 		$currentstring.='<'.$name.'.display>'.$display.
  411:                                '</'.$name.'.display>';
  412:             }
  413: 	}
  414:     } elsif ($target eq 'tex') {
  415: 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
  416: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
  417: 	if ((not defined $content) && (not defined $name)) {
  418: 	    &Apache::lonxml::startredirection();
  419: 	}
  420:     } elsif ($target eq 'edit') {
  421: 	$currentstring .= &Apache::edit::tag_start($target,$token);
  422: 	$currentstring .= &Apache::edit::text_arg('Name:','name',$token,30);
  423: 	$currentstring .= &Apache::edit::text_arg('Content:','content',$token,70);
  424: 	$currentstring .= &Apache::edit::end_row();
  425:     } elsif ($target eq 'modified') {
  426: 	my $constructtag =
  427: 	    &Apache::edit::get_new_args($token,$parstack,$safeeval,
  428: 					'name','content');
  429: 	if ($constructtag) { $currentstring = &Apache::edit::rebuild_tag($token); }
  430:     }
  431:     return $currentstring;
  432: }
  433: 
  434: sub end_meta {
  435:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  436:     my $currentstring = '';
  437:     if ($target eq 'web' || $target eq 'webgrade') {
  438: 	my $args='';
  439: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  440: 	if ($args ne '') {
  441: 	    $currentstring = $token->[4];
  442: 	}
  443:     } elsif ($target eq 'tex') {
  444: 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
  445: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
  446: 	if ((not defined $content) && (not defined $name)) {
  447: 	    &Apache::lonxml::endredirection();
  448: 	}
  449:     }
  450:     return $currentstring;
  451: }
  452: 
  453: sub insert_meta {
  454:     return '
  455:     <meta />';
  456: }
  457: 
  458: # accessrule
  459: sub start_accessrule {
  460:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  461:     my $currentstring = '';
  462:     my $eff  =&Apache::lonxml::get_param('effect',$parstack,$safeeval,undef,1);
  463:     my $realm=&Apache::lonxml::get_param('realm', $parstack,$safeeval,undef,1);
  464:     my $role =&Apache::lonxml::get_param('role',  $parstack,$safeeval,undef,1);
  465:     my $type =&Apache::lonxml::get_param('type',  $parstack,$safeeval,undef,1);
  466: 
  467:     my ($dom,$crs,$sec,$separator);
  468:     if ($type eq 'user') {
  469: 	($dom,$crs,$sec)=split(m{/},$realm);
  470: 	$crs = &LONCAPA::clean_username($crs);
  471: 	$separator = '/';
  472:     } else {
  473: 	($dom,$crs,$sec)=split(/\_/,$realm);
  474: 	$crs = &LONCAPA::clean_courseid($crs);
  475: 	$separator = '_';
  476:     }
  477:     $dom = &LONCAPA::clean_domain($dom);
  478: 
  479:     $sec =~s/\W//;
  480:     $realm = $dom;
  481:     if ($crs =~ /\S/) { $realm .= $separator.$crs; }
  482:     if ($sec =~ /\S/) { $realm .= $separator.$sec; }
  483:     $role=~s/\W//g;
  484: 
  485:     if ($target eq 'web') {
  486: 	my $args='';
  487: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  488: 	if ($args eq '') {
  489: 	    &Apache::lonxml::get_all_text("/accessrule",$parser,$style);
  490: 	} else {
  491: 	    $currentstring = $token->[4];
  492: 	}
  493:     }
  494:     if ($target eq 'meta') {
  495: 	$currentstring='<rule>'.$eff.':'.$realm.':'.$role.':'.$type.'</rule>';
  496:     }
  497:     return $currentstring;
  498: }
  499: 
  500: sub end_accessrule {
  501:     my ($target,$token,$tagstack,$parstack,$parser) = @_;
  502:     my $currentstring = '';
  503:     if ($target eq 'web') {
  504: 	my $args='';
  505: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  506: 	if ($args ne '') {
  507: 	    $currentstring = $token->[4];
  508: 	}
  509:     } 
  510:     return $currentstring;
  511: }
  512: 
  513: sub generate_css_links {
  514:     my $links;
  515:     my $css_href = &Apache::lonnet::EXT('resource.0.cssfile');
  516:     if ($css_href =~ /\S/) {
  517: 	&Apache::lonxml::extlink($css_href);
  518: 	$links .= 
  519: 	    '<link rel="stylesheet" type="text/css" href="'.$css_href.'" />';
  520:     }
  521:     return $links;
  522: }
  523: 
  524: #-- <body> tag (end tag required)
  525: sub start_body {
  526:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  527:     my $currentstring = '';
  528: 
  529:     if ($target eq 'web' || $target eq 'webgrade') {
  530: 	if ($Apache::lonhomework::parsing_a_problem) {
  531: 	    &Apache::lonxml::warning("<body> tag found inside of <problem> tag this can cause problems.");
  532: 	    return '';
  533: 	}
  534: 	
  535: 	if (&is_inside_of($tagstack, "head")) {
  536: 	    &end_head(@_);
  537: 	}
  538: 	
  539: 	my $extra_head = &generate_css_links();
  540: 
  541: 	$currentstring = 
  542: 	    &Apache::loncommon::start_page($Apache::londefdef::title,
  543: 					   $Apache::londefdef::head
  544: 					      .$extra_head,
  545: 					   {'add_entries'    => $token->[2],
  546: 					    'no_title'       => 1,
  547: 					    'force_register' => 1});
  548: 
  549: 	if ($env{'request.state'} ne 'published') {
  550: 	    $currentstring.=&Apache::lonmenu::constspaceform();
  551: 	    $currentstring.=&Apache::londefdef::edit_controls();
  552: 	}
  553: 	$currentstring.=&Apache::lonxml::message_location();
  554:     } elsif ($target eq 'tex') {
  555: 	$currentstring = '';   #  '\begin{document}' is in header.
  556:     } 
  557:     return $currentstring;
  558: }
  559: 
  560: sub edit_controls {
  561:     my ($nochgview) = @_;
  562:     my $result .= '
  563: <form method="post">
  564: <div class="LC_edit_problem_header">';
  565:     unless ($nochgview) {
  566:         $result .= '
  567: <div class="LC_edit_problem_header_row1">'.
  568: &Apache::lonxml::renderingoptions().'
  569: <input type="submit" name="changeproblemmode" value="'.&mt('Change View').'" />
  570: </div>';
  571:     }
  572:     $result .= '
  573: <div class="LC_edit_problem_header_edit_row"><input type="submit" name="editmode" accesskey="e" value="'.&mt('Edit').'" /></div></div>
  574: </form>
  575: <br />';
  576:     return $result;
  577: }
  578: 
  579: sub end_body {
  580:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  581:     my $currentstring = &end_p();	# Close off unclosed <p>
  582:     if ($target eq 'web' || $target eq 'webgrade') {
  583: 	$currentstring .= &Apache::loncommon::end_page({'discussion' => 1});
  584:     } elsif ($target eq 'tex') {
  585: 	$currentstring .= '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';  
  586:     } 
  587:     return $currentstring;
  588: }
  589: 
  590: # \begin{center} causes a new paragprah spacing that looks odd inside 
  591: # of a table cell.  Same at the end of a \center but with a slightly
  592: # larger space .. hence center_correction and center_end_correction.
  593: #
  594: sub center_correction { return '\vspace*{-6 mm}'; } 
  595: sub center_end_correction { return '\vspace*{-7 mm}'; }
  596: 
  597: #-- <center> tag (end tag required)
  598: sub start_center {
  599:     my ($target,$token,$tagstack) = @_;
  600:     my $currentstring = &end_p();	# Close off any prior para.
  601:     if ($target eq 'web' || $target eq 'webgrade') {
  602: 	$currentstring .= $token->[4];     
  603:     } elsif ($target eq 'tex') {
  604: 	if (&is_inside_of($tagstack, "table")) {
  605: 	    $currentstring .= &center_correction();
  606: 	}
  607: 	$currentstring .= '\begin{center}';  
  608:     }
  609:     return $currentstring;
  610: }
  611: 
  612: sub end_center {
  613:     my ($target,$token,$tagstack) = @_;
  614:     my $currentstring = '';
  615:     if ($target eq 'web' || $target eq 'webgrade') {
  616: 	$currentstring = $token->[2];     
  617:     } elsif ($target eq 'tex') {
  618: 	$currentstring = '\end{center}';  
  619: 	if (&is_inside_of($tagstack, "table")) {
  620: 	    $currentstring .= &center_end_correction();
  621: 	}
  622:     }
  623:     return $currentstring;
  624: }
  625: 
  626: #-- <b> tag (end tag required)
  627: #      NOTE: In TeX mode disables internal <p>
  628: sub start_b {
  629:     my ($target,$token) = @_;
  630:     my $currentstring = '';
  631:     if ($target eq 'web' || $target eq 'webgrade') {
  632: 	$currentstring = $token->[4];     
  633:     } elsif ($target eq 'tex') {
  634: 	&disable_para();
  635: 	$currentstring .= '\textbf{';  
  636:     } 
  637:     return $currentstring;
  638: }
  639: 
  640: sub end_b {
  641:     my ($target,$token) = @_;
  642:     my $currentstring = '';
  643:     if ($target eq 'web' || $target eq 'webgrade') {
  644: 	$currentstring = $token->[2];     
  645:     } elsif ($target eq 'tex') {
  646: 	&enable_para();
  647: 	$currentstring = '}';
  648:     } 
  649:     return $currentstring;
  650: }
  651: 
  652: #-- <strong> tag (end tag required)
  653: #    NOTE: in TeX mode disables internal <p>
  654: sub start_strong {
  655:     my ($target,$token) = @_;
  656:     my $currentstring = '';
  657:     if ($target eq 'web' || $target eq 'webgrade') {
  658: 	$currentstring = $token->[4];     
  659:     } elsif ($target eq 'tex') {
  660: 	&disable_para();
  661: 	$currentstring = '\textbf{';  
  662:     } 
  663:     return $currentstring;
  664: }
  665: 
  666: sub end_strong {
  667:     my ($target,$token) = @_;
  668:     my $currentstring = '';
  669:     if ($target eq 'web' || $target eq 'webgrade') {	
  670: 	$currentstring = $token->[2];     
  671:     } elsif ($target eq 'tex') {
  672: 	&enable_para();
  673: 	$currentstring = '}';  
  674:     }
  675:     return $currentstring;
  676: }
  677: 
  678: #-- <h1> tag (end tag required)
  679: sub start_h1 {
  680:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  681:     my $currentstring = &end_p();	# Close off any prior para.
  682:     if ($target eq 'web' || $target eq 'webgrade') {
  683: 	$currentstring .= $token->[4];
  684:     } elsif ($target eq 'tex') {
  685: 	my $pre;
  686: 	my $align=lc(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1));
  687: 	if ($align eq 'center') {
  688: 	    $pre='\begin{center}';
  689: 	} elsif ($align eq 'left') {
  690: 	    $pre='\rlap{';
  691: 	} elsif ($align eq 'right') {
  692: 	    $pre=' \hfill \llap{';
  693: 	}
  694: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  695: 	if (not defined $TeXsize) {$TeXsize="large";}
  696: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  697:     } elsif ($target eq 'meta') {
  698: 	$currentstring.='<subject>';
  699: 	&start_output($target);
  700:     }
  701:     return $currentstring;
  702: }
  703: 
  704: sub end_h1 {
  705:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  706:     my $currentstring = '';
  707:     if ($target eq 'web' || $target eq 'webgrade') {
  708: 	$currentstring .= $token->[2];
  709:     } elsif ($target eq 'tex') {
  710: 	my $post='\vskip 0 mm ';
  711: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  712: 	if ($align eq 'center') {
  713: 	    $post='\end{center}';
  714: 	} elsif ($align eq 'left') {
  715: 	    $post='} \hfill'.'\vskip 0 mm ';
  716: 	} elsif ($align eq 'right') {
  717: 	    $post='}'.'\vskip 0 mm ';
  718: 	}
  719: 	$currentstring .= '}}'.$post;
  720:     } elsif ($target eq 'meta') {
  721: 	&end_output($target);
  722: 	$currentstring='</subject>';
  723:     } 
  724:     return $currentstring;
  725: }
  726: 
  727: #-- <h2> tag
  728: sub start_h2 {
  729:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  730:     my $currentstring = &end_p();	# Close off any prior para.
  731:     if ($target eq 'web' || $target eq 'webgrade') {
  732: 	$currentstring .= $token->[4];
  733:     } elsif ($target eq 'tex') {
  734: 	my $pre;
  735: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  736: 	if ($align eq 'center') {
  737: 	    $pre='\begin{center}';
  738: 	} elsif ($align eq 'left') {
  739: 	    $pre='\rlap{';
  740: 	} elsif ($align eq 'right') {
  741: 	    $pre=' \hfill \llap{';
  742: 	}
  743: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  744: 	if (not defined $TeXsize) {$TeXsize="large";}
  745: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  746:     } 
  747:     return $currentstring;
  748: }
  749: 
  750: sub end_h2 {
  751:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  752:     my $currentstring = '';
  753:     if ($target eq 'web' || $target eq 'webgrade') {
  754: 	$currentstring .= $token->[2];
  755:     } elsif ($target eq 'tex') {
  756: 	my $post='\vskip 0 mm ';
  757: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  758: 	if ($align eq 'center') {
  759: 	    $post='\end{center}';
  760: 	} elsif ($align eq 'left') {
  761: 	    $post='} \hfill'.'\vskip 0 mm ';
  762: 	} elsif ($align eq 'right') {
  763: 	    $post='}'.'\vskip 0 mm ';
  764: 	}
  765: 	$currentstring .= '}}'.$post;
  766:     } 
  767:     return $currentstring;
  768: }
  769: 
  770: #-- <h3> tag
  771: sub start_h3 {
  772:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  773:     my $currentstring = &end_p();	# Close off any prior para.
  774:     if ($target eq 'web' || $target eq 'webgrade') {
  775: 	$currentstring .= $token->[4];
  776:     } elsif ($target eq 'tex') {
  777: 	my $pre;
  778: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  779: 	if ($align eq 'center') {
  780: 	    $pre='\begin{center}';
  781: 	} elsif ($align eq 'left') {
  782: 	    $pre='\rlap{';
  783: 	} elsif ($align eq 'right') {
  784: 	    $pre=' \hfill \llap{';
  785: 	}
  786: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  787: 	if (not defined $TeXsize) {$TeXsize="large";}
  788: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  789:     } 
  790:     return $currentstring;
  791: }
  792: 
  793: sub end_h3 {
  794:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  795:     my $currentstring = '';
  796:     if ($target eq 'web' || $target eq 'webgrade') {
  797: 	$currentstring .= $token->[2];
  798:     } elsif ($target eq 'tex') {
  799: 	my $post='\vskip 0 mm ';
  800: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  801: 	if ($align eq 'center') {
  802: 	    $post='\end{center}';
  803: 	} elsif ($align eq 'left') {
  804: 	    $post='} \hfill'.'\vskip 0 mm ';
  805: 	} elsif ($align eq 'right') {
  806: 	    $post='}'.'\vskip 0 mm ';
  807: 	}
  808: 	$currentstring .= '}}'.$post;
  809:     } 
  810:     return $currentstring;
  811: }
  812: 
  813: #-- <h4> tag
  814: sub start_h4 {
  815:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  816:     my $currentstring = &end_p();	# Close off any prior para.
  817:     if ($target eq 'web' || $target eq 'webgrade') {
  818: 	$currentstring .= $token->[4];
  819:     } elsif ($target eq 'tex') {
  820: 	my $pre;
  821: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  822: 	if ($align eq 'center') {
  823: 	    $pre='\begin{center}';
  824: 	} elsif ($align eq 'left') {
  825: 	    $pre='\rlap{';
  826: 	} elsif ($align eq 'right') {
  827: 	    $pre=' \hfill \llap{';
  828: 	}
  829: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  830: 	if (not defined $TeXsize) {$TeXsize="large";}
  831: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  832:     } 
  833:     return $currentstring;
  834: }
  835: 
  836: sub end_h4 {
  837:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  838:     my $currentstring = '';
  839:     if ($target eq 'web' || $target eq 'webgrade') {
  840: 	$currentstring .= $token->[2];
  841:     } elsif ($target eq 'tex') {
  842: 	my $post='\vskip 0 mm ';
  843: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  844: 	if ($align eq 'center') {
  845: 	    $post='\end{center}';
  846: 	} elsif ($align eq 'left') {
  847: 	    $post='} \hfill'.'\vskip 0 mm ';
  848: 	} elsif ($align eq 'right') {
  849: 	    $post='}'.'\vskip 0 mm ';
  850: 	}
  851: 	$currentstring .= '}}'.$post;
  852:     } 
  853:     return $currentstring;
  854: }
  855: 
  856: #-- <h5> tag
  857: sub start_h5 {
  858:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  859:     my $currentstring = &end_p();	# Close off any prior paras.
  860:     if ($target eq 'web' || $target eq 'webgrade') {
  861: 	$currentstring .= $token->[4];
  862:     } elsif ($target eq 'tex') {
  863: 	my $pre;
  864: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  865: 	if ($align eq 'center') {
  866: 	    $pre='\begin{center}';
  867: 	} elsif ($align eq 'left') {
  868: 	    $pre='\rlap{';
  869: 	} elsif ($align eq 'right') {
  870: 	    $pre=' \hfill \llap{';
  871: 	}
  872: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  873: 	if (not defined $TeXsize) {$TeXsize="large";}
  874: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  875:     } 
  876:     return $currentstring;
  877: }
  878: 
  879: sub end_h5 {
  880:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  881:     my $currentstring = '';
  882:     if ($target eq 'web' || $target eq 'webgrade') {
  883: 	$currentstring .= $token->[2];
  884:     } elsif ($target eq 'tex') {
  885: 	my $post='\vskip 0 mm ';
  886: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  887: 	if ($align eq 'center') {
  888: 	    $post='\end{center}';
  889: 	} elsif ($align eq 'left') {
  890: 	    $post='} \hfill'.'\vskip 0 mm ';
  891: 	} elsif ($align eq 'right') {
  892: 	    $post='}'.'\vskip 0 mm ';
  893: 	}
  894: 	$currentstring .= '}}'.$post;
  895:     } 
  896:     return $currentstring;
  897: }
  898: 
  899: #-- <h6> tag
  900: sub start_h6 {
  901:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  902:     my $currentstring = &end_p();	# Close off any prior paras.
  903:     if ($target eq 'web' || $target eq 'webgrade') {
  904: 	$currentstring .= $token->[4];
  905:     } elsif ($target eq 'tex') {
  906: 	my $pre;
  907: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  908: 	if ($align eq 'center') {
  909: 	    $pre='\begin{center}';
  910: 	} elsif ($align eq 'left') {
  911: 	    $pre='\rlap{';
  912: 	} elsif ($align eq 'right') {
  913: 	    $pre=' \hfill \llap{';
  914: 	}
  915: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  916: 	if (not defined $TeXsize) {$TeXsize="large";}
  917: 	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
  918:     } 
  919:     return $currentstring;
  920: }
  921: 
  922: sub end_h6 {
  923:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  924:     my $currentstring = '';
  925:     if ($target eq 'web' || $target eq 'webgrade') {
  926: 	$currentstring .= $token->[2];
  927:     } elsif ($target eq 'tex') {
  928: 	my $post='\vskip 0 mm ';
  929: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  930: 	if ($align eq 'center') {
  931: 	    $post='\end{center}';
  932: 	} elsif ($align eq 'left') {
  933: 	    $post='} \hfill'.'\vskip 0 mm ';
  934: 	} elsif ($align eq 'right') {
  935: 	    $post='}'.'\vskip 0 mm ';
  936: 	}
  937: 	$currentstring .= '}}'.$post;
  938:     } 
  939:     return $currentstring;
  940: }
  941: 
  942: #--- <cite> tag (end tag required)
  943: sub start_cite {
  944:     my ($target,$token) = @_;
  945:     my $currentstring = '';
  946:     if ($target eq 'web' || $target eq 'webgrade') {
  947: 	$currentstring .= $token->[4];
  948:     } elsif ($target eq 'tex') {
  949: 	$currentstring .= '\textit{';
  950:     }
  951:     return $currentstring;
  952: }
  953: 
  954: sub end_cite {
  955:     my ($target,$token) = @_;
  956:     my $currentstring = '';
  957:     if ($target eq 'web' || $target eq 'webgrade') {
  958: 	$currentstring .= $token->[2];
  959:     } elsif ($target eq 'tex') {
  960: 	$currentstring .= '}';
  961:     }
  962:     return $currentstring;
  963: }
  964: 
  965: #-- <i> tag (end tag required)
  966: sub start_i {
  967:     my ($target,$token) = @_;
  968:     my $currentstring = '';
  969:     if ($target eq 'web' || $target eq 'webgrade') {
  970: 	$currentstring .= $token->[4];
  971:     } elsif ($target eq 'tex') {
  972: 	$currentstring .= '\textit{';
  973:     }
  974:     return $currentstring;
  975: }
  976: 
  977: sub end_i {
  978:     my ($target,$token) = @_;
  979:     my $currentstring = '';
  980:     if ($target eq 'web' || $target eq 'webgrade') {
  981: 	$currentstring .= $token->[2];
  982:     } elsif ($target eq 'tex') {
  983: 	$currentstring .= '}';
  984:     } 
  985:     return $currentstring;
  986: }
  987: 
  988: #-- <address> tag (end tag required)
  989: sub start_address {
  990:     my ($target,$token) = @_;
  991:     my $currentstring = '';
  992:     if ($target eq 'web' || $target eq 'webgrade') {
  993: 	$currentstring .= $token->[4];
  994:     } elsif ($target eq 'tex') {
  995: 	$currentstring .= '\textit{';
  996:     }
  997:     return $currentstring;
  998: }
  999: 
 1000: sub end_address {
 1001:     my ($target,$token) = @_;
 1002:     my $currentstring = '';
 1003:     if ($target eq 'web' || $target eq 'webgrade') {
 1004: 	$currentstring .= $token->[2];
 1005:     } elsif ($target eq 'tex') {
 1006: 	$currentstring .= '}';
 1007:     }
 1008:     return $currentstring;
 1009: }
 1010: 
 1011: #-- <dfn> tag (end tag required)
 1012: sub start_dfn {
 1013:     my ($target,$token) = @_;
 1014:     my $currentstring = '';
 1015:     if ($target eq 'web' || $target eq 'webgrade') {
 1016: 	$currentstring .= $token->[4];
 1017:     } elsif ($target eq 'tex') {
 1018: 	$currentstring .= '\textit{';
 1019:     } 
 1020:     return $currentstring;
 1021: }
 1022: 
 1023: sub end_dfn {
 1024:     my ($target,$token) = @_;
 1025:     my $currentstring = '';
 1026:     if ($target eq 'web' || $target eq 'webgrade') {
 1027: 	$currentstring .= $token->[2];
 1028:     } elsif ($target eq 'tex') {
 1029: 	$currentstring .= '}';
 1030:     }
 1031:     return $currentstring;
 1032: }
 1033: 
 1034: #-- <tt> tag (end tag required)
 1035: sub start_tt {
 1036:     my ($target,$token) = @_;
 1037:     my $currentstring = '';
 1038:     if ($target eq 'web' || $target eq 'webgrade') {
 1039: 	$currentstring .= $token->[4];
 1040:     } elsif ($target eq 'tex') {
 1041: 	$currentstring .= '\texttt{';
 1042:     }
 1043:     return $currentstring;
 1044: }
 1045: 
 1046: sub end_tt {
 1047:     my ($target,$token) = @_;
 1048:     my $currentstring = '';
 1049:     if ($target eq 'web' || $target eq 'webgrade') {
 1050: 	$currentstring .= $token->[2];
 1051:     } elsif ($target eq 'tex') {
 1052: 	$currentstring .= '}';
 1053:     }
 1054:     return $currentstring;
 1055: }
 1056: 
 1057: #-- <kbd> tag (end tag required)
 1058: sub start_kbd {
 1059:     my ($target,$token) = @_;
 1060:     my $currentstring = '';
 1061:     if ($target eq 'web' || $target eq 'webgrade') {
 1062: 	$currentstring .= $token->[4];
 1063:     } elsif ($target eq 'tex') {
 1064: 	$currentstring .= '\texttt{';
 1065:     }
 1066:     return $currentstring;
 1067: }
 1068: 
 1069: sub end_kbd {
 1070:     my ($target,$token) = @_;
 1071:     my $currentstring = '';
 1072:     if ($target eq 'web' || $target eq 'webgrade') {
 1073: 	$currentstring .= $token->[2];
 1074:     } elsif ($target eq 'tex') {
 1075: 	$currentstring .= '}';
 1076:     }
 1077:     return $currentstring;
 1078: }
 1079: 
 1080: #-- <code> tag (end tag required)
 1081: sub start_code {
 1082:     my ($target,$token) = @_;
 1083:     my $currentstring = '';
 1084:     if ($target eq 'web' || $target eq 'webgrade') {
 1085: 	$currentstring .= $token->[4];
 1086:     } elsif ($target eq 'tex') {
 1087: 	$currentstring .= '\texttt{';
 1088:     } 
 1089:     return $currentstring;
 1090: }
 1091: 
 1092: sub end_code {
 1093:     my ($target,$token) = @_;
 1094:     my $currentstring = '';
 1095:     if ($target eq 'web' || $target eq 'webgrade') {
 1096: 	$currentstring .= $token->[2];
 1097:     } elsif ($target eq 'tex') {
 1098: 	$currentstring .= '}';
 1099:     } 
 1100:     return $currentstring;
 1101: }
 1102: 
 1103: #-- <em> tag (end tag required)
 1104: sub start_em {
 1105:     my ($target,$token) = @_;
 1106:     my $currentstring = '';
 1107:     if ($target eq 'web' || $target eq 'webgrade') {
 1108: 	$currentstring .= $token->[4];
 1109:     } elsif ($target eq 'tex') {
 1110: 	$currentstring .= '\emph{';
 1111:     }
 1112:     return $currentstring;
 1113: }
 1114: 
 1115: sub end_em {
 1116:     my ($target,$token) = @_;
 1117:     my $currentstring = '';
 1118:     if ($target eq 'web' || $target eq 'webgrade') {
 1119: 	$currentstring .= $token->[2];
 1120:     } elsif ($target eq 'tex') {
 1121: 	$currentstring .= '}';
 1122:     } 
 1123:     return $currentstring;
 1124: }
 1125: 
 1126: #-- <q> tag (end tag required)
 1127: sub start_q {
 1128:     my ($target,$token) = @_;
 1129:     my $currentstring = '';
 1130:     if ($target eq 'web' || $target eq 'webgrade') {
 1131: 	$currentstring .= $token->[4];
 1132:     } elsif ($target eq 'tex') {
 1133: 	$currentstring .= '\emph{';
 1134:     }
 1135:     return $currentstring;
 1136: }
 1137: 
 1138: sub end_q {
 1139:     my ($target,$token) = @_;
 1140:     my $currentstring = '';
 1141:     if ($target eq 'web' || $target eq 'webgrade') {
 1142: 	$currentstring .= $token->[2];
 1143:     } elsif ($target eq 'tex') {
 1144: 	$currentstring .= '}';
 1145:     } 
 1146:     return $currentstring;
 1147: }
 1148: 
 1149: #  <p> is a bit strange since it does not require a closing </p>
 1150: #  However in latex, we must often output closing stuff to end
 1151: #  environments and {}'s etc.  Therefore we do all the work
 1152: #  of figuring out the ending strings in the start tag processing,
 1153: #  and provide a mechanism to output the stop text external
 1154: #  to tag processing.
 1155: #
 1156: {
 1157: 
 1158:     my $closing_string = '';		# String required to close <p>
 1159: 
 1160: #   Some tags are <p> fragile meaning that <p> inside of them
 1161: #   does not work within TeX mode.  This is managed via the 
 1162: #   counter below:
 1163: #
 1164: 
 1165:     my $para_disabled = 0;
 1166: 
 1167: sub disable_para {
 1168:     $para_disabled++;
 1169: }
 1170: sub enable_para {
 1171:     $para_disabled--;
 1172: }
 1173: 
 1174: 
 1175: #-- <p> tag (end tag optional)
 1176: #optional attribute - align="center|left|right"
 1177: sub start_p {
 1178:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1179:     my $currentstring = '';
 1180:     if ($target eq 'web' || $target eq 'webgrade') {
 1181: 	$currentstring .= &end_p();	# close off prior para if in progress.
 1182: 	$currentstring .= $token->[4];
 1183: 	if (! ($currentstring =~ /\//)) {
 1184: 	    $closing_string = '</p>'; # Deal correctly with <p /> e.g.
 1185: 	}
 1186:     } elsif ($target eq 'tex' && !$para_disabled) {
 1187: 
 1188: 	$currentstring .= &end_p();	# close off prior para if in progress.
 1189: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1190: 	if ($align eq 'center') {
 1191: 	    $currentstring .='\begin{center}\par ';
 1192: 	    $closing_string = '\end{center}';
 1193: 	    if (&is_inside_of($tagstack, "table")) {
 1194: 		$currentstring = &center_correction().$currentstring;
 1195: 	    }
 1196: 	} elsif ($align eq 'right') {
 1197: 	    $currentstring.="\n".'{\flushright ';
 1198: #	    $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
 1199: 	    $closing_string= "}\n";
 1200: 	} elsif ($align eq 'left') {
 1201: 	    $currentstring.= "\n".'{\flushleft ';
 1202: #	    $currentstring.='\noindent\makebox['.$env{'form.textwidth'}.']{{';
 1203: 	    $closing_string = "}\n";
 1204: 	} else {
 1205:             $currentstring.='\par ';
 1206: 	    if (&is_inside_of($tagstack, 'table')) {
 1207: 		$closing_string = '\vskip 0pt'; # Seems to be consistent with <p> in tables.
 1208: 	    } else {
 1209: 		$closing_string = '\strut\\\\\strut ';
 1210: 	    }
 1211:         }
 1212: 
 1213:     }
 1214:     return $currentstring;
 1215: }
 1216: #
 1217: #  End paragraph processing just requires that we output the
 1218: #  closing string that was saved and blank it.
 1219: sub end_p {
 1220:     #  Note only 'tex' mode uses disable_para and enable_para
 1221:     #  so we don't need to know the target in the check below:
 1222: 
 1223:     if (!$para_disabled) {
 1224: 	my $current_string = $closing_string;
 1225: 	$closing_string = '';	# Not in a para anymore.
 1226: 	return $current_string;
 1227:     } else {
 1228: 	return '';
 1229:     }
 1230: 
 1231: }
 1232: }
 1233: #-- <br> tag (end tag forbidden)
 1234: sub start_br {
 1235:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1236:     my $currentstring = '';
 1237:     if ($target eq 'web' || $target eq 'webgrade') {
 1238: 	$currentstring .= $token->[4];
 1239:     } elsif ($target eq 'tex') {
 1240: 	my @tempo=@$tagstack;
 1241: 	my $signal=0;
 1242: 	#  Not going to factor this to is_inside_of since that would require
 1243:         #  multiple stack traversals.
 1244: 	#
 1245: 	for (my $i=$#tempo;$i>=0;$i--) {
 1246: 	    if (($tempo[$i] eq 'b') || ($tempo[$i] eq 'strong') ||
 1247:                 ($tempo[$i] eq 'ol') || ($tempo[$i] eq 'ul'))  {
 1248: 		$signal=1;
 1249: 	    }
 1250: 	    if (($tempo[$i] eq 'td') || ($tempo[$i] eq 'th')) {
 1251: 		$signal = 1;
 1252: 	    }
 1253: 	}
 1254: 	if ($signal != 1) {
 1255: 	    $currentstring .= '\strut \\\\ \strut ';
 1256: 	}
 1257:     
 1258:     }
 1259:     return $currentstring;
 1260: }
 1261: 
 1262: sub end_br {
 1263:     my ($target,$token) = @_;
 1264:     my $currentstring = '';
 1265:     if ($target eq 'web' || $target eq 'webgrade') {
 1266: 	$currentstring .= $token->[2];
 1267:     }
 1268:     return $currentstring;
 1269: }
 1270: 
 1271: #-- <big> tag (end tag required)
 1272: sub start_big {
 1273:     my ($target,$token) = @_;
 1274:     my $currentstring = '';
 1275:     if ($target eq 'web' || $target eq 'webgrade') {
 1276: 	$currentstring .= $token->[4];
 1277:     } elsif ($target eq 'tex') {
 1278: 	$currentstring .= '{\large ';
 1279:     } 
 1280:     return $currentstring;
 1281: }
 1282: 
 1283: sub end_big {
 1284:     my ($target,$token) = @_;
 1285:     my $currentstring = '';
 1286:     if ($target eq 'web' || $target eq 'webgrade') {
 1287: 	$currentstring .= $token->[2];
 1288:     } elsif ($target eq 'tex') {
 1289: 	$currentstring .= '}';
 1290:     }
 1291:     return $currentstring;
 1292: }
 1293: 
 1294: #-- <small> tag (end tag required)
 1295: sub start_small {
 1296:     my ($target,$token) = @_;
 1297:     my $currentstring = '';
 1298:     if ($target eq 'web' || $target eq 'webgrade') {
 1299: 	$currentstring .= $token->[4];
 1300:     } elsif ($target eq 'tex') {
 1301: 	$currentstring .= '{\footnotesize ';
 1302:     }
 1303:     return $currentstring;
 1304: }
 1305: 
 1306: sub end_small {
 1307:     my ($target,$token) = @_;
 1308:     my $currentstring = '';
 1309:     if ($target eq 'web' || $target eq 'webgrade') {
 1310: 	$currentstring .= $token->[2];
 1311:     } elsif ($target eq 'tex') {
 1312: 	$currentstring .= '}';
 1313:     }
 1314:     return $currentstring;
 1315: }
 1316: 
 1317: #-- <basefont> tag (end tag forbidden)
 1318: sub start_basefont {
 1319:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1320:     my $currentstring = '';
 1321:     if ($target eq 'web' || $target eq 'webgrade') {
 1322: 	$currentstring = $token->[4];     
 1323:     } elsif ($target eq 'tex') {
 1324: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1325: 	if (defined $basesize) {
 1326: 	    $currentstring = '{\\'.$basesize.' ';
 1327: 	}
 1328:     }
 1329:     return $currentstring;
 1330: }
 1331: 
 1332: sub end_basefont {
 1333:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1334:     my $currentstring = '';
 1335:     if ($target eq 'web' || $target eq 'webgrade') {
 1336: 	$currentstring = $token->[4];     
 1337:     } elsif ($target eq 'tex') {
 1338: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1339: 	if (defined $basesize) {
 1340: 	    $currentstring = '}';
 1341: 	}
 1342:     }
 1343:     return $currentstring;
 1344: }
 1345: 
 1346: #-- <font> tag (end tag required)
 1347: sub start_font {
 1348:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1349:     my $currentstring = '';
 1350:     if ($target eq 'web' || $target eq 'webgrade') {
 1351: 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
 1352: 	if ($face!~/symbol/i) {
 1353: 	    if (($env{'browser.fontenhance'} eq 'on') || 
 1354: 		($env{'browser.blackwhite'} eq 'on')) { return ''; }
 1355: 	}
 1356: 	$currentstring = $token->[4];     
 1357:     }  elsif ($target eq 'tex') {
 1358: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1359: 	if (defined $fontsize) {
 1360: 	    $currentstring = '{\\'.$fontsize.' ';
 1361: 	}
 1362:     }
 1363:     return $currentstring;
 1364: }
 1365: 
 1366: sub end_font {
 1367:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1368:     my $currentstring = '';
 1369:     if ($target eq 'web' || $target eq 'webgrade') {
 1370: 	$currentstring = $token->[2];    
 1371:     }  elsif ($target eq 'tex') {
 1372: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1373: 	if (defined $fontsize) {
 1374: 	    $currentstring = '}';
 1375: 	}
 1376:     }
 1377:     return $currentstring;
 1378: }
 1379:  
 1380: #-- <strike> tag (end tag required)
 1381: sub start_strike {
 1382:     my ($target,$token) = @_;
 1383:     my $currentstring = '';
 1384:     if ($target eq 'web' || $target eq 'webgrade') {
 1385: 	$currentstring .= $token->[4];
 1386:     } elsif ($target eq 'tex') {
 1387: 	&Apache::lonxml::startredirection();
 1388:     } 
 1389:     return $currentstring;
 1390: }
 1391: 
 1392: sub end_strike {
 1393:     my ($target,$token) = @_;
 1394:     my $currentstring = '';
 1395:     if ($target eq 'web' || $target eq 'webgrade') {
 1396: 	$currentstring .= $token->[2];
 1397:     } elsif ($target eq 'tex') {
 1398: 	$currentstring=&Apache::lonxml::endredirection();
 1399: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g; 
 1400: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/; 
 1401: 	$currentstring=~s/(\S)\s*$/$1\}/;
 1402:     }
 1403:     return $currentstring;
 1404: }
 1405: 
 1406: #-- <s> tag (end tag required)
 1407: sub start_s {
 1408:     my ($target,$token) = @_;
 1409:     my $currentstring = '';
 1410:     if ($target eq 'web' || $target eq 'webgrade') {
 1411: 	$currentstring .= $token->[4];
 1412:     } elsif ($target eq 'tex') {
 1413: 	&Apache::lonxml::startredirection();
 1414:     } 
 1415:     return $currentstring;
 1416: }
 1417: 
 1418: sub end_s {
 1419:     my ($target,$token) = @_;
 1420:     my $currentstring = '';
 1421:     if ($target eq 'web' || $target eq 'webgrade') {
 1422: 	$currentstring .= $token->[2];
 1423:     } elsif ($target eq 'tex') {
 1424: 	$currentstring=&Apache::lonxml::endredirection();
 1425: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1426: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1427: 	$currentstring=~s/(\S)\s*$/$1\}/;	
 1428:     }
 1429:     return $currentstring;
 1430: }
 1431: 
 1432: #-- <sub> tag (end tag required)
 1433: sub start_sub {
 1434:     my ($target,$token) = @_;
 1435:     my $currentstring = '';
 1436:     if ($target eq 'web' || $target eq 'webgrade') {
 1437: 	$currentstring .= $token->[4];
 1438:     } elsif ($target eq 'tex') {
 1439: 	$currentstring .= '\raisebox{-\smallskipamount}{\scriptsize{';
 1440:     } 
 1441:     return $currentstring;
 1442: }
 1443: 
 1444: sub end_sub {
 1445:     my ($target,$token) = @_;
 1446:     my $currentstring = '';
 1447:     if ($target eq 'web' || $target eq 'webgrade') {
 1448: 	$currentstring .= $token->[2];
 1449:     } elsif ($target eq 'tex') {
 1450: 	$currentstring .= '}}';
 1451:     }
 1452:     return $currentstring;
 1453: }
 1454: 
 1455: #-- <sup> tag (end tag required)
 1456: sub start_sup {
 1457:     my ($target,$token) = @_;
 1458:     my $currentstring = '';
 1459:     if ($target eq 'web' || $target eq 'webgrade') {
 1460: 	$currentstring .= $token->[4];
 1461:     } elsif ($target eq 'tex') {
 1462: 	$currentstring .= '\raisebox{\smallskipamount}{\scriptsize{';
 1463:     } 
 1464:     return $currentstring;
 1465: }
 1466: 
 1467: sub end_sup {
 1468:     my ($target,$token) = @_;
 1469:     my $currentstring = '';
 1470:     if ($target eq 'web' || $target eq 'webgrade') {
 1471: 	$currentstring .= $token->[2];
 1472:     } elsif ($target eq 'tex') {
 1473: 	$currentstring .= '}}';
 1474:     }
 1475:     return $currentstring;
 1476: }
 1477: 
 1478: #-- <hr> tag (end tag forbidden)
 1479: sub start_hr {
 1480:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1481:     my $currentstring = &end_p();	# End enclosing para.
 1482:     if ($target eq 'web' || $target eq 'webgrade') {
 1483: 	$currentstring .= $token->[4];
 1484:     } elsif ($target eq 'tex') {
 1485: 
 1486: 	# <hr /> can't be inside of <sup><sub> thank you LaTeX.
 1487: 	# 
 1488: 	my $restart_sub = 0;
 1489: 	my $restart_sup = 0;
 1490: 
 1491: 	# Since <sub> and <sup> are simple tags it's ok to turn off/on
 1492: 	# using the start_ stop_ functions.. those tags only care about
 1493: 	# $target.
 1494: 
 1495: 	if (&is_inside_of($tagstack, "sub")) {
 1496: 	    $restart_sub = 1;
 1497: 	    $currentstring .= &end_sub($target, $token, $tagstack, 
 1498: 				       $parstack, $parser, $safeeval);
 1499: 	}
 1500: 	if (&is_inside_of($tagstack, "sup")) {
 1501: 	    $restart_sup = 1;
 1502: 	    $currentstring .= &end_sup($target, $token, $tagstack,
 1503: 				       $parstack, $parser, $safeeval);
 1504: 	}	
 1505: 
 1506: 	my $LaTeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1507: 	if (defined $LaTeXwidth) {
 1508: 	    if ($LaTeXwidth=~/^%/) {
 1509: 		substr($LaTeXwidth,0,1)='';
 1510: 		$LaTeXwidth=($LaTeXwidth/100).'\textwidth';
 1511: 	    }
 1512: 	} else {
 1513: 	    $LaTeXwidth ='0.9\textwidth';
 1514: 	}
 1515: 	my ($pre,$post);
 1516: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1517: 	if (($align eq 'center') || (not defined $align)) {
 1518: 	    $pre=''; $post='';
 1519: 	} elsif ($align eq 'left') {
 1520: 	    $pre='\rlap{'; $post='} \hfill';
 1521: 	} elsif ($align eq 'right') {
 1522: 	    $pre=' \hfill \llap{'; $post='}';
 1523: 	}
 1524: 	$currentstring .= ' \vskip 0 mm \noindent\makebox['.$LaTeXwidth.']{'.$pre.'\makebox['.
 1525:                                     $LaTeXwidth.'][b]{\hrulefill}'.$post.'}\vskip 0 mm ';
 1526: 	# Turn stuff back on that we can't be inside of.
 1527: 
 1528: 	if ($restart_sub) {
 1529: 	    $currentstring .= &start_sub($target, $token, $tagstack,
 1530: 					$parstack, $parser, $safeeval);
 1531: 	}
 1532: 	if ($restart_sup) {
 1533: 	    $currentstring .= &start_sup($target, $token, $tagstack,
 1534: 					 $parstack, $parser, $safeeval);
 1535: 	}	
 1536:     } 
 1537:     return $currentstring;
 1538: }
 1539: 
 1540: sub end_hr {
 1541:     my ($target,$token) = @_;
 1542:     my $currentstring = '';
 1543:     if ($target eq 'web' || $target eq 'webgrade') {
 1544: 	$currentstring .= $token->[2];
 1545:     }
 1546:     return $currentstring;
 1547: }
 1548: 
 1549: #-- <div> tag (end tag required)
 1550: {
 1551: 
 1552: #  Since div can be nested, the stack below is used
 1553: #  in 'tex' mode to store the ending strings
 1554: #  for the div stack.
 1555: 
 1556:     my @div_end_stack;
 1557: 
 1558: sub start_div {
 1559:     my ($target,$token, $tagstack, $parstack, $parser, $safeeval) = @_;
 1560:     my $currentstring = &end_p();	# Close enclosing para.
 1561:     if ($target eq 'web' || $target eq 'webgrade') {
 1562: 	$currentstring .= $token->[4];
 1563:     } 
 1564:     if ($target eq 'tex') {
 1565: 	# 4 possible alignments: left, right, center, and -missing-.
 1566:         # If inside a table row, we must let the table logic
 1567: 	# do the alignment, however.
 1568: 	# 
 1569: 
 1570: 	my $endstring = '';
 1571: 
 1572: 	my $align = lc(&Apache::lonxml::get_param('align', $parstack,
 1573: 						  $safeeval, undef, 1));
 1574: 	if ($align eq 'center') {
 1575: 	    $currentstring .= '\begin{center}';
 1576: 	    $endstring      = '\end{center}';
 1577: 	    if (&is_inside_of($tagstack, "table")) {
 1578: 		$currentstring = &center_correction().$currentstring;
 1579: 		$endstring    .= &center_end_correction(); 
 1580: 	    }
 1581: 	}
 1582: 	elsif ($align eq 'right') {
 1583: 	    $currentstring .= '\begin{flushright}';
 1584: 	    $endstring     .= '\end{flushright}';
 1585: 	} elsif ($align eq 'left') {
 1586: 	    $currentstring .= '\begin{flushleft}';
 1587: 	    $endstring     = '\end{flushleft}';
 1588: 	} else {
 1589: 	
 1590: 	}
 1591: 	$currentstring .= "\n";   # For human readability.
 1592: 	$endstring       = "\n$endstring\n"; # For human readability
 1593: 	push(@div_end_stack, $endstring);
 1594:     }
 1595:     return $currentstring;
 1596: }
 1597: 
 1598: sub end_div {
 1599:     my ($target,$token) = @_;
 1600:     my $currentstring = '';
 1601:     if ($target eq 'web' || $target eq 'webgrade') {
 1602: 	$currentstring .= $token->[2];
 1603:     }
 1604:     if ($target eq 'tex') {
 1605: 	my $endstring = pop @div_end_stack;
 1606: 	$currentstring .= $endstring;
 1607:     }
 1608:     return $currentstring;
 1609: }
 1610: }
 1611: 
 1612: #-- <a> tag (end tag required)
 1613: sub start_a {
 1614:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1615:     my $currentstring = '';
 1616:     if ($target eq 'web' || $target eq 'webgrade') {
 1617: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 1618: 					    undef,1);
 1619: 	$currentstring=&Apache::lonenc::encrypt_ref($token,{'href'=>$href});
 1620:         if ($href =~ /\S/) {
 1621:             if ($href !~ m{^https?://}) {
 1622:                 my $url=&Apache::lonnet::hreflocation('',$env{'request.filename'});
 1623:                 my $linkurl;
 1624:                 if ($href =~ m{^/uploaded/}) {
 1625:                     $linkurl = $href;
 1626:                 } elsif ($href =~ m{^[^/]}) {
 1627:                     my $path = $url;
 1628:                     $path  =~ s{[^/]*$}{};
 1629:                     $linkurl = $path.$href;
 1630:                 }
 1631:                 if ($linkurl =~ m{^/uploaded/}) {
 1632:                     if (!&Apache::lonnet::allowed('bre',$linkurl)) {
 1633:                         if (&Apache::lonnet::is_on_map($url)) {
 1634:                             &Apache::lonxml::extlink($linkurl);
 1635:                         }
 1636:                     }
 1637:                 }
 1638:             }
 1639:         }
 1640:     }
 1641:     return $currentstring;
 1642: }
 1643: 
 1644: sub end_a {
 1645:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1646:     my $currentstring = '';
 1647:     if ($target eq 'web' || $target eq 'webgrade') {
 1648: 	$currentstring .= $token->[2];
 1649:     }
 1650:     if ($target eq 'tex') {
 1651: 	my $href =
 1652: 	    &Apache::lonxml::get_param('href',$parstack,$safeeval,undef,1);
 1653: 	my $name =
 1654: 	    &Apache::lonxml::get_param('name',$parstack,$safeeval,undef,1);
 1655:         my $uriprint =
 1656:             &Apache::lonxml::get_param('uriprint',$parstack,$safeeval,undef,1);
 1657:         my $anchorprint =
 1658:             &Apache::lonxml::get_param('anchorprint',$parstack,$safeeval,undef,1);
 1659: 	if (($href =~ /\S/) && ($uriprint=~/^on|uriprint|yes|1$/i)) {
 1660: 	    $href =~ s/([^\\])%/$1\\\%/g;
 1661: 	    # Substitute special symbols... and allow line breaks at each /
 1662: 	    #
 1663: 	    $href = &Apache::lonxml::latex_special_symbols($href);
 1664: 	    $href =~ s/\//\/\\-/g;              # Map / to /\- to allow hyphenation.
 1665: 	    $currentstring .= ' ({\tt URI:'.$href.'})';
 1666: 	} elsif (($name =~ /\S/) && ($anchorprint=~/^on|anchorprint|yes|1$/i)) {
 1667: 	    $currentstring .= ' ({\tt Anchor:'.&Apache::lonxml::latex_special_symbols($name).'})';
 1668: 	} else {
 1669: 	    $currentstring.='';
 1670: 	}	
 1671:     }
 1672:     return $currentstring;
 1673: }
 1674: 
 1675: #-- <li> tag (end tag optional)
 1676: sub start_li {
 1677:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1678:     my $currentstring = '';
 1679:     if ($target eq 'web' || $target eq 'webgrade') {
 1680: 	$currentstring = $token->[4];     
 1681:     } elsif ($target eq 'tex') {
 1682: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1683: 	my $value=&Apache::lonxml::get_param('value',$parstack,$safeeval,undef,0);
 1684: 	#FIXME need to support types i and I 
 1685: 	if ($type=~/disc/) {
 1686: 	    $currentstring .= ' \item[$\bullet$] ';
 1687: 	} elsif ($type=~/circle/) {
 1688: 	    $currentstring .= ' \item[$\circ$] ';
 1689: 	} elsif ($type=~/square/) {
 1690: 	    $currentstring .= ' \item[$\diamond$] ';
 1691: 	} elsif ($type eq '1') {
 1692: 	    $currentstring .= ' \item['.($Apache::londefdef::list_index+1).'.]';
 1693: 	} elsif ($type eq 'A') {
 1694: 	    $currentstring .= ' \item['.('A'..'Z')[$Apache::londefdef::list_index].'.]';
 1695: 	} elsif ($type eq 'a') {
 1696: 	    $currentstring .= ' \item['.('a'..'z')[$Apache::londefdef::list_index].'.]';
 1697: 	} elsif ($value ne '') {
 1698: 	    $currentstring .= ' \item['.$value.'] ';
 1699: 	} else {
 1700: 	    $currentstring .= ' \item ';
 1701: 	}  
 1702: 	$Apache::londefdef::list_index++;
 1703:     }
 1704:     return $currentstring;
 1705: }
 1706: 
 1707: sub end_li {
 1708:     my ($target,$token) = @_;
 1709:     my $currentstring = &end_p();	# In case there's a <p> in the <li>
 1710:     if ($target eq 'web' || $target eq 'webgrade') {
 1711: 	$currentstring .= $token->[2];     
 1712:     } 
 1713:     return $currentstring;
 1714: }
 1715: 
 1716: #-- <u> tag (end tag required)
 1717: sub start_u {
 1718:     my ($target,$token) = @_;
 1719:     my $currentstring = '';
 1720:     if ($target eq 'web' || $target eq 'webgrade') {
 1721: 	$currentstring .= $token->[4];
 1722:     } elsif ($target eq 'tex') {
 1723: 	&Apache::lonxml::startredirection();
 1724:     } 
 1725:     return $currentstring;
 1726: }
 1727: 
 1728: sub end_u {
 1729:     my ($target,$token) = @_;
 1730:     my $currentstring = '';
 1731:     if ($target eq 'web' || $target eq 'webgrade') {
 1732: 	$currentstring .= $token->[2];
 1733:     } elsif ($target eq 'tex') {
 1734: 	$currentstring=&Apache::lonxml::endredirection();
 1735: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1736: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1737: 	$currentstring=~s/(\S)\s*$/$1\}/;		
 1738:     }
 1739:     return $currentstring;
 1740: }
 1741: 
 1742: #-- <ul> tag (end tag required)
 1743: sub start_ul {
 1744:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1745:     my $currentstring = &end_p();	# Close off enclosing list.
 1746:     if ($target eq 'web' || $target eq 'webgrade') {
 1747: 	$currentstring .= $token->[4];     
 1748:     } elsif ($target eq 'tex') {
 1749: 	my $TeXtype=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1750: 	$Apache::londefdef::list_index=0;
 1751: 	if ($TeXtype eq 'disc') {
 1752: 	    $currentstring .= '\renewcommand{\labelitemi}{$\bullet$}'.
 1753:                               '\renewcommand{\labelitemii}{$\bullet$}'. 
 1754:                               '\renewcommand{\labelitemiii}{$\bullet$}'.
 1755:                               '\renewcommand{\labelitemiv}{$\bullet$}';
 1756: 	} elsif ($TeXtype eq 'circle') {
 1757: 	    $currentstring .= '\renewcommand{\labelitemi}{$\circ$}'.
 1758:                               '\renewcommand{\labelitemii}{$\circ$}'. 
 1759:                               '\renewcommand{\labelitemiii}{$\circ$}'.
 1760:                               '\renewcommand{\labelitemiv}{$\circ$}';
 1761: 	} elsif ($TeXtype eq 'square') {
 1762: 	    $currentstring .= '\renewcommand{\labelitemi}{$\diamond$}'.
 1763:                               '\renewcommand{\labelitemii}{$\diamond$}'. 
 1764:                               '\renewcommand{\labelitemiii}{$\diamond$}'.
 1765:                               '\renewcommand{\labelitemiv}{$\diamond$}';
 1766: 	}
 1767: 	$currentstring .= '\strut \begin{itemize}';  
 1768:     } 
 1769:     return $currentstring;
 1770: }
 1771: 
 1772: sub end_ul {
 1773:     my ($target,$token) = @_;
 1774:     my $currentstring = '';
 1775:     if ($target eq 'web' || $target eq 'webgrade') {
 1776: 	$currentstring = $token->[2];     
 1777:     } elsif ($target eq 'tex') {
 1778: 	$currentstring = '\end{itemize} \renewcommand{\labelitemi}{$\bullet$}'.
 1779:                                '\renewcommand{\labelitemii}{$\bullet$}'. 
 1780:                                '\renewcommand{\labelitemiii}{$\bullet$}'.
 1781:                                '\renewcommand{\labelitemiv}{$\bullet$}\strut ';  
 1782:     } 
 1783:     return $currentstring;
 1784: }
 1785: 
 1786: #-- <menu> tag (end tag required)
 1787: sub start_menu {
 1788:     my ($target,$token) = @_;
 1789:     my $currentstring = '';
 1790:     if ($target eq 'web' || $target eq 'webgrade') {
 1791: 	$currentstring = $token->[4];     
 1792:     } elsif ($target eq 'tex') {
 1793: 	$currentstring = " \\begin{itemize} ";  
 1794:     } 
 1795:     return $currentstring;
 1796: }
 1797: 
 1798: sub end_menu {
 1799:     my ($target,$token) = @_;
 1800:     my $currentstring = '';
 1801:     if ($target eq 'web' || $target eq 'webgrade') {
 1802: 	$currentstring = $token->[2];     
 1803:     } elsif ($target eq 'tex') {
 1804: 	$currentstring = " \\end{itemize}";  
 1805:     } 
 1806:     return $currentstring;
 1807: }
 1808: 
 1809: #-- <dir> tag (end tag required)
 1810: sub start_dir {
 1811:     my ($target,$token) = @_;
 1812:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1813:     if ($target eq 'web' || $target eq 'webgrade') {
 1814: 	$currentstring .= $token->[4];     
 1815:     } elsif ($target eq 'tex') {
 1816: 	$currentstring .= " \\begin{itemize} ";  
 1817:     } 
 1818:     return $currentstring;
 1819: }
 1820: 
 1821: sub end_dir {
 1822:     my ($target,$token) = @_;
 1823:     my $currentstring = '';
 1824:     if ($target eq 'web' || $target eq 'webgrade') {
 1825: 	$currentstring = $token->[2];     
 1826:     } elsif ($target eq 'tex') {
 1827: 	$currentstring = " \\end{itemize}";  
 1828:     } 
 1829:     return $currentstring;
 1830: }
 1831: 
 1832: #-- <ol> tag (end tag required)
 1833: sub start_ol {
 1834:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1835:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1836:     if ($target eq 'web' || $target eq 'webgrade') {
 1837: 	$currentstring .= $token->[4];     
 1838:     } elsif ($target eq 'tex') {
 1839: 	$Apache::londefdef::list_index=0;
 1840: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1841: 	if ($type eq '1') {
 1842: 	    $currentstring .= '\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1843:                               '\renewcommand{\labelenumii}{\arabic{enumii}.}'. 
 1844:                               '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1845:                               '\renewcommand{\labelenumiv}{\arabic{enumiv}.}';
 1846: 	} elsif ($type eq 'A') {
 1847: 	    $currentstring .= '\renewcommand{\labelenumi}{\Alph{enumi}.}'.
 1848:                               '\renewcommand{\labelenumii}{\Alph{enumii}.}'. 
 1849:                               '\renewcommand{\labelenumiii}{\Alph{enumiii}.}'.
 1850:                               '\renewcommand{\labelenumiv}{\Alph{enumiv}.}';
 1851: 	} elsif ($type eq 'a') {
 1852: 	    $currentstring .= '\renewcommand{\labelenumi}{\alph{enumi}.}'.
 1853:                               '\renewcommand{\labelenumii}{\alph{enumii}.}'.
 1854:                               '\renewcommand{\labelenumiii}{\alph{enumiii}.}'.
 1855:                               '\renewcommand{\labelenumiv}{\alph{enumiv}.}';
 1856: 	} elsif ($type eq 'i') {
 1857: 	    $currentstring .= '\renewcommand{\labelenumi}{\roman{enumi}.}'.
 1858:                               '\renewcommand{\labelenumii}{\roman{enumii}.}'.
 1859:                               '\renewcommand{\labelenumiii}{\roman{enumiii}.}'.
 1860:                               '\renewcommand{\labelenumiv}{\roman{enumiv}.}';
 1861: 	} elsif ($type eq 'I') {
 1862: 	    $currentstring .= '\renewcommand{\labelenumi}{\Roman{enumi}.}'.
 1863:                               '\renewcommand{\labelenumii}{\Roman{enumii}.}'.
 1864:                               '\renewcommand{\labelenumiii}{\Roman{enumiii}.}'.
 1865:                               '\renewcommand{\labelenumiv}{\Roman{enumiv}.}';
 1866: 	}
 1867: 	$currentstring .= '\strut \begin{enumerate}';  
 1868:     } 
 1869:     return $currentstring;
 1870: }
 1871: 
 1872: sub end_ol {
 1873:     my ($target,$token) = @_;
 1874:     my $currentstring = '';
 1875:     if ($target eq 'web' || $target eq 'webgrade') {
 1876: 	$currentstring = $token->[2];     
 1877:     } elsif ($target eq 'tex') {
 1878: 	$currentstring = '\end{enumerate}\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1879:                                         '\renewcommand{\labelenumii}{\arabic{enumii}.}'.
 1880:                                         '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1881:                                         '\renewcommand{\labelenumiv}{\arabic{enumiv}.}\strut ';  
 1882:     } 
 1883:     return $currentstring;
 1884: }
 1885: 
 1886: #-- <dl> tag (end tag required)
 1887: sub start_dl {
 1888:     my ($target,$token) = @_;
 1889:     my $currentstring = &end_p();	# In case there's a <p> unclosed prior to the list.
 1890:     if ($target eq 'web' || $target eq 'webgrade') {
 1891: 	$currentstring .= $token->[4];     
 1892:     } elsif ($target eq 'tex') {
 1893: 	$currentstring .= '\begin{description}';
 1894: 	$Apache::londefdef::DL++;
 1895: 	push(@Apache::londefdef::description,[]);
 1896: 	$Apache::londefdef::DD[$Apache::londefdef::DL]=0;
 1897: 	$Apache::londefdef::DT[$Apache::londefdef::DL]=0;
 1898: 	$Apache::londefdef::seenDT[$Apache::londefdef::DL]=0;
 1899:     } 
 1900:     return $currentstring;
 1901: }
 1902: 
 1903: sub end_dl {
 1904:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1905:     my $currentstring = '';
 1906:     if ($target eq 'web' || $target eq 'webgrade') {
 1907: 	$currentstring = $token->[2];     
 1908:     } elsif ($target eq 'tex') {
 1909: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1910: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1911: 	foreach my $element (@{$Apache::londefdef::description[-1]}) {
 1912: 	    $currentstring.=' '.$element.' ';
 1913: 	}
 1914: 	pop(@Apache::londefdef::description);
 1915: 	$currentstring.='\end{description}';  
 1916: 	delete($Apache::londefdef::DD[$Apache::londefdef::DL]);
 1917: 	delete($Apache::londefdef::DT[$Apache::londefdef::DL]);
 1918: 	delete($Apache::londefdef::seenDT[$Apache::londefdef::DL]);
 1919: 	$Apache::londefdef::DL--;
 1920:     } 
 1921:     return $currentstring;
 1922: }
 1923: 
 1924: #-- <dt> tag (end tag optional)
 1925: sub start_dt {
 1926:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1927:     my $currentstring='';
 1928:     if ($target eq 'web' || $target eq 'webgrade') {
 1929: 	$currentstring = $token->[4];     
 1930:     } elsif ($target eq 'tex') {
 1931: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1932: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1933: 	&Apache::lonxml::startredirection();
 1934: 	$Apache::londefdef::DT[-1]++;
 1935: 	$Apache::londefdef::seenDT[-1]=1;
 1936:     } 
 1937:     return $currentstring;
 1938: }
 1939: 
 1940: sub end_dt {
 1941:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1942:     my $currentstring = '';
 1943:     if ($target eq 'web' || $target eq 'webgrade') {
 1944: 	$currentstring = $token->[2];    
 1945:     } elsif ($target eq 'tex') {
 1946: 	if ($Apache::londefdef::DT[-1]) {
 1947: 	    my $data=&item_cleanup();
 1948: 	    push(@{$Apache::londefdef::description[-1]},'\item['.$data.'] \strut \vskip 0mm');
 1949: 	    $Apache::londefdef::DT[-1]--;
 1950: 	}
 1951:     } 
 1952:     return $currentstring;
 1953: }
 1954: 
 1955: sub item_cleanup {
 1956:     my $item=&Apache::lonxml::endredirection();
 1957:     $item=~s/\\begin{center}//g;
 1958:     $item=~s/\\end{center}//g;
 1959:     return $item;
 1960: }
 1961: 
 1962: #-- <dd> tag (end tag optional)
 1963: sub start_dd {
 1964:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1965:     my $currentstring = '';
 1966:     if ($target eq 'web' || $target eq 'webgrade') {
 1967: 	$currentstring = $token->[4];     
 1968:     } elsif ($target eq 'tex') {
 1969: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1970: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_);}
 1971: 	if (!$Apache::londefdef::seenDT[-1]) {
 1972: 	    push(@{$Apache::londefdef::description[-1]},'\item[\strut] \strut \vskip 0mm ');
 1973: 	}
 1974: 	push(@{$Apache::londefdef::description[-1]},'');
 1975: 	$Apache::londefdef::description[-1]->[-1].=' \strut ';
 1976: 	$Apache::londefdef::DD[-1]++;
 1977: 	&Apache::lonxml::startredirection();
 1978:     } 
 1979:     return $currentstring;
 1980: }
 1981: 
 1982: sub end_dd {
 1983:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1984:     my $currentstring = '';
 1985:     if ($target eq 'web' || $target eq 'webgrade') {
 1986: 	$currentstring = $token->[2];    
 1987:     }  elsif ($target eq 'tex') {
 1988: 	$Apache::londefdef::description[-1]->[-1].=
 1989: 	    &Apache::lonxml::endredirection().' \vskip 0mm ';
 1990: 	$Apache::londefdef::DD[-1]--;
 1991:     }
 1992:     return $currentstring;
 1993: }
 1994: 
 1995: #-- <table> tag (end tag required)
 1996: #       <table> also ends any prior <p> that is not closed.
 1997: #               but, unless I allow <p>'s to nest, that's the
 1998: #               only way I could think of to allow <p> in 
 1999: #               <tr> <th> bodies
 2000: #
 2001: #list of supported attributes: border,width,TeXwidth,TeXtheme
 2002: #                              align
 2003: sub start_table {
 2004:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2005:     my $textwidth = '';
 2006:     my $currentstring = &end_p();
 2007:     if ($target eq 'web' || $target eq 'webgrade') {
 2008: 	$currentstring .= $token->[4];     
 2009:     } elsif ($target eq 'tex') {
 2010: 	&disable_para();	# Can't have paras in a table.
 2011: 
 2012: 
 2013: 	#  New table code:
 2014: 
 2015: 	#  Get the parameters that we can do something about:
 2016: 
 2017: 	my $border = &Apache::lonxml::get_param('border', $parstack, $safeeval, undef, 0);
 2018: 	my $width  = &Apache::lonxml::get_param('TeXwidth', $parstack, $safeeval, undef, 0);
 2019: 	my $theme  = &Apache::lonxml::get_param('TeXtheme', $parstack, $safeeval, undef, 0);
 2020: 	my $align  = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 0);
 2021: 
 2022: 	# The only thing that needs any figuring out is the width.. and then only if it is
 2023: 	# a percent. If not it's assumed to be some valid TeX measurement unit e.g. 3.0cm
 2024: 	#
 2025: 
 2026: 	my $table = new Apache::lontable();
 2027: 	if ($border ne '') {
 2028: 	    $table->table_border(1);
 2029: 	    $table->cell_border(1);
 2030: 	}
 2031: 	if ($theme ne '') {
 2032: 	    $table->theme($theme);
 2033: 	}
 2034: 	if ($align ne '') {
 2035: 	    $table->alignment($align);
 2036: 	}
 2037: 
 2038: 	# Missing width is most of page width
 2039: 
 2040: 	if ($width eq "") {
 2041: 	    $width = '70%';
 2042: 	}
 2043: 	
 2044: 	# If a percentage, need to calculate what this means in terms of
 2045: 	# page width:
 2046: 	
 2047: 	if ($width =~ /%$/) {
 2048: 	    my $textwidth = &recalc($env{'form.textwidth'});  # Page width in mm.
 2049: 	    $width =~ s/%//;
 2050: 	    $width = $width * $textwidth / 100.0;
 2051: 	    $width .= " mm";
 2052: 	    $table->width($width);
 2053: 	}
 2054: 
 2055: 	push(@Apache::londefdef::table, $table);
 2056:         $currentstring.=' \keephidden{NEW TABLE ENTRY}';
 2057: 
 2058: 	#--------------------------------------------------------
 2059: 	#  Old table code here.
 2060: 	#--------------------------------------------------------
 2061: 
 2062: 
 2063: 	if (0) {
 2064: 	push(@Apache::londefdef::table, {}); 
 2065: 	$Apache::londefdef::table[-1]{'row_number'} = -1;
 2066:         #maximum table's width (default coincides with text line length)
 2067: 	if ($#Apache::londefdef::table==0) {
 2068: 	    $textwidth=&recalc($env{'form.textwidth'}); #result is always in mm
 2069: 	    $textwidth=~/(\d+\.?\d*)/;
 2070: 	    $textwidth=0.85*$1; #accounts "internal" LaTeX space for table frame
 2071: 	} else {
 2072: 	    if ($Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]=~/\d/) {
 2073: 		#the maximum width of nested table is determined by LATeX width of parent cell
 2074: 		$textwidth=$Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]; 
 2075: 	    } else {
 2076:               #try to use all space not used before (minus 5% for LaTeX table internal) - rather silly
 2077: 		$textwidth=$Apache::londefdef::table[-2]{'width'};
 2078: 		for (my $i=0;$i<$Apache::londefdef::table[-2]{'counter_columns'};$i++) {
 2079: 		    $textwidth=$textwidth-$Apache::londefdef::table[-2]{'TeXlen'}[0][$i];
 2080: 		}
 2081: 	    }
 2082: 	}
 2083: 
 2084: 	# width either comes forced from the TeXwidth or the width parameters.
 2085: 	# in either case it can be a percentage or absolute width.
 2086: 	# in the width case we ignore absolute width 
 2087: 	my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2088: 	if (!defined($TeXwidth)) {
 2089: 	    my $htmlwidth = &Apache::lonxml::get_param('width',$parstack,
 2090: 						       $safeeval,undef,1);
 2091: 	    if ($htmlwidth =~ /%/) {
 2092: 		$TeXwidth = $htmlwidth;
 2093: 	    } else { 
 2094: 		$TeXwidth = $textwidth;
 2095: 	    }
 2096: 	}
 2097: 	# if the width is specified as a % it is converted to an absolute width.
 2098: 	# otherwise.. just plugged right in the hash
 2099: 
 2100: 	if ($TeXwidth=~/%/) {
 2101: 	    $TeXwidth=~/(\d+)/;
 2102:             $Apache::londefdef::table[-1]{'width'}=$1*$textwidth/100;
 2103: 	} else {
 2104: 	    $Apache::londefdef::table[-1]{'width'}=$TeXwidth;
 2105: 	}
 2106:         #  In the end, however the table width cannot be wider than $textwidth...
 2107: 	
 2108: 	if ($Apache::londefdef::table[-1]{'width'} > $textwidth) {
 2109: 	    $Apache::londefdef::table[-1]{'width'} = $textwidth;
 2110: 	}
 2111:         #table's border
 2112: 	my $border = &Apache::lonxml::get_param('border',$parstack,$safeeval); 
 2113:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 2114: 	unless (defined $border) { $border = 0; }
 2115: 	if ($border) { 
 2116: 	    $Apache::londefdef::table[-1]{'hinc'} = '\hline '; 
 2117: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 2118: 	    $Apache::londefdef::table[-1]{'vvinc'} = '|';
 2119: 	} else {
 2120: 	    $Apache::londefdef::table[-1]{'hinc'} = ''; 
 2121: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 2122: 	    $Apache::londefdef::table[-1]{'vvinc'} = '';
 2123: 	}
 2124: 	if ($#Apache::londefdef::table==0) {
 2125: 	    #    Note that \newline seems to destroy the alignment envs.
 2126: 	    # $Apache::londefdef::table[-1]{'output'}='\strut\newline\strut\setlength{\tabcolsep}{1 mm}';
 2127: 	    $Apache::londefdef::table[-1]{'output'}='\strut'.'\\\\'."\n".'\strut\setlength{\tabcolsep}{1 mm}';
 2128: 	}
 2129: 	$Apache::londefdef::table[-1]{'output'}.=' \noindent \begin{tabular} ';
 2130:         $Apache::londefdef::table[-1]{'TeXlen'}=[];
 2131:         $Apache::londefdef::table[-1]{'objectlen'}=[];
 2132:         $Apache::londefdef::table[-1]{'objectsignal'}=[];
 2133:         $Apache::londefdef::table[-1]{'maxlen'}=[];
 2134:         $Apache::londefdef::table[-1]{'minlen'}=[];
 2135:         $Apache::londefdef::table[-1]{'content'}=[];
 2136:         $Apache::londefdef::table[-1]{'align'}=[];
 2137:         $currentstring.=' \keephidden{NEW TABLE ENTRY}';
 2138:     }
 2139: 
 2140:     }
 2141:     return $currentstring;
 2142: }
 2143:  
 2144: sub end_table {
 2145:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2146:     my $currentstring = '';
 2147:     if ($target eq 'web' || $target eq 'webgrade') {
 2148: 	$currentstring = $token->[2];     
 2149:     } elsif ($target eq 'tex') {
 2150: 
 2151: 
 2152: 	#  New table code:
 2153: 
 2154: 	my $table = pop(@Apache::londefdef::table);
 2155: 	my $t     = $table->generate();
 2156: 	$currentstring = $t->generate_string();
 2157: 	&enable_para();
 2158: 	#--------------------------------------------------------------
 2159: 	#  Old table code:
 2160: 	#--------------------------------------------------------------
 2161: 
 2162: 	if (0) {
 2163: 
 2164: 	my $border =  &Apache::lonxml::get_param('border',$parstack,$safeeval);
 2165: 	my $inmemory = '';
 2166: 	my $output = '';
 2167: 	my $WARNING='';
 2168:         #width of columns from TeXwidth attributes
 2169: 
 2170: 	# Protect against unbalanced </table> tag.
 2171: 
 2172: 	if (scalar(@Apache::londefdef::table) > 0) {
 2173: 
 2174: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2175: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2176: 		if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]<$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn]) {
 2177: 		    $Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]=$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn];
 2178: 		}	
 2179: 	    }
 2180: 	}
 2181:         #free space and number of empty columns
 2182: 	my ($available_space,$empty_columns)=($Apache::londefdef::table[-1]{'width'},0);
 2183: 	if ($#Apache::londefdef::table ne 0) {$available_space=0.9*$available_space;} 
 2184: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2185: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]==0) {
 2186: 		$empty_columns++;
 2187: 	    } else {
 2188: 		$available_space=$available_space-$Apache::londefdef::table[-1]{'TeXlen'}[0][$jn];
 2189: 	    }
 2190: 	}
 2191: 
 2192:         #boundaries for contents columns
 2193: 	my @min_len=();#columns can not be narrower 
 2194: 	my @max_len=();#maximum length of column
 2195: 	my $avg_max;
 2196: 	my $avg_min;
 2197: 	my $counter_cols = $Apache::londefdef::table[-1]{'counter_columns'};
 2198: 	for (my $jn=0;$jn<=$counter_cols; $jn++) {
 2199: 		my ($localmin,$localmax)=(0,0);
 2200: 		for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2201: 		    if ($localmin<$Apache::londefdef::table[-1]{'minlen'}[$in][$jn]) {
 2202: 			$localmin=$Apache::londefdef::table[-1]{'minlen'}[$in][$jn];
 2203: 		    }
 2204: 		    if ($localmax<$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn]) {
 2205: 			$localmax=$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn];
 2206: 		    }
 2207: 		}
 2208: 		push @min_len, $localmin;
 2209: 		push @max_len, $localmax;
 2210: 		$avg_max = $localmax + $avg_max;
 2211: 		$avg_min = $localmin + $avg_min;
 2212: 	}
 2213: 	# Does not really matter what the average max/min are if there are no cols.
 2214: 	# and this prevents div 0 in that case.
 2215: 
 2216: 	if ($counter_cols != 0) {
 2217: 	    $avg_max = $avg_max/$counter_cols;
 2218: 	    $avg_min = $avg_min/$counter_cols;
 2219: 	}
 2220: 
 2221: 
 2222: 	#  I don't think the below is needed.. but just in case:
 2223: 
 2224: 	if ($avg_min > $avg_max) {
 2225: 	    my $temp = $avg_min;
 2226: 	    $avg_min = $avg_max;
 2227: 	    $avg_max = $temp;
 2228: 	}
 2229: 
 2230: 
 2231: 	for (my $jn=0;$jn<=$counter_cols;$jn++) {
 2232: 	    my $localmin=0,;
 2233: 	    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2234: 		if ($localmin<$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn]) {
 2235: 		    $localmin=$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn];
 2236: 		}
 2237: 	    }
 2238: 	    if ($max_len[$jn]<$localmin) {
 2239: 		$max_len[$jn]=$localmin;
 2240: 	    	$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 2241: 	    }#object size is bigger
 2242: 	    if ($min_len[$jn]<$localmin) {
 2243: 		$min_len[$jn]=$localmin;
 2244: 		$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 2245: 	    }#object size is bigger
 2246: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]!=0) {
 2247: 		$min_len[$jn]=0;
 2248: 		$max_len[$jn]=0;
 2249: 	    }
 2250: 	    #  Spans seem to be really bothered by max/min = 0.  So if we have one
 2251: 	    #  make it an average joe max/min.
 2252: 	    
 2253: 	    if ($max_len[$jn] == 0) {
 2254: 		$max_len[$jn] = $avg_max;
 2255: 	    }
 2256: 	    if ($min_len[$jn] == 0) {
 2257: 		$min_len[$jn] = $avg_min;
 2258: 	    }
 2259: 
 2260: 	}
 2261:        #final adjustment of column width
 2262: 	my @fwidth=@{$Apache::londefdef::table[-1]{'TeXlen'}[0]};#final width array
 2263: 	my @adjust=();
 2264:         #step 1. adjustment by maximum value
 2265: 	my $space_needed=0;
 2266: 	for (my $jn=0;$jn<=$#max_len;$jn++) {
 2267: 	    $space_needed=$space_needed+$max_len[$jn];
 2268: 	}
 2269: 	if ($space_needed<=$available_space) {
 2270: 
 2271: 	    for (my $jn=0;$jn<=$#max_len;$jn++) {
 2272: 		if ($fwidth[$jn]==0) {
 2273: 		    $fwidth[$jn]=$max_len[$jn];
 2274: 		}
 2275: 	    }
 2276: 	} else {
 2277:         #step 2. adjustment by minimum value (estimation)
 2278: 	    $space_needed=0;
 2279: 	    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2280: 		$space_needed+=$min_len[$jn];
 2281: 	    }
 2282: 	    if ($space_needed>$available_space) {
 2283: 		$WARNING=' \textbf{NOT ENOUGH SPACE FOR TABLE} ';
 2284: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 2285: 		    if ($fwidth[$jn]==0) {
 2286: 			$fwidth[$jn]=$min_len[$jn];
 2287: 		    }
 2288: 		}
 2289: 		#check if we have objects which can be scaled
 2290: 		my $how_many_to_scale=0;
 2291: 		my @to_scale=();
 2292: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 2293: 		    if ($Apache::londefdef::table[-1]{'objectsignal'}[$jn] eq '1') {
 2294: 			$how_many_to_scale++;
 2295: 			push @to_scale, $jn;
 2296: 		    }
 2297: 		}
 2298: 		if ($how_many_to_scale>0) {
 2299: 		    my $space_to_adjust=($space_needed-$available_space)/$how_many_to_scale;
 2300: 		    foreach my $jn (@to_scale) {
 2301: 			for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2302: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/;
 2303: 			    if ($1 ne '') {
 2304: 				my $current_length=&recalc($1);
 2305: 				$current_length=~/(\d+\.?\d*)/;
 2306: 				$current_length=$current_length-$space_to_adjust;
 2307: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/width=$current_length mm/;
 2308: 			    }
 2309: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/\[(\d+\.?\d*)\s*mm\]/;
 2310: 			    if ($1 ne '') {
 2311: 				my $current_length=$1;
 2312: 				$current_length=$current_length-$space_to_adjust;
 2313: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/\[(\d+\.?\d*)\s*mm\]/\[$current_length mm\]/;
 2314: 			    }				
 2315: 			}
 2316: 			$fwidth[$jn]=$fwidth[$jn]-$space_to_adjust;
 2317: 		    }
 2318: 		}
 2319: 	    } else {
 2320: 	      #step 3. adjustment over minimal + corrections
 2321: 		my $enlarge_coef=$available_space/$space_needed;
 2322: 		my $acsessive=0;
 2323: 		for (my $jn=0;$jn<=$#min_len;$jn++) {
 2324: 		    $adjust[$jn]=$min_len[$jn]*$enlarge_coef;
 2325: 		    if ($adjust[$jn]>$max_len[$jn]) {
 2326: 			$fwidth[$jn]=$max_len[$jn];
 2327: 			$acsessive=$acsessive+$adjust[$jn]-$max_len[$jn];
 2328: 			$adjust[$jn]=0;
 2329: 
 2330: 		    }
 2331: 		}
 2332: 		if ($acsessive>0) {
 2333: 		#we have an excess of space and can redistribute it
 2334: 		    my $notempty_columns=0;
 2335: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2336: 			if ($adjust[$jn]!=0) {
 2337: 			    $notempty_columns++;
 2338: 			}
 2339: 		    }
 2340: 		    my $per_column=$acsessive/$notempty_columns;
 2341: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2342: 			if ($adjust[$jn]!=0) {
 2343: 			    $adjust[$jn]+=$per_column;
 2344: 			    $fwidth[$jn]=$adjust[$jn];
 2345: 			}
 2346: 		    }
 2347: 		} else {
 2348: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2349: 			$fwidth[$jn]=$adjust[$jn];
 2350: 		    }
 2351: 		}
 2352: 	    }
 2353: 	}
 2354:         # use all available width or specified width as if not specified,
 2355: 	# the specified width gets defaulted to the available width.
 2356: 
 2357: 	my $current=0; 
 2358: 	for (my $i=0;$i<=$#fwidth;$i++) {  
 2359: 	    $current+=$fwidth[$i];
 2360: 	}
 2361: 	if ($current == 0) {
 2362:             $current = $Apache::londefdef::table[-1]{'width'};
 2363:         }
 2364: 	my $coef=$Apache::londefdef::table[-1]{'width'}/$current;
 2365: 	for (my $i=0;$i<=$#fwidth;$i++) {  
 2366: 	    $fwidth[$i]*=$coef;
 2367: 	}
 2368:         #removing of empty columns if allowed
 2369:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 2370: 	if ($permission eq 'yes') {
 2371: 	    my @cleaned_table=();
 2372:             my @cleaned_header=();
 2373: 	    my $colind=0;
 2374: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2375: 		if ($fwidth[$jn]!=0) {
 2376: 		    #we need to copy column
 2377: 		    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2378: 			$cleaned_table[$in][$colind]=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2379: 			$cleaned_header[$colind]=$fwidth[$jn];
 2380: 		    }
 2381: 		    $colind++;
 2382: 		}
 2383: 	    }
 2384: 	    $Apache::londefdef::table[-1]{'content'}=\@cleaned_table;
 2385: 	    @fwidth=@cleaned_header;
 2386: 	}
 2387: 
 2388: 
 2389: 	#construct header of the table
 2390: 	my $header_of_table = '{'.$Apache::londefdef::table[-1]{'vvinc'};
 2391: 	for (my $in=0;$in<=$#fwidth;$in++) {
 2392: 	    $header_of_table.='p{'.$fwidth[$in].' mm}'.$Apache::londefdef::table[-1]{'vvinc'};
 2393: 	}
 2394: 	$header_of_table .= '}';
 2395: 
 2396: 	#fill the table
 2397: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2398: 	    my $have_rowspan = 0;
 2399: 	    for (my $jn=0;$jn<=$#fwidth;$jn++) {
 2400: 
 2401: 		#-----------------------------------------------------------
 2402:                 #   I think this order of doing things will ensure that
 2403: 		#   single rowspan, columspan and combined row/colspans will
 2404:                 #   work correctly.  LaTeX is delicate here.
 2405: 		#    RF.
 2406: 		
 2407: 		# Start a rowspan if necessary:
 2408: 		
 2409: 		my $primary_col_width = $fwidth[$jn]; # Width of primary column.
 2410: 		my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
 2411: 		my $colspan = $Apache::londefdef::table[-1]{'colspan'}[$in][$jn];
 2412: 		#
 2413: 		#  Do the appropriate magic if this has a colspan
 2414: 		# 
 2415: 		
 2416: 		my $border_char = "";
 2417: 		if ($border) {
 2418: 		    $border_char = "|";
 2419: 		}
 2420: 		my $spanwidth = 0;
 2421: 		if ($colspan > 1) {
 2422: 		    for (my $spancol = $jn; $spancol < $jn + $colspan; $spancol++) {
 2423: 			$spanwidth += $fwidth[$spancol];
 2424: 		    }
 2425: 		    $output .= '\multicolumn{'.
 2426: 			$colspan
 2427: 			."}";
 2428: 		    if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2429: 			$output .= '{'.$border_char.'c'.$border_char.'}{';
 2430: 		    } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2431: 			$output .= '{'.$border_char.'r'.$border_char.'}{';
 2432: 		    }
 2433: 		    else {
 2434: 			$output .= '{'.$border_char."p{$spanwidth mm}".$border_char.'}{';
 2435: 		    }
 2436: 		    
 2437: 		} else {
 2438: 		    $spanwidth = $primary_col_width; # If no span width will be just colwidth
 2439: 		}
 2440: 
 2441: 		# Rowspan... if colspan is 1, and there's an alignment we'll need
 2442: 		# to kick in a multicolumn in order to get the alignment spec.
 2443: 		# this must precede the multirow or LaTex gets quite upset.
 2444: 		# Naturally if colspan > 1 we've already done that above ^
 2445: 		#
 2446: 		my $multirow_aligned = 0;
 2447: 		if ($rowspan > 1) {
 2448: 		    if ($colspan == 1) {
 2449: 			if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2450: 			    $output .= '\multicolumn{1}{'.$border_char.'c'.$border_char.'}{';
 2451: 			    $multirow_aligned = 1;
 2452: 			} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2453: 			    $output .= '\multicolumn{1}{'.$border_char.'r'.$border_char.'}{';
 2454: 			    $multirow_aligned = 1;
 2455: 			}
 2456: 		    }
 2457: 		    $have_rowspan++;
 2458: 		    if ($multirow_aligned) {
 2459: 			$output .= '\multirow{'.$rowspan.'}[0]{*}{';
 2460: 		    } else {
 2461: 			$output .= '\multirow{'.$rowspan."}[0]{$spanwidth mm}{";
 2462: 		    }
 2463: 		    
 2464: 		    $Apache::londefdef::table[-1]{'content'}[$in][$jn] =~
 2465: 			s{^\s*\\par\s*}{};
 2466: 		    $Apache::londefdef::table[-1]{'content'}[$in][$jn] =~
 2467: 			s{\s*\\vskip\s*0pt\s*$}{};
 2468: 			  
 2469: 		    #
 2470: 		    # If we did not throw in a multicolumn to align, then add 
 2471: 		    # an extra {
 2472: 		    # so we close correctly without having to keep additional state
 2473: 		    # around
 2474: 		    #
 2475: 		    if (!$multirow_aligned) {
 2476: 			$output .= '{';
 2477: 		    }
 2478: 		}
 2479: 		if (($rowspan eq '^') || ($rowspan eq '_')) {
 2480: 		    $have_rowspan++;
 2481: 		}
 2482: 		    #--------------------------------------------------------------
 2483: 
 2484: 
 2485: 		# For right and center alignment of single cells.
 2486: 		# we are going to use a multicolumn with a span of 1 to specify alignment.
 2487: 		#
 2488: 		if ($colspan == 1  && $rowspan == 1) {
 2489: 		    if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2490: 			$output .= '\multicolumn{1}{'.$border_char.'c'.$border_char.'}{';
 2491: 		    } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2492: 			$output .= '\multicolumn{1}{'.$border_char.'r'.$border_char.'}{';
 2493: 		    }
 2494: 		}
 2495: 
 2496: 		$output.=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2497: 
 2498: 		if (($colspan == 1 && $rowspan == 1)   &&
 2499: 		    (($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') ||
 2500: 		     ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r'))) {
 2501: 		    $output .= '}';
 2502: 		}
 2503: 
 2504: 		# Close off any open multirow:
 2505: 		
 2506: 		if ($rowspan > 1) {
 2507: 		    $output .= '}}';
 2508: 		}
 2509: 		#  Close off the colspan...
 2510: 		#
 2511: 		if ($colspan > 1)  {
 2512: 		    $output .= '}';
 2513: 		    $jn += $colspan-1; # Adjust for number of rows really left.
 2514: 		}
 2515:                 if ($jn!=$#fwidth) {$output.=' '.$Apache::londefdef::table[-1]{'vinc'};}
 2516: 	    }
 2517: 	    #  If have_rowspan > 0, and borders are on, then 
 2518: 	    #  we need to do more than put an \hline at the bottom of row.
 2519: 	    #  we need to do the appropriate \cline to ensure that
 2520: 	    #  the spanned rows don't have \hlines through them.
 2521: 
 2522: 	    if (($Apache::londefdef::table[-1]{'hinc'} =~ /\\hline/) && $have_rowspan) {
 2523: 		$output .= ' \\\\ ';
 2524: 		for (my $jn=0; $jn<=$#fwidth;$jn++) {
 2525: 		    my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
 2526: 		    if ($rowspan ne "^") {
 2527: 			if (($rowspan <= 1) || ($rowspan eq '_')) {
 2528: 			    my $column = $jn+1;
 2529: 			    $output .= '\cline{'.$column.'-'.$column.'} ';
 2530: 			}
 2531: 		    }
 2532: 		}
 2533: 
 2534: 	    } else {
 2535: 		$output.=' \\\\ '.$Apache::londefdef::table[-1]{'hinc'}.' ';
 2536: 	    }
 2537: 	}
 2538: 	# Note that \newline destroys alignment env's produced  by e.g. <div>
 2539: 	# $Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut\newline\strut ';
 2540: 	$Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut'.'\\\\'."\n".'\strut ';
 2541: 	if ($#Apache::londefdef::table > 0) {	    
 2542: 	    my $inmemory = $Apache::londefdef::table[-1]{'output'};
 2543: 	    # Figure out max/and min width  by summing us and then
 2544: 	    # apply that to the current column of the table we nest in
 2545: 	    # if it's larger than the current width or the current width
 2546: 	    # is undefined.
 2547: 	    #
 2548: 	    my $min_nested_width = 0;
 2549: 	    my $max_nested_width = 0;
 2550: 	    for (my $col = 0; $col <= $Apache::londefdef::table[-1]{'counter_columns'}; $col++) {
 2551: 		$min_nested_width +=  $min_len[$col];
 2552: 		$max_nested_width +=  $max_len[$col];
 2553: 		
 2554: 	    }
 2555: 	    # Fudge in an extra 5 mm for borders etc:
 2556: 	    
 2557: 	    $min_nested_width += 5;
 2558: 	    $max_nested_width += 5;
 2559: 
 2560: 	    my $outer_column = $Apache::londefdef::table[-2]{'counter_columns'};
 2561: 	    my $outer_row    = $Apache::londefdef::table[-2]{'row_number'};
 2562: 	    if ($min_nested_width > $Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column]) {
 2563: 		$Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column] = $min_nested_width;
 2564: 	    }
 2565: 	    if ($max_nested_width > $Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column]) {
 2566: 		$Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column] = $max_nested_width;
 2567: 	    }
 2568: 
 2569: 	    pop @Apache::londefdef::table;
 2570: 	    push @{$Apache::londefdef::table[-1]{'include'}}, $inmemory;
 2571: 	} else {
 2572: 	    $currentstring .= $Apache::londefdef::table[-1]{'output'};
 2573: 	    pop @Apache::londefdef::table;
 2574: 	    undef @Apache::londefdef::table;
 2575: 	}
 2576: 	}
 2577: 	&enable_para();
 2578:     }
 2579:     }
 2580:     return $currentstring;
 2581: }
 2582: 
 2583: #-- <tr> tag (end tag optional)
 2584: sub start_tr {
 2585:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2586:     my $currentstring = '';
 2587:     if ($target eq 'web' || $target eq 'webgrade') {
 2588: 	$currentstring = $token->[4];     
 2589:     } elsif ($target eq 'tex') {
 2590: 
 2591: 	my $align = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 1);
 2592: 	$Apache::londefdef::table[-1]->start_row();
 2593: 	
 2594: 	if ($align ne '') {
 2595: 	    $Apache::londefdef::table[-1]->configure_row({default_halign => $align});
 2596: 	}
 2597: 
 2598: 	#---------------------------------------------------------------
 2599: 	# Old table code.
 2600: 	#---------------------------------------------------------------
 2601: 
 2602: 	if (0) {
 2603: 	$Apache::londefdef::table[-1]{'row_number'}++;
 2604: 	my $alignchar=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 2605: 	if ($alignchar ne '') {
 2606: 	    push @ {$Apache::londefdef::table[-1]{'rows'} },substr($alignchar,0,1);
 2607: 	} else {
 2608: 	    push @ {$Apache::londefdef::table[-1]{'rows'} }, 'l';
 2609: 	}
 2610: 	push ( @{ $Apache::londefdef::table[-1]{'rowdata'} }, $Apache::londefdef::table[-1]{'hinc'});
 2611: 	#
 2612: 	#  Need to save the number of table columns to preserve the max # columns.
 2613: 	#
 2614: 	$Apache::londefdef::table[-1]{'prior_columns'}   = $Apache::londefdef::table[-1]{'counter_columns'};
 2615: 	$Apache::londefdef::table[-1]{'counter_columns'} = -1;
 2616: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}}, [];
 2617: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}}, [];
 2618: 	push @ {$Apache::londefdef::table[-1]{'minlen'}}, [];
 2619: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}}, [];
 2620: 	push @ {$Apache::londefdef::table[-1]{'content'}}, [];
 2621:     }
 2622:     } 
 2623:     return $currentstring;
 2624: }
 2625:         
 2626: sub end_tr {
 2627:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2628:     my $currentstring = &end_p();	# Close any pending <p> in the row.
 2629:     if ($target eq 'web' || $target eq 'webgrade') {
 2630: 	$currentstring .= $token->[2];     
 2631:     } elsif ($target eq 'tex') {
 2632: 
 2633: 	# In case the user is missing a </td> or </th> tag:
 2634: 
 2635: 	if ($Apache::londefdef::TD_redirection) {
 2636: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2637: 	}
 2638: 	$Apache::londefdef::table[-1]->end_row();
 2639: 
 2640: 	#-----------------------------------------------
 2641: 	# Old table code
 2642: 	#-----------------------------------------------
 2643: 
 2644: 	if (0) {
 2645: 	if ($Apache::londefdef::TD_redirection) {
 2646: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2647: 	}
 2648: 	# Counter columns must be the maximum number of columns seen
 2649: 	# in the table so far so:
 2650: 	if ($Apache::londefdef::table[-1]{'prior_columns'} > $Apache::londefdef::table[-1]{'counter_columns'}) {
 2651: 	    $Apache::londefdef::table[-1]{'counter_columns'} = $Apache::londefdef::table[-1]{'prior_columns'};
 2652: 	}
 2653:     }
 2654: 
 2655: 	
 2656:     }
 2657:     return $currentstring;
 2658: }
 2659: 
 2660: #-- <td> tag (end tag optional)
 2661: sub start_td {
 2662:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2663:     my $currentstring = '';
 2664:     if ($target eq 'web' || $target eq 'webgrade') {
 2665: 	$currentstring = $token->[4];     
 2666:     } elsif ($target eq 'tex') {
 2667: 	$Apache::londefdef::TD_redirection = 1;
 2668: 	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
 2669:     } 
 2670:     return $currentstring;
 2671: }   
 2672:     
 2673: sub tag_check {
 2674:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2675:     my @ar=@$parstack; 
 2676:     for (my $i=$#ar-1;$i>=0;$i--) {
 2677: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2678: 	    &start_td_tex($parstack,$parser,$safeeval);
 2679: 	    last;
 2680: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2681: 	    splice @ar, $i+1;
 2682: 	    &end_td_tex(\@ar,$parser,$safeeval);
 2683: 	    &start_td_tex($parstack,$parser,$safeeval);
 2684: 	    last;
 2685: 	}
 2686:     }
 2687:     return '';
 2688: }
 2689: 
 2690: #
 2691: #  Factor out cell configuration hash generation:
 2692: #
 2693: 
 2694: sub cell_config_hash {
 2695:     my ($align, $rowspan, $colspan) = @_;
 2696:     my %config;
 2697:     if ($align ne '') {
 2698: 	$config{'halign'} = $align;
 2699:     }
 2700:     if ($colspan ne "") {
 2701: 	$config{'colspan'} = $colspan;
 2702:     }
 2703:     if ($rowspan ne '') {
 2704: 	$config{'rowspan'} = $rowspan;
 2705:     }
 2706:     return \%config;
 2707: }
 2708:  
 2709: sub start_td_tex {
 2710:     my ($parstack,$parser,$safeeval) = @_;
 2711: 
 2712:     # At this stage, an empty cell is created with the
 2713:     # appropriate rowspan/colspan and alignment
 2714:     # attributes, but empty of text.  end_td_tex will
 2715:     # fetch the contents from the recursive parse and
 2716:     # fill the cell with them:
 2717:     my $align   = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 1);
 2718:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 1);
 2719:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 1);
 2720: 
 2721:     my $config = &cell_config_hash($align, $rowspan, $colspan);
 2722: 
 2723:     my $table = $Apache::londefdef::table[-1];
 2724:     $table->add_cell('', $config);
 2725:     
 2726: 
 2727:     #------------------------------------------------
 2728:     #  Old table code.
 2729:     #------------------------------------------------
 2730: 
 2731:     if (0) {
 2732: 
 2733:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2734:     if ($alignchar eq '') {
 2735: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2736:     }
 2737:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2738:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2739:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2740:     if (defined $TeXwidth) {		
 2741: 	my $current_length=&recalc($TeXwidth);
 2742: 	$current_length=~/(\d+\.?\d*)/;
 2743: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2744:     }
 2745:     }
 2746:     &Apache::lonxml::startredirection();
 2747:     return '';
 2748: }
 2749: 
 2750: sub end_td_tex {
 2751: 
 2752:     my $text = &Apache::lonxml::endredirection();
 2753:     my $table = $Apache::londefdef::table[-1];
 2754:     $table->append_cell_text($text);
 2755: 
 2756:     #-------------------------------------------------
 2757:     # Old table code
 2758:     #-------------------------------------------------
 2759: 
 2760:     if (0) {
 2761:     my ($parstack,$parser,$safeeval) = @_;
 2762:     my $current_row    = $Apache::londefdef::table[-1]{'row_number'};
 2763:     my $current_column = $Apache::londefdef::table[-1]{'counter_columns'}; 
 2764:     my $data = &Apache::lonxml::endredirection();
 2765: 
 2766:     #  The rowspan array of the table indicates which cells are part of a span.
 2767:     #  n indicates the start of a span set of n rows.
 2768:     #  ^ indicates a cell that continues a span set.
 2769:     #  _ indicates the cell is at the bottom of a span set.
 2770:     #  If this and subsequent cells are part of a rowspan, we must
 2771:     #  push along the row until we find one that is not.
 2772: 
 2773:     while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column]) 
 2774: 	   && ($Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] =~ /[\^\_]/)) {
 2775: 	# Part of a span.
 2776: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1]}, '';
 2777: 	$current_column++;
 2778:     }
 2779:     $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
 2780:    
 2781: 
 2782:     # Get the column and row spans.
 2783:     # Colspan can be done via \multicolumn if I can figure out the data structs.
 2784: 
 2785:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
 2786:     if (!$colspan) {
 2787: 	$colspan = 1;
 2788:     }
 2789: 
 2790:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
 2791:     if (!$rowspan) {
 2792: 	$rowspan = 1;
 2793:     }
 2794: 
 2795: 
 2796: 
 2797:     for (my $c = 0; $c < $colspan; $c++) {
 2798: 	$Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column+$c] = $rowspan;
 2799: 	for (my $i = 1; $i < $rowspan; $i++) {
 2800: 	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '^';
 2801: 	    if ($i == ($rowspan-1)) {
 2802: 		$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '_';
 2803: 	    }
 2804: 	}
 2805:     }
 2806: 
 2807:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2808:     if (defined $TeXwidth) {		
 2809: 	for (my $c = 0; $c < $colspan; $c++) {
 2810: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2811: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2812: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2813: 	}
 2814:     } else {
 2815: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2816: 	    my $garbage_data=$data;
 2817: 	    my $fwidth=0;
 2818:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2819: 		my $current_length=&recalc($1);
 2820: 		$current_length=~/(\d+\.?\d*)/;
 2821: 		if ($fwidth<$1) {$fwidth=$1;}
 2822: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2823: 	    }
 2824:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2825: 		my $current_length=$1;
 2826: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2827: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2828: 	    }
 2829: 	    for (my $c = 0; $c < $colspan; $c++) {
 2830: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2831: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2832: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2833: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2834: 	    }
 2835: 	} elsif ($data=~/\\parbox\{\s*\d+\.?\d*\s*(mm|cm|in|pc|pt)*\s*\}/ or $data=~/\\epsfxsize\s*=\s*\d+\.?\d*\s*(mm|cm|in|pc|pt)*/) {
 2836: 	    my $garbage_data=$data;
 2837: 	    my $fwidth=0;
 2838:             while ($garbage_data=~/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)\s*\}/) {
 2839: 		my $current_length=&recalc($1);
 2840: 		$current_length=~/(\d+\.?\d*)/;
 2841: 		if ($fwidth<$1) {$fwidth=$1;}
 2842: 		$garbage_data=~s/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2843: 	    }
 2844:             while ($garbage_data=~/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2845: 		my $current_length=&recalc($1);
 2846: 		$current_length=~/(\d+\.?\d*)/;
 2847: 		if ($fwidth<$1) {$fwidth=$1;}
 2848: 		$garbage_data=~s/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2849: 	    }
 2850: 	    for (my $c = 0; $c < $colspan; $c++) {
 2851: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2852: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2853: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2854: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2855: 	    }
 2856: 	    $data=~s/\\\\\s*$//; 
 2857: 	} else {  
 2858: 	    $data=~s/^\s+(\S.*)/$1/; 
 2859: 	    $data=~s/(.*\S)\s+$/$1/;
 2860: 	    $data=~s/(\s)+/$1/;
 2861: 	    my ($current_length,$min_length)=(0,0);
 2862: 	    if ($data=~/\\vskip/) {
 2863:                 my $newdata=$data;
 2864: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2865: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2866: 		foreach my $elementdata (@newdata) {
 2867: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2868: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2869:                     my @words=split(/ /,$elementdata);
 2870: 		    foreach my $word (@words) {
 2871: 			my $lengthword=2.5*&LATEX_length($word);
 2872: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2873: 		    }
 2874: 		}
 2875: 	    } else {
 2876: 		$current_length=2.5*&LATEX_length($data);
 2877:                     my @words=split(/ /,$data);
 2878: 		    foreach my $word (@words) {
 2879: 			my $lengthword=2*&LATEX_length($word);
 2880: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2881: 		    }
 2882: 	    }
 2883: 	    for (my $c = 0; $c < $colspan; $c++) {
 2884: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2885: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2886: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2887: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2888: 	    }
 2889: 	}        
 2890:     }
 2891:     # Substitute all of the tables nested in this cell in their appropriate places.
 2892: 
 2893: 
 2894:     my $nested_count = $#{$Apache::londefdef::table[-1]{'include'}}; # This one is constant...
 2895:     for (my $in=0; $in<=$nested_count; $in++) {    
 2896: 	my $nested = shift @{$Apache::londefdef::table[-1]{'include'}};
 2897: 	$nested =~ s/\\end\{tabular\}\\strut\\\\/\\end\{tabular\}/;
 2898: 	# $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2899: 	$data =~ s/\\keephidden\{NEW TABLE ENTRY\}/$nested/;
 2900: 
 2901:     }
 2902:     # Should be be killing off the 'include' elements as they're used up?
 2903: 
 2904:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2905: 
 2906: 
 2907: 
 2908: 
 2909:     #  the colspan array will indicate how many columns will be spanned by this
 2910:     #  cell..this requires that counter_columns also be adjusted accordingly
 2911:     #  so that the next bunch of text goes in the right cell.  Note that since
 2912:     #  counter_columns is incremented in the start_td_tex, we adjust by colspan-1.
 2913:     #
 2914: 
 2915:     $Apache::londefdef::table[-1]{'counter_columns'} += $colspan -1;
 2916:     for (my $i = 0; $i < ($colspan -1); $i++) {
 2917: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1] },'';
 2918:     }
 2919:     for (my $r = 0; $r < $rowspan; $r++) {
 2920: 	$Apache::londefdef::table[-1]{'colspan'}[$current_row+$r][$current_column] = $colspan;
 2921: 	# Put empty text in spanned cols.
 2922: 	
 2923:     }
 2924: 
 2925:     }
 2926: 
 2927:     return '';
 2928: }
 2929: 
 2930: sub end_td {
 2931:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2932:     my $currentstring = '';
 2933:     if ($target eq 'web' || $target eq 'webgrade') {
 2934: 	$currentstring = $token->[2];     
 2935:     } elsif ($target eq 'tex') {
 2936:         $Apache::londefdef::TD_redirection =0;
 2937: 	&end_td_tex($parstack,$parser,$safeeval);
 2938:     }
 2939:     return $currentstring;
 2940: }
 2941: 
 2942: #-- <th> tag (end tag optional)
 2943: sub start_th {
 2944:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2945:     my $currentstring = '';
 2946:     if ($target eq 'web' || $target eq 'webgrade') {
 2947: 	$currentstring = $token->[4];     
 2948:     } elsif ($target eq 'tex') {
 2949: 	$Apache::londefdef::TD_redirection = 1;
 2950: 	&tagg_check('tr','th',$tagstack,$parstack,$parser,$safeeval);
 2951:     } 
 2952:     return $currentstring;
 2953: }   
 2954:     
 2955: sub tagg_check {
 2956:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2957:     my @ar=@$parstack; 
 2958:     for (my $i=$#ar-1;$i>=0;$i--) {
 2959: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2960: 	    &start_th_tex($parstack,$parser,$safeeval);
 2961: 	    last;
 2962: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2963: 	    splice @ar, $i+1;
 2964: 	    &end_th_tex(\@ar,$parser,$safeeval);
 2965: 	    &start_th_tex($parstack,$parser,$safeeval);
 2966: 	    last;
 2967: 	}
 2968:     }
 2969:     return '';
 2970: }
 2971:  
 2972: sub start_th_tex {
 2973:     my ($parstack,$parser,$safeeval) = @_;
 2974: 
 2975:     my $alignment = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef,1);
 2976:     my $rowspan  =  &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 1);
 2977:     my $colspan  =  &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 1);
 2978: 
 2979:     my $config   = cell_config_hash($alignment, $rowspan, $colspan);
 2980:     my $table    = $Apache::londefdef::table[-1];
 2981:     $table->add_cell('\textbf{', $config);
 2982: 
 2983:     #-------------------------------------------------------------------------------------
 2984:     #
 2985:     #  Old table code.
 2986:     #
 2987:     #--------------------------------------------------------------------------------------
 2988: 
 2989:     if (0) {
 2990: 
 2991: 
 2992:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2993:     if ($alignchar eq '') {
 2994: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2995:     }
 2996:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2997:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2998:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2999:     if (defined $TeXwidth) {		
 3000: 	my $current_length=&recalc($TeXwidth);
 3001: 	$current_length=~/(\d+\.?\d*)/;
 3002: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 3003:     }
 3004:     }
 3005: 
 3006:     # Accept xml until the </th> tag.
 3007: 
 3008:     &Apache::lonxml::startredirection();
 3009:     return '';
 3010: }
 3011: 
 3012: sub end_th_tex {
 3013:     my ($parstack,$parser,$safeeval) = @_;
 3014: 
 3015:     my $table = $Apache::londefdef::table[-1];
 3016:     my $text  = &Apache::lonxml::endredirection();
 3017:     $table->append_cell_text($text.'}');
 3018: 
 3019:     #-----------------------------------------------------------------------------
 3020:     #  Old table code:
 3021:     #-----------------------------------------------------------------------------
 3022: 
 3023:     if (0) {
 3024:     my $current_row = $Apache::londefdef::table[-1]{'row_number'};
 3025:     my $data=&Apache::lonxml::endredirection();
 3026:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 3027:     if (defined $TeXwidth) {		
 3028: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3029: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3030: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3031:     } else {
 3032: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 3033: 	    my $garbage_data=$data;
 3034: 	    my $fwidth=0;
 3035:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 3036: 		my $current_length=&recalc($1);
 3037: 		$current_length=~/(\d+\.?\d*)/;
 3038: 		if ($fwidth<$1) {$fwidth=$1;}
 3039: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 3040: 	    }
 3041:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 3042: 		my $current_length=$1;
 3043: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 3044: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 3045: 	    }
 3046: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3047: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 3048: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3049: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3050: 	} else {  
 3051: 	    $data=~s/^\s+(\S.*)/$1/; 
 3052: 	    $data=~s/(.*\S)\s+$/$1/;
 3053: 	    $data=~s/(\s)+/$1/;
 3054: 	    my ($current_length,$min_length)=(0,0);
 3055: 	    if ($data=~/\\vskip/) {
 3056:                 my $newdata=$data;
 3057: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 3058: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 3059: 		foreach my $elementdata (@newdata) {
 3060: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 3061: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 3062:                     my @words=split(/ /,$elementdata);
 3063: 		    foreach my $word (@words) {
 3064: 			my $lengthword=2.5*&LATEX_length($word);
 3065: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 3066: 		    }
 3067: 		}
 3068: 	    } else {
 3069: 		$current_length=2.5*&LATEX_length($data);
 3070:                     my @words=split(/ /,$data);
 3071: 		    foreach my $word (@words) {
 3072: 			my $lengthword=2*&LATEX_length($word);
 3073: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 3074: 		    }
 3075: 	    }
 3076: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3077: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 3078: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 3079: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 3080: 	}        
 3081:     }
 3082: 	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
 3083: 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 3084: 	}
 3085:     #make data bold
 3086:     $data='\textbf{'.$data.'}';
 3087:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 3088:     }
 3089:     return'';
 3090: }
 3091: 
 3092: sub end_th {
 3093:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3094:     my $currentstring = &end_p();	# Close any open <p> in the row.
 3095:     if ($target eq 'web' || $target eq 'webgrade') {
 3096: 	$currentstring .= $token->[2];     
 3097:     } elsif ($target eq 'tex') {
 3098:         $Apache::londefdef::TD_redirection =0;
 3099: 	&end_th_tex($parstack,$parser,$safeeval);
 3100:     }
 3101:     return $currentstring;
 3102: }
 3103:      
 3104: #-- <img> tag (end tag forbidden)
 3105: #
 3106: #  Render the <IMG> tag.
 3107: #     <IMG> has the following attributes (in addition to the 
 3108: #     standard HTML ones:
 3109: #      TeXwrap   - Governs how the tex target will try to wrap text around
 3110: #                  horizontally aligned images.
 3111: #      TeXwidth  - The width of the image when rendered for print (mm).
 3112: #      TeXheight - The height of the image when rendered for print (mm)
 3113: #         (Note there seems to also be support for this as a % of page size)
 3114: #      
 3115: sub start_img {
 3116:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
 3117:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
 3118: 					 undef,1);
 3119:     if (! $src && 
 3120: 	($target eq 'web' || $target eq 'webgrade' || $target eq 'tex')
 3121: 	) { 
 3122: 	my $inside = &Apache::lonxml::get_all_text("/img",$parser,$style);
 3123: 	return '';
 3124:     }
 3125:     &Apache::lonxml::extlink($src);
 3126:     my $currentstring = '';
 3127:     my $scaling = .3;
 3128: 
 3129:    # Render unto browsers that which are the browser's...
 3130: 
 3131:     if ($target eq 'web' || $target eq 'webgrade') {
 3132: 	if ($env{'browser.imagesuppress'} ne 'on') {
 3133: 	    my $enc = ('yes' eq 
 3134: 		       lc(&Apache::lonxml::get_param('encrypturl',$parstack,
 3135: 						     $safeeval)));
 3136: 	    $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
 3137: 							 $enc);
 3138: 	} else {
 3139: 	    my $alttag = &Apache::lonxml::get_param('alt',$parstack,$safeeval,
 3140: 						    undef,1);
 3141: 	    if (!$alttag) {
 3142: 		$alttag = &Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 3143: 						   $src);
 3144: 	    }
 3145: 	    $currentstring.='[IMAGE: '.$alttag.']';
 3146: 	}
 3147: 
 3148: 	# and render unto TeX that which is LaTeX
 3149: 
 3150:     } elsif ($target eq 'tex') {
 3151: 	#
 3152: 	#  The alignment will require some superstructure to be put around
 3153: 	#  the \includegraphics stuff.  At present we can only partially
 3154: 	#  simulate the alignments offered by html.
 3155: 	#
 3156: 	#
 3157: 	my $align = lc(&Apache::lonxml::get_param('align', 
 3158: 						  $parstack,
 3159: 						  $safeeval,
 3160: 						  undef,1));
 3161: 	if(!$align) {
 3162: 		$align = "bottom";	# This is html's default so it's ours too.
 3163: 	}
 3164: 	#
 3165: 	&Apache::lonxml::debug("Alignemnt = $align");
 3166: 	#  LaTeX's image/text wrapping is really bad since it wants to
 3167: 	#  make figures float.  
 3168:         #   The user has the optional parameter (applicable only to l/r
 3169: 	# alignment to use the picins/parpic directive to get wrapped text
 3170: 	# this is also imperfect.. that's why we give them a choice...
 3171: 	# so they can't yell at us for our choice.
 3172: 	#
 3173: 	my $latex_rendering = &Apache::lonxml::get_param('TeXwrap',
 3174: 							    $parstack,
 3175: 							    $safeeval,
 3176: 							    undef,0);
 3177: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
 3178: 	if(!$latex_rendering) {
 3179: 		$latex_rendering = "texwrap";
 3180: 	}
 3181: 	# using texwrap inside a table does not work. So, if after all of this,
 3182: 	# texwrap is on, we turn it off if we detect we're in a table:
 3183: 	#
 3184: 	if (($latex_rendering eq 'texwrap') && &is_inside_of($tagstack, "table")) {
 3185: 	    $latex_rendering = 'parpic';
 3186: 	}
 3187: 
 3188: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
 3189: 
 3190: 	#if original bmp/gif/jpg/png file exist do following:
 3191: 	my $origsrc=$src;
 3192: 	my ($path,$file) = &get_eps_image($src);
 3193: 	# &Apache::lonnet::logthis("Image source: $src result: $path $file");
 3194: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 3195: 	&Apache::lonxml::debug("path = $path file = $file src = $src");
 3196: 	if (-e $src) {
 3197: 	    &Apache::lonxml::debug("$src exists");
 3198: 	    my ($height_param,$width_param)=
 3199: 		&image_size($origsrc,0.3,$parstack,$safeeval);
 3200: 	    my $size;
 3201: 	    if ($width_param)  { $size.='width='.$width_param.' mm,'; }
 3202: 	    if ($height_param) { $size.='height='.$height_param.' mm]'; }
 3203: 	    # Default size if not able to extract that (e.g. eps image).
 3204: 	    
 3205: 	    # &Apache::lonnet::logthis("Size = $size");
 3206: 	    
 3207: 	    $size='['.$size;
 3208: 	    $size=~s/,$/]/; 
 3209: 	    $currentstring .= '\graphicspath{{'.$path.'}}'
 3210: 		.'\includegraphics'.$size.'{'.$file.'} ';
 3211: 	    my $closure;
 3212: 	    ($currentstring, $closure) = &align_latex_image($align, 
 3213: 							    $latex_rendering, 
 3214: 							    $currentstring, 
 3215: 							    $width_param, 
 3216: 							    $height_param);
 3217: 	    $currentstring .= $closure;
 3218: 						
 3219: 	} else {
 3220: 	    &Apache::lonxml::debug("$src does not exist");
 3221: 	    #original image file doesn't exist so check the alt attribute
 3222: 	    my $alt = 
 3223: 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
 3224: 	    unless ($alt) {
 3225: 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 3226: 	    }
 3227: 
 3228: 	    if ($alt) { $currentstring .= ' '.$alt.' '; }
 3229: 	}
 3230: 
 3231: 	# And here's where the semi-quote breaks down: allow the user
 3232:         # to edit the beast as well by rendering the problem for edit:
 3233:     } elsif ($target eq 'edit') {
 3234:         my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
 3235: 	$currentstring .=&Apache::edit::tag_start($target,$token);
 3236: 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
 3237: 	    &Apache::edit::browse('src',undef,'alt',$only).' '.
 3238: 	    &Apache::edit::search('src',undef,'alt').'<br />';
 3239: 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
 3240: 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
 3241: 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
 3242: 	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
 3243: 	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
 3244: 	$currentstring .=&Apache::edit::select_arg('Alignment:','align',
 3245: 						   ['','bottom','middle','top','left','right'],$token,5);
 3246: 	$currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap',
 3247: 						   ['', 'none','parbox', 'parpic', 'wrapfigure'], $token, 2);
 3248: 	$currentstring .=&Apache::edit::select_arg('Encrypt URL:','encrypturl',
 3249: 						   ['no','yes'], $token, 2);
 3250: 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 3251: 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
 3252: 	my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
 3253: 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
 3254: 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
 3255: 
 3256:         if ($token->[2]{'src'}=~/\$/) {
 3257:            $currentstring.='Variable image source';
 3258:         } else {
 3259: 	   $currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
 3260: 	   if ($width) { $currentstring.=' width="'.$width.'" '; }
 3261: 	   if ($height) { $currentstring.=' height="'.$height.'" '; }
 3262: 	   $currentstring .= ' />';
 3263:         }
 3264:     } elsif ($target eq 'modified') {
 3265: 	my ($osrc,$owidth,$oheight)=
 3266: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 3267: 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
 3268: 					     $safeeval,'src','alt','align',
 3269: 					     'TeXwidth','TeXheight', 'TeXwrap',
 3270: 					     'width','height','encrypturl');
 3271: 	my ($nsrc,$nwidth,$nheight)=
 3272: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 3273: 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
 3274: 	&image_replication($loc);
 3275: 	my ($iwidth,$iheight);
 3276: 	if (-e $loc) {
 3277: 	    my $image = Image::Magick->new;
 3278: 	    $image->Read($loc);
 3279: 	    ($iwidth, $iheight) = ($image->Get('width'),
 3280: 				   $image->Get('height'));
 3281: 	}
 3282: 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
 3283: 	    # changed image or no size specified,
 3284:             # if they didn't explicitly change the 
 3285:             # width or height use the ones from the image
 3286: 	    if ($iwidth && $iheight) {
 3287: 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
 3288: 		    $token->[2]{'width'} = $iwidth;$ctag=1;
 3289: 		}
 3290: 		if ($oheight == $nheight || (!$nwidth && !$nheight)) {
 3291: 		    $token->[2]{'height'}=$iheight;$ctag=1;
 3292: 		}
 3293: 	    }
 3294: 	}
 3295: 	my ($cwidth,$cheight)=($token->[2]{'width'},$token->[2]{'height'});
 3296: 	# if we don't have a width or height
 3297: 	if ($iwidth && $cwidth && !$cheight) {
 3298: 	    $token->[2]{'height'}=int(($cwidth/$iwidth)*$iheight);$ctag=1;
 3299: 	}
 3300: 	if ($iheight && $cheight && !$cwidth) {
 3301: 	    $token->[2]{'width'}=int(($cheight/$iheight)*$iwidth);$ctag=1;
 3302: 	}
 3303: 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
 3304:     }
 3305: 
 3306:     return $currentstring;
 3307: }
 3308: 
 3309: sub end_img {
 3310:     my ($target,$token) = @_;
 3311:     my $currentstring = '';
 3312:     if ($target eq 'web' || $target eq 'webgrade') {
 3313: 	$currentstring = $token->[2];
 3314:     } elsif ($target eq 'tex') {
 3315: 	$currentstring = '';
 3316:     }
 3317:     return $currentstring;
 3318: }
 3319: 
 3320: #-- <applet> tag (end tag required)
 3321: sub start_applet {
 3322:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3323:     
 3324:     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
 3325:     &Apache::lonxml::extlink($code);
 3326:     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
 3327: 					   undef,1);
 3328:     &Apache::lonxml::extlink($archive);
 3329:     my $currentstring = '';
 3330:     if ($target eq 'web' || $target eq 'webgrade') {
 3331: 	if ($env{'browser.appletsuppress'} ne 'on') {
 3332: 	    $currentstring = &Apache::lonenc::encrypt_ref($token,
 3333: 							  {'code'=>$code,
 3334: 							   'archive'=>$archive}
 3335: 							  );
 3336: 	} else {
 3337: 	    my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 3338: 						   $safeeval,undef,1);
 3339: 	    unless ($alttag) {
 3340: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 3341: 						 $code);
 3342: 	    }
 3343: 	    $currentstring='[APPLET: '.$alttag.']';
 3344: 	}
 3345:     } elsif ($target eq 'tex') {
 3346: 	# Turn off some stuff we can't be inside thank you LaTeX
 3347: 	
 3348: 
 3349: 	my $restart_sub = 0;
 3350: 	my $restart_sup = 0;
 3351: 
 3352: 	# Since <sub> and <sup> are simple tags it's ok to turn off/on
 3353: 	# using the start_ stop_ functions.. those tags only care about
 3354: 	# $target.
 3355: 
 3356: 	if (&is_inside_of($tagstack, "sub")) {
 3357: 	    $restart_sub = 1;
 3358: 	    $currentstring .= &end_sub($target, $token, $tagstack, 
 3359: 				       $parstack, $parser, $safeeval);
 3360: 	}
 3361: 	if (&is_inside_of($tagstack, "sup")) {
 3362: 	    $restart_sup = 1;
 3363: 	    $currentstring .= &end_sup($target, $token, $tagstack,
 3364: 				       $parstack, $parser, $safeeval);
 3365: 	}
 3366: 
 3367: 	# Now process the applet; just replace it with its alt attribute.
 3368: 
 3369: 	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 3370: 					       $safeeval,undef,1);
 3371: 	unless ($alttag) {
 3372: 	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
 3373: 						undef,1);
 3374: 	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 3375: 					     $code);
 3376: 	}
 3377: 	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
 3378: 	    '.}\end{center}';
 3379: 
 3380: 	# Turn stuff back on that we can't be inside of.
 3381: 
 3382: 	if ($restart_sub) {
 3383: 	    $currentstring .= &start_sub($target, $token, $tagstack,
 3384: 					$parstack, $parser, $safeeval);
 3385: 	}
 3386: 	if ($restart_sup) {
 3387: 	    $currentstring .= &start_sup($target, $token, $tagstack,
 3388: 					 $parstack, $parser, $safeeval);
 3389: 	}
 3390:     } 
 3391:     return $currentstring;
 3392: }
 3393: 
 3394: sub end_applet {
 3395:     my ($target,$token) = @_;
 3396:     my $currentstring = '';
 3397:     if ($target eq 'web' || $target eq 'webgrade') {
 3398: 	$currentstring = $token->[2];
 3399:     } elsif ($target eq 'tex') {
 3400:     } 
 3401:     return $currentstring;
 3402: }
 3403: 
 3404: #-- <embed> tag (end tag optional/required)
 3405: sub start_embed {    
 3406:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3407:     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3408:     &Apache::lonxml::extlink($src);
 3409:     my $currentstring = '';
 3410:     if ($target eq 'web' || $target eq 'webgrade') {
 3411: 	if ($env{'browser.embedsuppress'} ne 'on') {
 3412: 	    $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src});
 3413: 	} else {
 3414: 	    my $alttag=&Apache::lonxml::get_param
 3415: 		('alt',$parstack,$safeeval,undef,1);
 3416: 	    unless ($alttag) {
 3417: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 3418: 	    }
 3419: 	    $currentstring='[EMBED: '.$alttag.']';
 3420: 	}
 3421:     } elsif ($target eq 'tex') {
 3422:     } 
 3423:     return $currentstring;
 3424: }
 3425: 
 3426: sub end_embed {
 3427:     my ($target,$token) = @_;
 3428:     my $currentstring = '';
 3429:     if ($target eq 'web' || $target eq 'webgrade') {
 3430: 	$currentstring = $token->[2];     
 3431:     } elsif ($target eq 'tex') {  
 3432:     } 
 3433:     return $currentstring;
 3434: }
 3435: 
 3436: #-- <param> tag (end tag forbidden)
 3437: sub start_param {
 3438:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3439:     if (&Apache::lonxml::get_param('name',$parstack,
 3440: 				   $safeeval,undef,1)=~/^cabbase$/i) {
 3441: 	my $value=&Apache::lonxml::get_param('value',$parstack,
 3442: 					     $safeeval,undef,1);
 3443: 	&Apache::lonxml::extlink($value);
 3444:     } 
 3445:   
 3446:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3447:     &Apache::lonxml::extlink($src);
 3448:     my $currentstring = '';
 3449:     if ($target eq 'web' || $target eq 'webgrade') {
 3450: 	my %toconvert;
 3451: 	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3452: 	if ($src) { $toconvert{'src'}= $src; }
 3453: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval,
 3454: 					    undef,1);
 3455: 	if ($name=~/^cabbase$/i) {
 3456: 	    $toconvert{'value'}=&Apache::lonxml::get_param('value',$parstack,
 3457: 							   $safeeval,undef,1);
 3458: 	}
 3459: 	$currentstring = &Apache::lonenc::encrypt_ref($token,\%toconvert);
 3460:     } elsif ($target eq 'tex') {
 3461:     } 
 3462:     return $currentstring;
 3463: }
 3464: 
 3465: sub end_param {
 3466:     my ($target,$token) = @_;
 3467:     my $currentstring = '';
 3468:     if ($target eq 'web' || $target eq 'webgrade') {
 3469: 	$currentstring = $token->[2];     
 3470:     } elsif ($target eq 'tex') {
 3471:     } 
 3472:     return $currentstring;
 3473: }
 3474: 
 3475: #-- <allow> tag
 3476: sub start_allow {
 3477:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3478:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3479:     &Apache::lonxml::extlink($src);
 3480: 
 3481:     if ($target eq 'tex') { &image_replication($src); }
 3482:     my $result;
 3483:     if ($target eq 'edit') {
 3484: 	$result .=&Apache::edit::tag_start($target,$token);
 3485: 	$result .=&Apache::edit::text_arg('File Spec:','src',$token,70);
 3486: 	$result .=&Apache::edit::end_row();#.&Apache::edit::start_spanning_row();
 3487:     } elsif ($target eq 'modified') {
 3488: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
 3489: 						     $safeeval,'src');
 3490: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 3491:     }
 3492:     return $result;
 3493: }
 3494: 
 3495: sub end_allow {
 3496:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3497:     if ( $target eq 'edit') { return (&Apache::edit::end_table()); }
 3498:     return '';
 3499: }
 3500: 
 3501: #-- Frames (end tag required)
 3502: #-- <frameset>
 3503: sub start_frameset {
 3504:     my ($target,$token) = @_;
 3505:     my $currentstring = '';	# Close any pending para.
 3506:     if ($target eq 'web' || $target eq 'webgrade') { 
 3507: 	$currentstring = 
 3508: 	    &Apache::loncommon::start_page($Apache::londefdef::title,
 3509: 					   $Apache::londefdef::head,
 3510: 					   {'add_entries'    => $token->[2],
 3511: 					    'no_title'       => 1,
 3512: 					    'force_register' => 1,
 3513: 					    'frameset'       => 1,});
 3514: 
 3515:     }
 3516:     return $currentstring;
 3517: }
 3518: 
 3519: sub end_frameset {
 3520:     my ($target,$token) = @_;
 3521:     my $currentstring = '';
 3522:     if ($target eq 'web' || $target eq 'webgrade') {
 3523: 	$currentstring = $token->[2];
 3524:     }
 3525:     return $currentstring;
 3526: }
 3527: 
 3528: #-- <xmp> (end tag required)
 3529: sub start_xmp {
 3530:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3531:     my $currentstring = '';
 3532:     if ($target eq 'web' || $target eq 'webgrade') {
 3533: 	$currentstring .= $token->[4];
 3534:     } elsif ($target eq 'tex') {
 3535: 	$currentstring .= '\begin{verbatim}';
 3536:     } 
 3537:     return $currentstring;
 3538: }
 3539: 
 3540: sub end_xmp {
 3541:     my ($target,$token) = @_;
 3542:     my $currentstring = '';
 3543:     if ($target eq 'web' || $target eq 'webgrade') {
 3544: 	$currentstring .= $token->[2];
 3545:     } elsif ($target eq 'tex') {
 3546: 	$currentstring .= '\end{verbatim}';
 3547:     }
 3548:     return $currentstring;
 3549: }
 3550: 
 3551: #-- <pre> (end tag required)
 3552: sub start_pre {
 3553:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3554:     my $currentstring = &end_p();	# close off pending <p>
 3555:     if ($target eq 'web' || $target eq 'webgrade') {
 3556: 	$currentstring .= $token->[4];
 3557:     } elsif ($target eq 'tex') {
 3558: 	$currentstring .= '\begin{verbatim}';
 3559: 	&Apache::lonxml::disable_LaTeX_substitutions();
 3560:     } 
 3561:     return $currentstring;
 3562: }
 3563: 
 3564: sub end_pre {
 3565:     my ($target,$token) = @_;
 3566:     my $currentstring = '';
 3567:     if ($target eq 'web' || $target eq 'webgrade') {
 3568: 	$currentstring .= $token->[2];
 3569:     } elsif ($target eq 'tex') {
 3570: 	$currentstring .= '\end{verbatim}';
 3571: 	&Apache::lonxml::enable_LaTeX_substitutions();
 3572:     }
 3573:     return $currentstring;
 3574: }
 3575: 
 3576: #-- <insert>
 3577: sub start_insert {
 3578:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3579:     my $currentstring = '';
 3580:     if ($target eq 'web' || $target eq 'webgrade') {
 3581: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3582: 	$currentstring .= '<b>'.$display.'</b>';;
 3583:     }
 3584:     return $currentstring;
 3585: }
 3586: 
 3587: sub end_insert {
 3588:     my ($target,$token) = @_;
 3589:     my $currentstring = '';
 3590:     if ($target eq 'web' || $target eq 'webgrade') {
 3591: 	$currentstring .= '';
 3592:     }
 3593:     return $currentstring;
 3594: }
 3595: 
 3596: #-- <externallink>
 3597: sub start_externallink {
 3598:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3599:     my $currentstring = '';
 3600:     if ($target eq 'web' || $target eq 'webgrade') {
 3601: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3602: 	$currentstring .= '<b>'.$display.'</b>';;
 3603:     }
 3604:     return $currentstring;
 3605: }
 3606: 
 3607: sub end_externallink {
 3608:     my ($target,$token) = @_;
 3609:     my $currentstring = '';
 3610:     if ($target eq 'web' || $target eq 'webgrade') {
 3611: 	$currentstring .= '';
 3612:     }
 3613:     return $currentstring;
 3614: }
 3615: 
 3616: #-- <blankspace heigth="">
 3617: sub start_blankspace {
 3618:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3619:     my $currentstring = &end_p();	# closes off any unclosed <p>
 3620:     if ($target eq 'tex') {
 3621: 	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
 3622: 	$currentstring .= '\vskip '.$howmuch.' ';
 3623:     }
 3624:     return $currentstring;
 3625: }
 3626: 
 3627: sub end_blankspace {
 3628:     my ($target,$token) = @_;
 3629:     my $currentstring = '';
 3630:     if ($target eq 'tex') {
 3631: 	$currentstring .= '';
 3632:     }
 3633:     return $currentstring;
 3634: }
 3635: 
 3636: #-- <abbr> tag (end tag required)
 3637: sub start_abbr {
 3638:     my ($target,$token) = @_;
 3639:     my $currentstring = '';
 3640:     if ($target eq 'web' || $target eq 'webgrade') {
 3641: 	$currentstring = $token->[4];     
 3642:     } 
 3643:     return $currentstring;
 3644: }
 3645: 
 3646: sub end_abbr {
 3647:     my ($target,$token) = @_;
 3648:     my $currentstring = '';
 3649:     if ($target eq 'web' || $target eq 'webgrade') {
 3650: 	$currentstring = $token->[2];    
 3651:     } 
 3652:     return $currentstring;
 3653: }
 3654: 
 3655: #-- <acronym> tag (end tag required)
 3656: sub start_acronym {
 3657:     my ($target,$token) = @_;
 3658:     my $currentstring = '';
 3659:     if ($target eq 'web' || $target eq 'webgrade') {
 3660: 	$currentstring = $token->[4];     
 3661:     } 
 3662:     return $currentstring;
 3663: }
 3664: 
 3665: sub end_acronym {
 3666:     my ($target,$token) = @_;
 3667:     my $currentstring = '';
 3668:     if ($target eq 'web' || $target eq 'webgrade') {
 3669: 	$currentstring = $token->[2];    
 3670:     } 
 3671:     return $currentstring;
 3672: }
 3673: 
 3674: #-- <area> tag (end tag forbidden)
 3675: sub start_area {
 3676:     my ($target,$token) = @_;
 3677:     my $currentstring = '';
 3678:     if ($target eq 'web' || $target eq 'webgrade') {
 3679: 	$currentstring = $token->[4];     
 3680:     } 
 3681:     return $currentstring;
 3682: }
 3683: 
 3684: sub end_area {
 3685:     my ($target,$token) = @_;
 3686:     my $currentstring = '';
 3687:     if ($target eq 'web' || $target eq 'webgrade') {
 3688: 	$currentstring = $token->[2];    
 3689:     } 
 3690:     return $currentstring;
 3691: }
 3692: 
 3693: #-- <base> tag (end tag forbidden)
 3694: sub start_base {
 3695:     my ($target,$token) = @_;
 3696:     my $currentstring = '';
 3697:     if ($target eq 'web' || $target eq 'webgrade') {
 3698: 	$currentstring = $token->[4];     
 3699:     }
 3700:     return $currentstring;
 3701: }
 3702: 
 3703: sub end_base {
 3704:     my ($target,$token) = @_;
 3705:     my $currentstring = '';
 3706:     if ($target eq 'web' || $target eq 'webgrade') {
 3707: 	$currentstring = $token->[2];    
 3708:     } 
 3709:     return $currentstring;
 3710: }
 3711: 
 3712: #-- <bdo> tag (end tag required)
 3713: sub start_bdo {
 3714:     my ($target,$token) = @_;
 3715:     my $currentstring = '';
 3716:     if ($target eq 'web' || $target eq 'webgrade') {
 3717: 	$currentstring = $token->[4];     
 3718:     } 
 3719:     return $currentstring;
 3720: }
 3721: 
 3722: sub end_bdo {
 3723:     my ($target,$token) = @_;
 3724:     my $currentstring = '';
 3725:     if ($target eq 'web' || $target eq 'webgrade') {
 3726: 	$currentstring = $token->[2];    
 3727:     } 
 3728:     return $currentstring;
 3729: }
 3730: 
 3731: #-- <bgsound> tag (end tag optional)
 3732: sub start_bgsound {
 3733:     my ($target,$token) = @_;
 3734:     my $currentstring = '';
 3735:     if ($target eq 'web' || $target eq 'webgrade') {
 3736: 	$currentstring = $token->[4];     
 3737:     } 
 3738:     return $currentstring;
 3739: }
 3740: 
 3741: sub end_bgsound {
 3742:     my ($target,$token) = @_;
 3743:     my $currentstring = '';
 3744:     if ($target eq 'web' || $target eq 'webgrade') {
 3745: 	$currentstring = $token->[2];    
 3746:     } 
 3747:     return $currentstring;
 3748: }
 3749: 
 3750: #-- <blink> tag (end tag required)
 3751: sub start_blink {
 3752:     my ($target,$token) = @_;
 3753:     my $currentstring = '';
 3754:     if ($target eq 'web' || $target eq 'webgrade') {
 3755: 	$currentstring = $token->[4];     
 3756:     } 
 3757:     return $currentstring;
 3758: }
 3759: 
 3760: sub end_blink {
 3761:     my ($target,$token) = @_;
 3762:     my $currentstring = '';
 3763:     if ($target eq 'web' || $target eq 'webgrade') {
 3764: 	$currentstring = $token->[2];    
 3765:     } 
 3766:     return $currentstring;
 3767: }
 3768: 
 3769: #-- <blockquote> tag (end tag required)
 3770: sub start_blockquote {
 3771:     my ($target,$token) = @_;
 3772:     my $currentstring = &end_p();	# Close any unclosed <p>
 3773:     if ($target eq 'web' || $target eq 'webgrade') {
 3774: 	$currentstring .= $token->[4];     
 3775:     } 
 3776:     if ($target eq 'tex') {
 3777: 	$currentstring .= '\begin{quote}';
 3778:     }
 3779:     return $currentstring;
 3780: }
 3781: 
 3782: sub end_blockquote {
 3783:     my ($target,$token) = @_;
 3784:     my $currentstring = '';
 3785:     if ($target eq 'web' || $target eq 'webgrade') {
 3786: 	$currentstring = $token->[2];    
 3787:     } 
 3788:     if ($target eq 'tex') {
 3789: 	$currentstring = '\end{quote}';
 3790:     }
 3791:     return $currentstring;
 3792: }
 3793: 
 3794: #-- <button> tag (end tag required)
 3795: sub start_button {
 3796:     my ($target,$token) = @_;
 3797:     my $currentstring = '';
 3798:     if ($target eq 'web' || $target eq 'webgrade') {
 3799: 	$currentstring = $token->[4];     
 3800:     } 
 3801:     return $currentstring;
 3802: }
 3803: 
 3804: sub end_button {
 3805:     my ($target,$token) = @_;
 3806:     my $currentstring = '';
 3807:     if ($target eq 'web' || $target eq 'webgrade') {
 3808: 	$currentstring = $token->[2];    
 3809:     } 
 3810:     return $currentstring;
 3811: }
 3812: 
 3813: #-- <caption> tag (end tag required)
 3814: sub start_caption {
 3815:     my ($target,$token) = @_;
 3816:     my $currentstring = '';
 3817:     if ($target eq 'web' || $target eq 'webgrade') {
 3818: 	$currentstring = $token->[4];     
 3819:     } 
 3820:     return $currentstring;
 3821: }
 3822: 
 3823: sub end_caption {
 3824:     my ($target,$token) = @_;
 3825:     my $currentstring = '';
 3826:     if ($target eq 'web' || $target eq 'webgrade') {
 3827: 	$currentstring = $token->[2];    
 3828:     } 
 3829:     return $currentstring;
 3830: }
 3831: 
 3832: #-- <col> tag (end tag forbdden)
 3833: sub start_col {
 3834:     my ($target,$token) = @_;
 3835:     my $currentstring = '';
 3836:     if ($target eq 'web' || $target eq 'webgrade') {
 3837: 	$currentstring = $token->[4];     
 3838:     } 
 3839:     return $currentstring;
 3840: }
 3841: 
 3842: sub end_col {
 3843:     my ($target,$token) = @_;
 3844:     my $currentstring = '';
 3845:     if ($target eq 'web' || $target eq 'webgrade') {
 3846: 	$currentstring = $token->[2];    
 3847:     } 
 3848:     return $currentstring;
 3849: }
 3850: 
 3851: #-- <colgroup> tag (end tag optional)
 3852: sub start_colgroup {
 3853:     my ($target,$token) = @_;
 3854:     my $currentstring = '';
 3855:     if ($target eq 'web' || $target eq 'webgrade') {
 3856: 	$currentstring = $token->[4];     
 3857:     } 
 3858:     return $currentstring;
 3859: }
 3860: 
 3861: sub end_colgroup {
 3862:     my ($target,$token) = @_;
 3863:     my $currentstring = '';
 3864:     if ($target eq 'web' || $target eq 'webgrade') {
 3865: 	$currentstring = $token->[2];    
 3866:     } 
 3867:     return $currentstring;
 3868: }
 3869: 
 3870: #-- <del> tag (end tag required)
 3871: sub start_del {
 3872:     my ($target,$token) = @_;
 3873:     my $currentstring = '';
 3874:     if ($target eq 'web' || $target eq 'webgrade') {
 3875: 	$currentstring = $token->[4];     
 3876:     } 
 3877:     return $currentstring;
 3878: }
 3879: 
 3880: sub end_del {
 3881:     my ($target,$token) = @_;
 3882:     my $currentstring = '';
 3883:     if ($target eq 'web' || $target eq 'webgrade') {
 3884: 	$currentstring = $token->[2];    
 3885:     } 
 3886:     return $currentstring;
 3887: }
 3888: 
 3889: #-- <fieldset> tag (end tag required)
 3890: sub start_fieldset {
 3891:     my ($target,$token) = @_;
 3892:     my $currentstring = '';
 3893:     if ($target eq 'web' || $target eq 'webgrade') {
 3894: 	$currentstring = $token->[4];     
 3895:     } 
 3896:     return $currentstring;
 3897: }
 3898: 
 3899: sub end_fieldset {
 3900:     my ($target,$token) = @_;
 3901:     my $currentstring = '';
 3902:     if ($target eq 'web' || $target eq 'webgrade') {
 3903: 	$currentstring = $token->[2];    
 3904:     } 
 3905:     return $currentstring;
 3906: }
 3907: 
 3908: #-- <frame> tag (end tag forbidden)
 3909: sub start_frame {
 3910:     my ($target,$token) = @_;
 3911:     my $currentstring = '';
 3912:     if ($target eq 'web' || $target eq 'webgrade') {
 3913: 	$currentstring = $token->[4];     
 3914:     } 
 3915:     return $currentstring;
 3916: }
 3917: 
 3918: sub end_frame {
 3919:     my ($target,$token) = @_;
 3920:     my $currentstring = '';
 3921:     if ($target eq 'web' || $target eq 'webgrade') {
 3922: 	$currentstring = $token->[2];    
 3923:     } 
 3924:     return $currentstring;
 3925: }
 3926: 
 3927: #-- <iframe> tag (end tag required)
 3928: sub start_iframe {
 3929:     my ($target,$token) = @_;
 3930:     my $currentstring = '';
 3931:     if ($target eq 'web' || $target eq 'webgrade') {
 3932: 	$currentstring = $token->[4];     
 3933:     } 
 3934:     return $currentstring;
 3935: }
 3936: 
 3937: sub end_iframe {
 3938:     my ($target,$token) = @_;
 3939:     my $currentstring = '';
 3940:     if ($target eq 'web' || $target eq 'webgrade') {
 3941: 	$currentstring = $token->[2];    
 3942:     } 
 3943:     return $currentstring;
 3944: }
 3945: 
 3946: #-- <ins> tag (end tag required)
 3947: sub start_ins {
 3948:     my ($target,$token) = @_;
 3949:     my $currentstring = '';
 3950:     if ($target eq 'web' || $target eq 'webgrade') {
 3951: 	$currentstring = $token->[4];     
 3952:     } 
 3953:     return $currentstring;
 3954: }
 3955: 
 3956: sub end_ins {
 3957:     my ($target,$token) = @_;
 3958:     my $currentstring = '';
 3959:     if ($target eq 'web' || $target eq 'webgrade') {
 3960: 	$currentstring = $token->[2];    
 3961:     } 
 3962:     return $currentstring;
 3963: }
 3964: 
 3965: #-- <isindex> tag (end tag forbidden)
 3966: sub start_isindex {
 3967:     my ($target,$token) = @_;
 3968:     my $currentstring = '';
 3969:     if ($target eq 'web' || $target eq 'webgrade') {
 3970: 	$currentstring = $token->[4];     
 3971:     } 
 3972:     return $currentstring;
 3973: }
 3974: 
 3975: sub end_isindex {
 3976:     my ($target,$token) = @_;
 3977:     my $currentstring = '';
 3978:     if ($target eq 'web' || $target eq 'webgrade') {
 3979: 	$currentstring = $token->[2];    
 3980:     } 
 3981:     return $currentstring;
 3982: }
 3983: 
 3984: #-- <keygen> tag (end tag forbidden)
 3985: sub start_keygen {
 3986:     my ($target,$token) = @_;
 3987:     my $currentstring = '';
 3988:     if ($target eq 'web' || $target eq 'webgrade') {
 3989: 	$currentstring = $token->[4];     
 3990:     } 
 3991:     return $currentstring;
 3992: }
 3993: 
 3994: sub end_keygen {
 3995:     my ($target,$token) = @_;
 3996:     my $currentstring = '';
 3997:     if ($target eq 'web' || $target eq 'webgrade') {
 3998: 	$currentstring = $token->[2];    
 3999:     } 
 4000:     return $currentstring;
 4001: }
 4002: 
 4003: #-- <label> tag
 4004: sub start_label {
 4005:     my ($target,$token) = @_;
 4006:     my $currentstring = '';
 4007:     if ($target eq 'web' || $target eq 'webgrade') {
 4008: 	$currentstring = $token->[4];     
 4009:     } 
 4010:     return $currentstring;
 4011: }
 4012: 
 4013: sub end_label {
 4014:     my ($target,$token) = @_;
 4015:     my $currentstring = '';
 4016:     if ($target eq 'web' || $target eq 'webgrade') {
 4017: 	$currentstring = $token->[2];    
 4018:     } 
 4019:     return $currentstring;
 4020: }
 4021: 
 4022: #-- <layer> tag (end tag required)
 4023: sub start_layer {
 4024:     my ($target,$token) = @_;
 4025:     my $currentstring = '';
 4026:     if ($target eq 'web' || $target eq 'webgrade') {
 4027: 	$currentstring = $token->[4];     
 4028:     } 
 4029:     return $currentstring;
 4030: }
 4031: 
 4032: sub end_layer {
 4033:     my ($target,$token) = @_;
 4034:     my $currentstring = '';
 4035:     if ($target eq 'web' || $target eq 'webgrade') {
 4036: 	$currentstring = $token->[2];    
 4037:     } 
 4038:     return $currentstring;
 4039: }
 4040: 
 4041: #-- <legend> tag (end tag required)
 4042: sub start_legend {
 4043:     my ($target,$token) = @_;
 4044:     my $currentstring = '';
 4045:     if ($target eq 'web' || $target eq 'webgrade') {
 4046: 	$currentstring = $token->[4];     
 4047:     } 
 4048:     return $currentstring;
 4049: }
 4050: 
 4051: sub end_legend {
 4052:     my ($target,$token) = @_;
 4053:     my $currentstring = '';
 4054:     if ($target eq 'web' || $target eq 'webgrade') {
 4055: 	$currentstring = $token->[2];    
 4056:     } 
 4057:     return $currentstring;
 4058: }
 4059: 
 4060: #-- <link> tag (end tag forbidden)
 4061: sub start_link {
 4062:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 4063:     my $currentstring = '';
 4064:     if ($target eq 'web' || $target eq 'webgrade') {
 4065: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 4066: 					    undef,1);
 4067: 	&Apache::lonxml::extlink($href);
 4068: 	$currentstring = $token->[4];     
 4069:     } 
 4070:     return $currentstring;
 4071: }
 4072: 
 4073: sub end_link {
 4074:     my ($target,$token) = @_;
 4075:     my $currentstring = '';
 4076:     if ($target eq 'web' || $target eq 'webgrade') {
 4077: 	$currentstring = $token->[2];    
 4078:     } 
 4079:     return $currentstring;
 4080: }
 4081: 
 4082: #-- <marquee> tag (end tag optional)
 4083: sub start_marquee {
 4084:     my ($target,$token) = @_;
 4085:     my $currentstring = '';
 4086:     if ($target eq 'web' || $target eq 'webgrade') {
 4087: 	$currentstring = $token->[4];     
 4088:     } 
 4089:     return $currentstring;
 4090: }
 4091: 
 4092: sub end_marquee {
 4093:     my ($target,$token) = @_;
 4094:     my $currentstring = '';
 4095:     if ($target eq 'web' || $target eq 'webgrade') {
 4096: 	$currentstring = $token->[2];    
 4097:     } 
 4098:     return $currentstring;
 4099: }
 4100: 
 4101: #-- <multicol> tag (end tag required)
 4102: sub start_multicol {
 4103:     my ($target,$token) = @_;
 4104:     my $currentstring = &end_p();	# Close any pending <p>
 4105:     if ($target eq 'web' || $target eq 'webgrade') {
 4106: 	$currentstring .= $token->[4];     
 4107:     } 
 4108:     return $currentstring;
 4109: }
 4110: 
 4111: sub end_multicol {
 4112:     my ($target,$token) = @_;
 4113:     my $currentstring = '';
 4114:     if ($target eq 'web' || $target eq 'webgrade') {
 4115: 	$currentstring = $token->[2];    
 4116:     } 
 4117:     return $currentstring;
 4118: }
 4119: 
 4120: #-- <nobr> tag (end tag required)
 4121: sub start_nobr {
 4122:     my ($target,$token) = @_;
 4123:     my $currentstring = '';
 4124:     if ($target eq 'web' || $target eq 'webgrade') {
 4125: 	$currentstring = $token->[4];     
 4126:     }  elsif ($target eq 'tex') {
 4127: 	$currentstring='\mbox{';
 4128:     }
 4129:     return $currentstring;
 4130: }
 4131: 
 4132: sub end_nobr {
 4133:     my ($target,$token) = @_;
 4134:     my $currentstring = '';
 4135:     if ($target eq 'web' || $target eq 'webgrade') {
 4136: 	$currentstring = $token->[2];    
 4137:     }   elsif ($target eq 'tex') {
 4138: 	$currentstring='}';
 4139:     }
 4140:     return $currentstring;
 4141: }
 4142: 
 4143: #-- <noembed> tag (end tag required)
 4144: sub start_noembed {
 4145:     my ($target,$token) = @_;
 4146:     my $currentstring = '';
 4147:     if ($target eq 'web' || $target eq 'webgrade') {
 4148: 	$currentstring = $token->[4];     
 4149:     } 
 4150:     return $currentstring;
 4151: }
 4152: 
 4153: sub end_noembed {
 4154:     my ($target,$token) = @_;
 4155:     my $currentstring = '';
 4156:     if ($target eq 'web' || $target eq 'webgrade') {
 4157: 	$currentstring = $token->[2];    
 4158:     } 
 4159:     return $currentstring;
 4160: }
 4161: 
 4162: #-- <noframes> tag (end tag required)
 4163: sub start_noframes {
 4164:     my ($target,$token) = @_;
 4165:     my $currentstring = '';
 4166:     if ($target eq 'web' || $target eq 'webgrade') {
 4167: 	$currentstring = $token->[4];     
 4168:     } 
 4169:     return $currentstring;
 4170: }
 4171: 
 4172: sub end_noframes {
 4173:     my ($target,$token) = @_;
 4174:     my $currentstring = '';
 4175:     if ($target eq 'web' || $target eq 'webgrade') {
 4176: 	$currentstring = $token->[2];    
 4177:     } 
 4178:     return $currentstring;
 4179: }
 4180: 
 4181: #-- <nolayer> tag (end tag required)
 4182: sub start_nolayer {
 4183:     my ($target,$token) = @_;
 4184:     my $currentstring = '';
 4185:     if ($target eq 'web' || $target eq 'webgrade') {
 4186: 	$currentstring = $token->[4];     
 4187:     } 
 4188:     return $currentstring;
 4189: }
 4190: 
 4191: sub end_nolayer {
 4192:     my ($target,$token) = @_;
 4193:     my $currentstring = '';
 4194:     if ($target eq 'web' || $target eq 'webgrade') {
 4195: 	$currentstring = $token->[2];    
 4196:     } 
 4197:     return $currentstring;
 4198: }
 4199: 
 4200: #-- <noscript> tag (end tag required)
 4201: sub start_noscript {
 4202:     my ($target,$token) = @_;
 4203:     my $currentstring = '';
 4204:     if ($target eq 'web' || $target eq 'webgrade') {
 4205: 	$currentstring = $token->[4];     
 4206:     } 
 4207:     return $currentstring;
 4208: }
 4209: 
 4210: sub end_noscript {
 4211:     my ($target,$token) = @_;
 4212:     my $currentstring = '';
 4213:     if ($target eq 'web' || $target eq 'webgrade') {
 4214: 	$currentstring = $token->[2];    
 4215:     } 
 4216:     return $currentstring;
 4217: }
 4218: 
 4219: #-- <object> tag (end tag required)
 4220: sub start_object {
 4221:     my ($target,$token) = @_;
 4222:     my $currentstring = '';
 4223:     if ($target eq 'web' || $target eq 'webgrade') {
 4224: 	$currentstring = $token->[4];     
 4225:     } 
 4226:     return $currentstring;
 4227: }
 4228: 
 4229: sub end_object {
 4230:     my ($target,$token) = @_;
 4231:     my $currentstring = '';
 4232:     if ($target eq 'web' || $target eq 'webgrade') {
 4233: 	$currentstring = $token->[2];    
 4234:     } 
 4235:     return $currentstring;
 4236: }
 4237: 
 4238: #-- <optgroup> tag (end tag required)
 4239: sub start_optgroup {
 4240:     my ($target,$token) = @_;
 4241:     my $currentstring = '';
 4242:     if ($target eq 'web' || $target eq 'webgrade') {
 4243: 	$currentstring = $token->[4];     
 4244:     } 
 4245:     return $currentstring;
 4246: }
 4247: 
 4248: sub end_optgroup {
 4249:     my ($target,$token) = @_;
 4250:     my $currentstring = '';
 4251:     if ($target eq 'web' || $target eq 'webgrade') {
 4252: 	$currentstring = $token->[2];    
 4253:     } 
 4254:     return $currentstring;
 4255: }
 4256: 
 4257: #-- <samp> tag (end tag required)
 4258: sub start_samp {
 4259:     my ($target,$token) = @_;
 4260:     my $currentstring = '';
 4261:     if ($target eq 'web' || $target eq 'webgrade') {
 4262: 	$currentstring = $token->[4];     
 4263:     } elsif ($target eq 'tex') {
 4264: 	$currentstring='\texttt{';
 4265:     }
 4266:     return $currentstring;
 4267: }
 4268: 
 4269: sub end_samp {
 4270:     my ($target,$token) = @_;
 4271:     my $currentstring = '';
 4272:     if ($target eq 'web' || $target eq 'webgrade') {
 4273: 	$currentstring = $token->[2];    
 4274:     } elsif ($target eq 'tex') {
 4275: 	$currentstring='}';
 4276:     }
 4277:     return $currentstring;
 4278: }
 4279: 
 4280: #-- <server> tag
 4281: sub start_server {
 4282:     my ($target,$token) = @_;
 4283:     my $currentstring = '';
 4284:     if ($target eq 'web' || $target eq 'webgrade') {
 4285: 	$currentstring = $token->[4];     
 4286:     } 
 4287:     return $currentstring;
 4288: }
 4289: 
 4290: sub end_server {
 4291:     my ($target,$token) = @_;
 4292:     my $currentstring = '';
 4293:     if ($target eq 'web' || $target eq 'webgrade') {
 4294: 	$currentstring = $token->[2];    
 4295:     } 
 4296:     return $currentstring;
 4297: }
 4298: 
 4299: #-- <spacer> tag (end tag forbidden)
 4300: sub start_spacer {
 4301:     my ($target,$token) = @_;
 4302:     my $currentstring = &end_p();	# Close off any open <p> tag.
 4303:     if ($target eq 'web' || $target eq 'webgrade') {
 4304: 	$currentstring .= $token->[4];     
 4305:     } 
 4306:     return $currentstring;
 4307: }
 4308: 
 4309: sub end_spacer {
 4310:     my ($target,$token) = @_;
 4311:     my $currentstring = '';
 4312:     if ($target eq 'web' || $target eq 'webgrade') {
 4313: 	$currentstring = $token->[2];    
 4314:     } 
 4315:     return $currentstring;
 4316: }
 4317: 
 4318: #-- <span> tag (end tag required)
 4319: sub start_span {
 4320:     my ($target,$token) = @_;
 4321:     my $currentstring = '';
 4322:     if ($target eq 'web' || $target eq 'webgrade') {
 4323: 	$currentstring = $token->[4];     
 4324:     } 
 4325:     return $currentstring;
 4326: }
 4327: 
 4328: sub end_span {
 4329:     my ($target,$token) = @_;
 4330:     my $currentstring = '';
 4331:     if ($target eq 'web' || $target eq 'webgrade') {
 4332: 	$currentstring = $token->[2];    
 4333:     } 
 4334:     return $currentstring;
 4335: }
 4336: 
 4337: #-- <tbody> tag (end tag optional)
 4338: sub start_tbody {
 4339:     my ($target,$token) = @_;
 4340:     my $currentstring = '';
 4341:     if ($target eq 'web' || $target eq 'webgrade') {
 4342: 	$currentstring = $token->[4];     
 4343:     } 
 4344:     return $currentstring;
 4345: }
 4346: 
 4347: sub end_tbody {
 4348:     my ($target,$token) = @_;
 4349:     my $currentstring = '';
 4350:     if ($target eq 'web' || $target eq 'webgrade') {
 4351: 	$currentstring = $token->[2];    
 4352:     } 
 4353:     return $currentstring;
 4354: }
 4355: 
 4356: #-- <tfoot> tag (end tag optional)
 4357: sub start_tfoot {
 4358:     my ($target,$token) = @_;
 4359:     my $currentstring = '';
 4360:     if ($target eq 'web' || $target eq 'webgrade') {
 4361: 	$currentstring = $token->[4];     
 4362:     } 
 4363:     return $currentstring;
 4364: }
 4365: 
 4366: sub end_tfoot {
 4367:     my ($target,$token) = @_;
 4368:     my $currentstring = '';
 4369:     if ($target eq 'web' || $target eq 'webgrade') {
 4370: 	$currentstring = $token->[2];    
 4371:     } 
 4372:     return $currentstring;
 4373: }
 4374: 
 4375: #-- <thead> tag (end tag optional)
 4376: sub start_thead {
 4377:     my ($target,$token) = @_;
 4378:     my $currentstring = '';
 4379:     if ($target eq 'web' || $target eq 'webgrade') {
 4380: 	$currentstring = $token->[4];     
 4381:     } 
 4382:     return $currentstring;
 4383: }
 4384: 
 4385: sub end_thead {
 4386:     my ($target,$token) = @_;
 4387:     my $currentstring = '';
 4388:     if ($target eq 'web' || $target eq 'webgrade') {
 4389: 	$currentstring = $token->[2];    
 4390:     } 
 4391:     return $currentstring;
 4392: }
 4393: 
 4394: #-- <var> tag
 4395: sub start_var {
 4396:     my ($target,$token) = @_;
 4397:     my $currentstring = '';
 4398:     if ($target eq 'web' || $target eq 'webgrade') {
 4399: 	$currentstring = $token->[4];     
 4400:     } elsif ($target eq 'tex') {
 4401: 	$currentstring = '\textit{'; 
 4402:     }
 4403:     return $currentstring;
 4404: }
 4405: 
 4406: sub end_var {
 4407:     my ($target,$token) = @_;
 4408:     my $currentstring = '';
 4409:     if ($target eq 'web' || $target eq 'webgrade') {
 4410: 	$currentstring = $token->[2];
 4411:     } elsif ($target eq 'tex') {
 4412: 	$currentstring = '}'; 
 4413:     } 
 4414:     return $currentstring;
 4415: }
 4416: 
 4417: #-- <wbr> tag (end tag forbidden)
 4418: sub start_wbr {
 4419:     my ($target,$token) = @_;
 4420:     my $currentstring = '';
 4421:     if ($target eq 'web' || $target eq 'webgrade') {
 4422: 	$currentstring = $token->[4];     
 4423:     } 
 4424:     return $currentstring;
 4425: }
 4426: 
 4427: sub end_wbr {
 4428:     my ($target,$token) = @_;
 4429:     my $currentstring = '';
 4430:     if ($target eq 'web' || $target eq 'webgrade') {
 4431: 	$currentstring = $token->[2];    
 4432:     } 
 4433:     return $currentstring;
 4434: }
 4435: 
 4436: #-- <hideweboutput> tag
 4437: sub start_hideweboutput {
 4438:     my ($target,$token) = @_;
 4439:     if ($target eq 'web' || $target eq 'webgrade') {
 4440: 	&Apache::lonxml::startredirection();     
 4441:     } 
 4442:     return '';
 4443: }
 4444: 
 4445: sub end_hideweboutput {
 4446:     my ($target,$token) = @_;
 4447:     my $currentstring = '';
 4448:     if ($target eq 'web' || $target eq 'webgrade') {
 4449: 	$currentstring = &Apache::lonxml::endredirection();    
 4450:     } 
 4451:     return '';
 4452: }
 4453: 
 4454: 
 4455: sub image_replication {
 4456:     my $src = shift;
 4457:     if (not -e $src) { &Apache::lonnet::repcopy($src); }
 4458:     #replicates eps or ps 
 4459:     my $epssrc = my $pssrc = $src;
 4460:     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
 4461:     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
 4462:     if (not -e $epssrc && not -e $pssrc) {
 4463: 	my $result=&Apache::lonnet::repcopy($epssrc);
 4464: 	if ($result ne 'ok') { &Apache::lonnet::repcopy($pssrc); }
 4465:     }
 4466:     return '';
 4467: }
 4468: 
 4469: 
 4470: 
 4471: sub resize_image {
 4472:     my ($height_param, $width_param, $scaling,
 4473: 	$parstack, $safeeval, $depth, $cis) = @_;
 4474: 
 4475:     # First apply the scaling...
 4476: 
 4477:     $height_param = $height_param * $scaling;
 4478:     $width_param  = $width_param  * $scaling;
 4479: 
 4480:     #do we have any specified LaTeX size of the picture?
 4481:     my $toget='TeXwidth'; 
 4482:     if ($cis) { 
 4483: 	$toget=lc($toget); 
 4484:     }
 4485:     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
 4486: 					      $safeeval,$depth,$cis);
 4487:     $toget='TeXheight'; if ($cis) { $toget=lc($toget); }
 4488:     my $TeXheight = &Apache::lonxml::get_param($toget,$parstack,
 4489: 					       $safeeval,$depth,$cis);
 4490:     #do we have any specified web size of the picture?
 4491:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
 4492: 					   $depth,1);
 4493:     if ($TeXwidth) { 
 4494: 	my $old_width_param=$width_param;
 4495: 	if ($TeXwidth=~/(\d+)\s*\%/) {
 4496: 	    $width_param = $1*$env{'form.textwidth'}/100;
 4497: 	} else { 
 4498: 	    $width_param = $TeXwidth;
 4499: 	}
 4500: 	if ($TeXheight) {
 4501: 	    $height_param = $TeXheight;
 4502: 	} elsif ($old_width_param) {
 4503: 	    $height_param=$TeXwidth/$old_width_param*$height_param;
 4504: 	}
 4505:     } elsif ($TeXheight) {
 4506: 	$height_param = $TeXheight;
 4507: 	if ($height_param) {
 4508: 	    $width_param  = $TeXheight/$height_param*$width_param;
 4509: 	}
 4510:     } elsif ($width) {
 4511: 	my $old_width_param=$width_param;
 4512: 	$width_param = $width*$scaling;
 4513: 	if ($old_width_param) {
 4514: 	    $height_param=$width_param/$old_width_param*$height_param;
 4515: 	}
 4516:     }
 4517:     if ($width_param > $env{'form.textwidth'}) {
 4518:         my $old_width_param=$width_param;
 4519: 	$width_param =0.95*$env{'form.textwidth'};
 4520: 	if ($old_width_param) {
 4521: 	    $height_param=$width_param/$old_width_param*$height_param;
 4522: 	}
 4523:     }
 4524: 
 4525:     return ($height_param, $width_param);
 4526: }
 4527: 
 4528: sub image_size {
 4529:     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
 4530: 
 4531:     #size of image from gif/jpg/jpeg/png 
 4532:     my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4533:     if (-e $ressrc) {
 4534: 	$src = $ressrc;
 4535:     }
 4536:     my $image = Image::Magick->new;
 4537:     my $current_figure = $image->Read($src);
 4538:     my $width_param = $image->Get('width');
 4539:     my $height_param = $image->Get('height');
 4540:     &Apache::lonxml::debug("Image magick says: $src :  Height = $height_param width = $width_param");
 4541:     undef($image);
 4542: 
 4543:     ($height_param, $width_param) = &resize_image($height_param, $width_param,
 4544: 						  $scaling, $parstack, $safeeval, 
 4545: 						  $depth, $cis);
 4546: 
 4547:     return ($height_param, $width_param);
 4548: }
 4549: 
 4550: sub image_width {
 4551:     my ($height, $width) = &image_size(@_);
 4552:     return $width;
 4553: }
 4554: #  Not yet 100% sure this is correct in all circumstances..
 4555: #  due to my uncertainty about mods to image_size.
 4556: #
 4557: sub image_height {
 4558:     my ($height, $width) = &image_size(@_);
 4559:     return $height;
 4560: }
 4561: 
 4562: sub get_eps_image {
 4563:     my ($src)=@_;
 4564:     my $orig_src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1], $src);
 4565: 
 4566:     # In order to prevent the substitution of the alt text, we need to
 4567:     # be sure the orig_src file is on system now so:
 4568: 
 4569:     if (! -e $orig_src) {
 4570: 	&Apache::lonnet::repcopy($orig_src); # Failure is not completely fatal.
 4571:     }
 4572:     &Apache::lonxml::debug("get_eps_image: Original image: $orig_src");
 4573:     my ($spath, $sname, $sext) = &fileparse($src, qr/\.(bmp|gif|png|jpg|jpeg)/i);
 4574:     $src=~s/\.(bmp|gif|png|jpg|jpeg)$/\.eps/i;
 4575:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4576:     &Apache::lonxml::debug("Filelocation gives: $src");
 4577:     if (! -e $src) {
 4578: 	&Apache::lonxml::debug("$src does not exist");
 4579: 	if (&Apache::lonnet::repcopy($src) ne 'ok' ) {
 4580: 	    &Apache::lonxml::debug("Repcopy of $src failed (1)");
 4581: 	    #if replication failed try to find ps file
 4582: 	    $src=~s/\.eps$/\.ps/;
 4583: 	    &Apache::lonxml::debug("Now looking for $src");
 4584: 	    #if no ps file try to replicate it.
 4585: 	    my $didrepcopy = &Apache::lonnet::repcopy($src);
 4586: 	    &Apache::lonxml::debug("repcopy of $src ... $didrepcopy");
 4587: 	    if ( (not -e $src) ||
 4588: 		($didrepcopy ne 'ok')) {
 4589: 		&Apache::lonxml::debug("Failed to find or replicate $src");
 4590: 
 4591: 		#if replication failed try to produce eps file dynamically
 4592: 		$src=~s/\.ps$/\.eps/;
 4593: 		my $temp_file;
 4594: 		open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
 4595: 		my $newsrc=$orig_src;
 4596: 		$newsrc =~ s|(.*)/res/|/home/httpd/html/res/|;
 4597: 		&Apache::lonxml::debug("queueing $newsrc for dynamic eps production.");
 4598: 		print FILE ("$newsrc\n");
 4599: 		close(FILE);
 4600: 		$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 4601: 		$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 4602: 		if ($sext ne "") {	 # Put the ext. back in to uniquify.
 4603: 		    $src =~ s/\.eps$/$sext.eps/;
 4604: 		}
 4605: 
 4606: 	    }
 4607: 
 4608: 	}
 4609:     } else {
 4610: 	# If the postscript file has spaces in its name,
 4611: 	# LaTeX will gratuitiously vomit.  Therefore
 4612: 	# queue such files for copy with " " replaced by "_".
 4613: 	# printout.pm will know them by their .ps  or .eps extensions.
 4614: 	my $newsrc = $orig_src;
 4615: 	$newsrc    =~  s|(.*)/res/|/home/httpd/html/res/|;
 4616: 	open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
 4617: 	print FILE "$src\n";
 4618: 	close FILE;
 4619: 	$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 4620: 	$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 4621:     }
 4622:     my ($path,$file)=($src=~m|(.*)/([^/]*)$|);
 4623:     $path =~ s/ /\_/g;
 4624:     $file =~ s/ /\_/g;
 4625:     &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />");
 4626:     return ($path.'/',$file);
 4627: }
 4628: 
 4629: sub eps_generation {
 4630:     my ($src,$file,$width_param) = @_;	     
 4631:     my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
 4632:     my $temp_file = Apache::File->new('>>'.$filename); 
 4633:     print $temp_file "$src\n";
 4634:     my $newsrc = $src;
 4635:     $newsrc =~ s/(\.bmp|\.gif|\.jpg|\.jpeg)$/\.eps/i;
 4636:     $newsrc=~s{/home/httpd/html/res}{};
 4637:     $newsrc=~s{/home/($LONCAPA::username_re)/public_html/}{/$1/};
 4638:     $newsrc=~s{/\./}{/};
 4639:     $newsrc=~s{/([^/]+)\.(ps|eps)}{/};
 4640:     if ($newsrc=~m{/home/httpd/lonUsers/}) {
 4641: 	$newsrc=~s{/home/httpd/lonUsers}{};
 4642: 	$newsrc=~s{/($LONCAPA::domain_re)/./././}{/$1/};
 4643:     }
 4644:     if ($newsrc=~m{/userfiles/}) {
 4645: 	return ' \graphicspath{{'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4646:     } else {
 4647: 	return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4648:     }
 4649: }
 4650: 
 4651: sub file_path {     
 4652:     my $src=shift;
 4653:     my ($file,$path); 
 4654:     if ($src =~ m!(.*)/([^/]*)$!) {
 4655: 	$file = $2; 
 4656: 	$path = $1.'/'; 
 4657:     } 
 4658:     return $file,$path;
 4659: }
 4660: 
 4661: 
 4662: sub recalc {
 4663:     my $argument = shift;
 4664:     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
 4665:     $argument=~/\s*(\d+\.?\d*)\s*(mm|cm|in|pc|pt)/;
 4666:     my $value=$1;
 4667:     my $units=$2;
 4668:     if ($units eq 'cm') {
 4669: 	$value*=10;
 4670:     } elsif ($units eq 'in') {
 4671: 	$value*=25.4;
 4672:     } elsif ($units eq 'pc') {
 4673: 	$value*=(25.4*12/72.27);
 4674:     } elsif ($units eq 'pt') {
 4675: 	$value*=(25.4/72.27);
 4676:     }
 4677:     return $value.' mm';
 4678: }
 4679: 
 4680: sub LATEX_length {
 4681:     my $garbage=shift;
 4682:     $garbage=~s/^\s+$//;
 4683:     $garbage=~s/^\s+(\S.*)/$1/;#space before 
 4684:     $garbage=~s/(.*\S)\s+$/$1/;#space after 
 4685:     $garbage=~s/(\s)+/$1/;#only one space
 4686:     $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
 4687:     $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
 4688:     $garbage=~s/([^\\])\$/$1/g;#$
 4689:     $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
 4690:    $garbage=~s/(\\alpha|\\beta|\\gamma|\\delta|\\epsilon|\\verepsilon|\\zeta|\\eta|\\theta|\\vartheta|\\iota|\\kappa|\\lambda|\\mu|\\nu|\\xi|\\pi|\\varpi|\\rho|\\varrho|\\sigma|\\varsigma|\\tau|\\upsilon|\\phi|\\varphi|\\chi|\\psi|\\omega|\\Gamma|\\Delta|\\Theta|\\Lambda|\\Xi|\\Pi|\\Sigma|\\Upsilon|\\Phi|\\Psi|\\Omega)/1/g;
 4691:     $garbage=~s/(\\pm|\\mp|\\times|\\div|\\cdot|\\ast|\\star|\\dagger|\\ddagger|\\amalg|\\cap|\\cup|\\uplus|\\sqcap|\\sqcup|\\vee|\\wedge|\\oplus|\\ominus|\\otimes|\\circ|\\bullet|\\diamond|\\lhd|\\rhd|\\unlhd|\\unrhd|\\oslash|\\odot|\\bigcirc|\\Box|\\Diamond|\\bigtriangleup|\\bigtriangledown|\\triangleleft|\\triangleright|\\setminus|\\wr)/1/g;
 4692:     $garbage=~s/(\\le|\\ll|\\leq|\\ge|\\geq|\\gg|\\neq|\\doreq|\\sim|\\simeq|\\subset|\\subseteq|\\sqsubset|\\sqsubseteq|\\in|\\vdash|\\models|\\supset|\\supseteq|\\sqsupset|\\sqsupseteq|\\ni|\\dash|\\perp|\\approx|\\cong|\\equiv|\\propto|\\prec|\\preceq|\\parallel|\\asymp|\\smile|\\frown|\\bowtie|\\succ|\\succeq|\\mid)/1/g;
 4693:     $garbage=~s/(\\not<|\\\\not\\le|\\not\\prec|\\not\\preceq|\\not\\subset|\\not\\subseteq|\\not\\sqsubseteq|\\not\\in|\\not>|\\not\\ge|\\not\\succ|\\notsucceq|\\not\\supset|\\notsupseteq|\\not\\sqsupseteq|\\notin|\\not=|\\not\\equiv|\\not\\sim|\\not\\simeq|\\not\\approx|\\not\\cong|\\not\\asymp)/1/g;
 4694:     $garbage=~s/(\\leftarrow|\\gets|\\Leftarrow|\\rightarrow|\\to|\\Rightarrow|\\leftrightarrow|\\Leftrightarrow|\\mapsto|\\hookleftarrow|\\leftharpoonup|\\leftkarpoondown|\\rightleftharpoons|\\longleftarrow|\\Longleftarrow|\\longrightarrow|\\Longrightarrow|\\longleftrightarrow|\\Longleftrightarrow|\\longmapsto|\\hookrightarrow|\\rightharpoonup|\\rightharpoondown|\\uparrow|\\Uparrow|\\downarrow|\\Downarrow|\\updownarrow|\\Updownarrow|\\nearrow|\\searrow|\\swarrow|\\nwarrow)/11/g;
 4695:     $garbage=~s/(\\aleph|\\hbar|\\imath|\\jmath|\\ell|\\wp|\\Re|\\Im|\\mho|\\prime|\\emptyset|\\nabla|\\surd|\\partial|\\top|\\bot|\\vdash|\\dashv|\\forall|\\exists|\\neg|\\flat|\\natural|\\sharp|\\\||\\angle|\\backslash|\\Box|\\Diamond|\\triangle|\\clubsuit|\\diamondsuit|\\heartsuit|\\spadesuit|\\Join|\\infty)/11/g;
 4696:     $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
 4697:     #remove some other LaTeX command
 4698:     $garbage=~s|\\(\w+)\\|\\|g;	 
 4699:     $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
 4700:     $garbage=~s|\+|11|g;
 4701:     my  $value=length($garbage);
 4702:     return $value;
 4703: }
 4704: 
 4705: 
 4706: sub align_latex_image {
 4707:     my ($align, $latex_rendering, $image, $width, $height) = @_;
 4708:     my $currentstring;        # The 1/2 wrapped image.
 4709:     my $closure;              # The closure of the wrappage.
 4710: 
 4711:     # if it's none just return it back
 4712:     if ($latex_rendering eq 'none') {
 4713: 	return ($image,'');
 4714:     }
 4715: 
 4716:     #    If there's an alignment specification we need to honor it here.
 4717:     #    For the horizontal alignments, we will also honor the
 4718:     #    value of the latex specfication.  The default is parbox,
 4719:     #    and that's used for illegal values too.  
 4720:     #    
 4721:     #    Even though we set a default alignment value, the user
 4722:     #    could have given us an illegal value.  In that case we
 4723:     #    just use the default alignment of bottom..
 4724:     $currentstring = "\n% figurewrapping \n";
 4725:     if      ($align eq "top")    {
 4726: 	$currentstring .= '\raisebox{-'.$height.'mm}{'.$image;
 4727: 	$closure = '}';
 4728:     } elsif (($align eq "center") || ($align eq "middle")) { # Being kind
 4729: 	my $offset = $height/2;
 4730: 	$currentstring .= '\raisebox{-'.$offset.'mm}{'.$image;
 4731: 	$closure       = '}';
 4732:     } elsif ($align eq "left")   { 
 4733: 	if ($latex_rendering eq "parpic") { 
 4734: 	    $currentstring .= '\parpic[l]{'.$image;
 4735: 	    $closure       = '}';
 4736: 	} elsif ($latex_rendering eq "parbox") {
 4737: 	    $currentstring .= '\begin{minipage}[l]{'.$width.'mm}'
 4738: 		.$image;
 4739: 	    $closure = '\end{minipage}';
 4740: 	} elsif ($latex_rendering eq "wrapfigure"
 4741: 		 || $latex_rendering ne 'none') {  # wrapfig render
 4742: 	    $currentstring .= 
 4743: 		'\begin{wrapfigure}{l}{'.$width.'mm}'
 4744: 		.'\scalebox{1.0}{'.$image;
 4745: 	    $closure = '}\end{wrapfigure}';
 4746: 	}
 4747:     } elsif ($align eq "right")  {   
 4748: 	if ($latex_rendering eq "parpic") {
 4749: 	    $currentstring .= '\parpic[r]{'.$image;
 4750: 	    $closure = '}';
 4751: 	} elsif ($latex_rendering eq "parbox") {
 4752: 	    $currentstring .=  '\begin{minipage}[r]{'.$width.'mm}'
 4753: 		.$image;
 4754: 	    $closure = '\end{minipage}';
 4755: 	} elsif ($latex_rendering eq "wrapfigure"
 4756: 		 || $latex_rendering ne 'none') {  # wrapfig render
 4757: 	    $currentstring .= 
 4758: 		'\begin{wrapfigure}{r}{'.$width.'mm}'
 4759: 		.'\scalebox{1.0}{'.$image;
 4760: 	    $closure = '}\end{wrapfigure}';
 4761: 	}
 4762:     } else {		# Bottom is also default.
 4763: 	# $currentstring = '\raisebox{'.$height.'mm}{'.$image.'}';
 4764: 	$currentstring .= "{$image";
 4765: 	$closure       = '}';
 4766:     }
 4767:     $currentstring .= "\n% end wrappage\n";
 4768:     $closure        = "\n% Begin closure\n".$closure."\n% End closure\n";
 4769:     return ($currentstring, $closure);
 4770: }
 4771: 
 4772: 
 4773: sub is_inside_of {
 4774:     my ($tagstack, $tag) = @_;
 4775:     my @stack = @$tagstack;
 4776:     for (my $i = ($#stack - 1); $i >= 0; $i--) {
 4777: 	if ($stack[$i] eq $tag) {
 4778: 	    return 1;
 4779: 	}
 4780:     }
 4781:     return 0;
 4782: }
 4783: 
 4784: 
 4785: #
 4786: #   This sub provides the typical LaTeX prefix matter for tex output:
 4787: #
 4788: sub latex_header
 4789: {
 4790:     my ($mode) = @_;
 4791:     my $currentstring = '';
 4792: 
 4793:     $currentstring .= 
 4794: 	"\n% &Apache::lonxml::londefdef \n" .
 4795: 	'\documentclass[letterpaper,twoside]{article}\raggedbottom';
 4796:     if (($env{'form.latex_type'}=~'batchmode') ||
 4797: 	(!$env{'request.role.adv'}) || 
 4798: 	($mode eq 'batchmode')) {$currentstring .='\batchmode';} 
 4799:     $currentstring .= '\newcommand{\keephidden}[1]{}'.
 4800: 	'\renewcommand{\deg}{$^{\circ}$}'.
 4801: 	'\usepackage{multirow}'.
 4802: 	'\usepackage{longtable}'.
 4803: 	'\usepackage{textcomp}'.
 4804: 	'\usepackage{makeidx}'.
 4805: 	'\usepackage[dvips]{graphicx}'.
 4806: 	'\usepackage{wrapfig}'.
 4807: 	'\usepackage{picins}'.
 4808: 	'\usepackage[T1]{fontenc}'."\n".
 4809: 	'\usepackage{lmodern}'."\n".
 4810: 	'\usepackage[postscript]{ucs}'."\n".
 4811: 	'\usepackage[utf8x]{inputenc}'."\n".
 4812: 	'\usepackage{pifont}' ."\n".
 4813: 	'\usepackage{latexsym}'."\n".
 4814: 	'\usepackage{epsfig}'.
 4815: 	"\\usepackage{xtab}\n".
 4816: 	"\\usepackage{tabularx}\n".
 4817: 	"\\usepackage{booktabs}\n".
 4818: 	"\\usepackage{array}\n".
 4819: 	"\\usepackage{colortbl}\n".
 4820: 	"\\usepackage{xcolor}\n".
 4821: 	'\usepackage{calc}'.
 4822: 	'\usepackage{amsmath}'.
 4823: 	'\usepackage{amssymb}'.
 4824: 	'\usepackage{amsfonts}'.
 4825: 	'\usepackage{amsthm}'.
 4826: 	'\usepackage{amscd}'
 4827:         .'\usepackage{picins}\usepackage{calc}'."\n". # From lonprintout.pm
 4828: 	'\usepackage[T1]{fontenc}'."\n".
 4829: 	'\usepackage{lmodern}'."\n".
 4830: 	'\usepackage[postscript]{ucs}'."\n".
 4831: 	'\usepackage[utf8x]{inputenc}'."\n".
 4832: 	'\usepackage{pifont}'  . "\n";
 4833: 	
 4834:     if($env{'form.pdfFormFields'} eq 'yes') {
 4835: 	$currentstring .= '\usepackage{hyperref}'.
 4836: 	    '\usepackage{eforms}'.
 4837: 	    '\usepackage{tabularx}';
 4838:     } 
 4839:     
 4840:         $currentstring .= '\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}'.
 4841:                           '\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large\textbf{Index}} \newline \setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}';
 4842:     $currentstring .= '\begin{document}';
 4843:     
 4844:     return $currentstring;
 4845: 
 4846: }
 4847: 
 4848: =pod
 4849: 
 4850: =head1 NAME
 4851: 
 4852: Apache::londefdef.pm
 4853: 
 4854: =head1 SYNOPSIS
 4855: 
 4856: Tags Default Definition Module
 4857: 
 4858: This is part of the LearningOnline Network with CAPA project
 4859: described at http://www.lon-capa.org.
 4860: 
 4861: 
 4862: =head1 NOTABLE SUBROUTINES
 4863: 
 4864: =over
 4865: 
 4866: =item start_hideweboutput()
 4867: 
 4868: =item end_hideweboutput()
 4869: 
 4870: =item image_replication()
 4871: 
 4872: =item resize_image()
 4873: 
 4874: 	Get correct sizing parameter for an image given
 4875: 	it's initial ht. and wid.  This allows sizing of
 4876: 	images that are generated on-the-fly (e.g. gnuplot)
 4877: 	as well as serving as a utility for image_size.
 4878:  
 4879: 	Parameter:
 4880:         height_param
 4881:         width_param    - Initial picture dimensions.
 4882:         scaling        - A scale factor.
 4883:         parstack,      - the current stack of tag attributes 
 4884:                          from the xml parser
 4885:         safeeval,      - pointer to the safespace
 4886:         depth,         - from what level in the stack to look for attributes
 4887:                          (assumes -1 if unspecified)
 4888:         cis            - look for attrubutes case insensitively
 4889:                          (assumes false)
 4890: 
 4891: 	Returns:
 4892: 		height, width   - new dimensions.
 4893: 
 4894: =item image_size()
 4895: 
 4896: =item image_width()
 4897: 
 4898: =item image_height()
 4899: 
 4900: =item get_eps_image()
 4901: 
 4902: =item eps_generation()
 4903: 
 4904: =item file_path()
 4905: 
 4906: =item recalc()
 4907: 
 4908: 	Converts a measurement in to mm from any of 
 4909: 	the other valid LaTeX units of measure.
 4910: 	If the units of measure are missing from the 
 4911: 	parameter, it is assumed to be in and returned
 4912: 	with mm units of measure
 4913: 
 4914: =item LATEX_length()
 4915: 
 4916: =item align_latex_image()
 4917: 
 4918:   	Wrap image 'stuff' inside of the LaTeX required to implement 
 4919:    	alignment:
 4920:      	align_tex_image(align, latex_rendering, image)
 4921:    	Where:
 4922:      	align   - The HTML alignment specification.
 4923:      	latex_rendering - rendering hint for latex.
 4924:      	image   - The LaTeX needed to insert the image itsef.
 4925:      	width,height - dimensions of the image.
 4926:  	Returns:
 4927:     	The 1/2 wrapped image and the stuff required to close the
 4928:     	wrappage.  This allows e.g. randomlabel to insert more stuff
 4929:     	into the closure.
 4930: 
 4931: 
 4932: =item is_inside_of($tagstack, $tag)
 4933:    	This sub returns true if the current state of Xml processing is inside of the tag.   
 4934: 	Parameters:
 4935:     	tagstack   - The tagstack from the parser.
 4936:     	tag        - The tag (without the <>'s.).
 4937: 	Sample usage:
 4938:     	if (is_inside_of($tagstack "table")) {
 4939:      	   I'm in a table....
 4940:      	}
 4941: 
 4942: 
 4943: 
 4944: =back
 4945: 
 4946: =cut
 4947: 
 4948: 
 4949: 1;
 4950: __END__

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