File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.436: download - view: text, annotated - select for diffs
Mon Nov 14 00:20:42 2011 UTC (12 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
- &authorspace() routine now takes an argument: $uri
  so "Construction Space" breadcrumb points at appropriate author space
  for resource/directory being viewed or acted om.
  - current role could be different or ("no role" - i.e., cm), so is only
    used to determine authorspace when $uri is unavailable.

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

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