File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.335: download - view: text, annotated - select for diffs
Sat Sep 2 18:49:37 2006 UTC (17 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#5004

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

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