File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.410: download - view: text, annotated - select for diffs
Tue Jun 2 13:10:29 2009 UTC (14 years, 11 months ago) by bisitz
Branches: MAIN
CVS tags: bz5969, bz2851, HEAD, BZ5971-printing-apage
Changes related to modification of LON-CAPA screen header.
Ensure that Construction Space breadcrumbs only appear when in Construction Space
ToDo: other cases

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

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