File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.425: download - view: text, annotated - select for diffs
Mon Nov 1 10:52:09 2010 UTC (13 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
BZ6378 - Add back support for TeXwidth in <td> tag.

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

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