File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.403.4.2: download - view: text, annotated - select for diffs
Fri Aug 20 21:20:24 2010 UTC (13 years, 8 months ago) by raeburn
Branches: version_2_9_X
Diff to branchpoint 1.403: preferred, unified
- Backport 1.418, 1.419, 1.420, 1.421.

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

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