File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.374: download - view: text, annotated - select for diffs
Tue Jul 17 19:05:24 2007 UTC (16 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_4_99_0, HEAD
- BUG#5319 implement edit/modifed targets for <meta>

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

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