File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.411: download - view: text, annotated - select for diffs
Thu Oct 1 15:31:24 2009 UTC (14 years, 7 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Fixed bug 6015:
Menu links (e.g. Publish) work again for HTML pages in CSTR.

CSTR_pageheader includes a lonmenu::constspaceform() call.
Don't call lonmenu::constspaceform() again when CSTR_pageheader is called.

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

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