File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.405: download - view: text, annotated - select for diffs
Fri May 15 18:06:20 2009 UTC (15 years ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Removed accessiblity related code. These options aren't supported anymore.
(more to do)

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

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