File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.373: download - view: text, annotated - select for diffs
Fri Jul 13 18:35:20 2007 UTC (16 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#5307, can allow deny users with _ in their username

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

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