File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.398: download - view: text, annotated - select for diffs
Tue Jan 20 12:01:00 2009 UTC (15 years, 3 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Switch to new table code.  It seems to work for all my table problems.

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

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