File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.388: download - view: text, annotated - select for diffs
Mon May 5 10:41:29 2008 UTC (16 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
Add [postscript] option to ucs package...helps get a few more
utf-8 glyphs

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

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