File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.367: download - view: text, annotated - select for diffs
Tue Jun 19 19:40:07 2007 UTC (16 years, 11 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	bug 3796: Restrict display to graphic files only.

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

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