File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.406: download - view: text, annotated - select for diffs
Tue May 26 14:06:23 2009 UTC (14 years, 11 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Changes related to modification of LON-CAPA screen header.
start_page call:
- Added breadcrumbs
- Added head_subbox and included CSTR standard header

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

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