File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.394: download - view: text, annotated - select for diffs
Wed Oct 15 09:25:42 2008 UTC (15 years, 7 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Localization:
Added missing &mt() call to edit button text

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

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