File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.400: download - view: text, annotated - select for diffs
Sat Apr 11 01:11:02 2009 UTC (15 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Side effect of change in lonnet.pm 1.886 -- to address bug 5290 (portfolio).
   - HTML parsing code is also used when uploading to DOCS.
   - Need to add httpref entry for any files linked to in uploaded file (not these types of file do not appear in the course map).

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

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