File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.391: download - view: text, annotated - select for diffs
Fri Sep 5 11:23:06 2008 UTC (15 years, 8 months ago) by onken
Branches: MAIN
CVS tags: HEAD
Adds needed LaTeX-packages to render PDF-formsfields into tex-code, when
user choose to print with PDF-Formfields in print-options.

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

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