File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.438: download - view: text, annotated - select for diffs
Thu Mar 1 21:13:36 2012 UTC (12 years, 3 months ago) by droeschl
Branches: MAIN
CVS tags: HEAD
Bug 6563. Printing: Stylesheets in PDF
This attempts to fix the issue by extending the parser to handle <style> tags.
The new behaviour will not output anything within those tags if the target
is set to tex. Other targets are left unchanged.

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

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