File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.427: download - view: text, annotated - select for diffs
Sun Nov 28 02:46:23 2010 UTC (13 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add "use" for loncommon
- xhtml - missing form action
- Course vs. Community

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

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