File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.383: download - view: text, annotated - select for diffs
Mon Mar 10 08:54:13 2008 UTC (16 years, 2 months ago) by foxr
Branches: MAIN
CVS tags: version_2_6_X, version_2_6_3, HEAD
BZ 2885 - Added
\usepackage[utf8]{inputenc} to LaTeX prefix code to make LaTeX utf8
transparent (or so the LaTeX people claim).

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

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