File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.430: download - view: text, annotated - select for diffs
Wed Apr 13 10:08:06 2011 UTC (13 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
BZ 6317 - NOTE This commit contains over-extensive debugging output and should not be used in production.
- This commit includes initial work to support the rule="groups" option in
<table> for printing.  Commit because some drastic re-factoring and some data
structure re-engineering was requuired to capture some of the group information.
Anticipate the next commit will support <thead><tbody><tfoot> properly
both with respect to the order in which they appear in the output and
with respect to rules="groups"...if this version does not already do that
correctly...at that time the debug spew will be removed as well.

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

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