File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.333: download - view: text, annotated - select for diffs
Wed Aug 16 20:14:24 2006 UTC (17 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: version_2_2_0, HEAD
- need a spance after \par

    1: # The LearningOnline Network with CAPA
    2: # Tags Default Definition Module 
    3: #
    4: # $Id: londefdef.pm,v 1.333 2006/08/16 20:14:24 albertel Exp $
    5: # 
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: ## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
   29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
   30: # binary executable programs or libraries distributed by the 
   31: # Michigan State University (the "Licensee"), but any binaries so 
   32: # distributed are hereby licensed only for use in the context
   33: # of a program or computational system for which the Licensee is the 
   34: # primary author or distributor, and which performs substantial 
   35: # additional tasks beyond the translation of (La)TeX into HTML.
   36: # The C source of the Code may not be distributed by the Licensee
   37: # to any other parties under any circumstances.
   38: #
   39: 
   40: package Apache::londefdef; 
   41: 
   42: use Apache::lonnet;
   43: use strict;
   44: use Apache::lonxml;
   45: use Apache::File();
   46: use Image::Magick;
   47: use Apache::lonmenu();
   48: use Apache::lonmeta();
   49: use Apache::Constants qw(:common);
   50: use File::Basename;
   51: # use 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:                 ($tempo[$i] eq 'td') || ($tempo[$i] eq 'th'))  {
 1215: 		$signal=1;
 1216: 		last;
 1217: 	    }
 1218: 	}
 1219: 	if ($signal) {
 1220: 	    $currentstring .= ' \vskip 0 mm ';
 1221: 	} elsif ($$tagstack[-2] ne 'sub' && $$tagstack[-2] ne 'sup') {
 1222: 	    $currentstring .= '\strut \\\\ \strut ';
 1223: 	}
 1224:     }
 1225:     return $currentstring;
 1226: }
 1227: 
 1228: sub end_br {
 1229:     my ($target,$token) = @_;
 1230:     my $currentstring = '';
 1231:     if ($target eq 'web' || $target eq 'webgrade') {
 1232: 	$currentstring .= $token->[2];
 1233:     }
 1234:     return $currentstring;
 1235: }
 1236: 
 1237: #-- <big> tag (end tag required)
 1238: sub start_big {
 1239:     my ($target,$token) = @_;
 1240:     my $currentstring = '';
 1241:     if ($target eq 'web' || $target eq 'webgrade') {
 1242: 	$currentstring .= $token->[4];
 1243:     } elsif ($target eq 'tex') {
 1244: 	$currentstring .= '{\large ';
 1245:     } 
 1246:     return $currentstring;
 1247: }
 1248: 
 1249: sub end_big {
 1250:     my ($target,$token) = @_;
 1251:     my $currentstring = '';
 1252:     if ($target eq 'web' || $target eq 'webgrade') {
 1253: 	$currentstring .= $token->[2];
 1254:     } elsif ($target eq 'tex') {
 1255: 	$currentstring .= '}';
 1256:     }
 1257:     return $currentstring;
 1258: }
 1259: 
 1260: #-- <small> tag (end tag required)
 1261: sub start_small {
 1262:     my ($target,$token) = @_;
 1263:     my $currentstring = '';
 1264:     if ($target eq 'web' || $target eq 'webgrade') {
 1265: 	$currentstring .= $token->[4];
 1266:     } elsif ($target eq 'tex') {
 1267: 	$currentstring .= '{\footnotesize ';
 1268:     }
 1269:     return $currentstring;
 1270: }
 1271: 
 1272: sub end_small {
 1273:     my ($target,$token) = @_;
 1274:     my $currentstring = '';
 1275:     if ($target eq 'web' || $target eq 'webgrade') {
 1276: 	$currentstring .= $token->[2];
 1277:     } elsif ($target eq 'tex') {
 1278: 	$currentstring .= '}';
 1279:     }
 1280:     return $currentstring;
 1281: }
 1282: 
 1283: #-- <basefont> tag (end tag forbidden)
 1284: sub start_basefont {
 1285:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1286:     my $currentstring = '';
 1287:     if ($target eq 'web' || $target eq 'webgrade') {
 1288: 	$currentstring = $token->[4];     
 1289:     } elsif ($target eq 'tex') {
 1290: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1291: 	if (defined $basesize) {
 1292: 	    $currentstring = '{\\'.$basesize.' ';
 1293: 	}
 1294:     }
 1295:     return $currentstring;
 1296: }
 1297: 
 1298: sub end_basefont {
 1299:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1300:     my $currentstring = '';
 1301:     if ($target eq 'web' || $target eq 'webgrade') {
 1302: 	$currentstring = $token->[4];     
 1303:     } elsif ($target eq 'tex') {
 1304: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1305: 	if (defined $basesize) {
 1306: 	    $currentstring = '}';
 1307: 	}
 1308:     }
 1309:     return $currentstring;
 1310: }
 1311: 
 1312: #-- <font> tag (end tag required)
 1313: sub start_font {
 1314:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1315:     my $currentstring = '';
 1316:     if ($target eq 'web' || $target eq 'webgrade') {
 1317: 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
 1318: 	if ($face!~/symbol/i) {
 1319: 	    if (($env{'browser.fontenhance'} eq 'on') || 
 1320: 		($env{'browser.blackwhite'} eq 'on')) { return ''; }
 1321: 	}
 1322: 	$currentstring = $token->[4];     
 1323:     }  elsif ($target eq 'tex') {
 1324: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1325: 	if (defined $fontsize) {
 1326: 	    $currentstring = '{\\'.$fontsize.' ';
 1327: 	}
 1328:     }
 1329:     return $currentstring;
 1330: }
 1331: 
 1332: sub end_font {
 1333:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1334:     my $currentstring = '';
 1335:     if ($target eq 'web' || $target eq 'webgrade') {
 1336: 	$currentstring = $token->[2];    
 1337:     }  elsif ($target eq 'tex') {
 1338: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1339: 	if (defined $fontsize) {
 1340: 	    $currentstring = '}';
 1341: 	}
 1342:     }
 1343:     return $currentstring;
 1344: }
 1345:  
 1346: #-- <strike> tag (end tag required)
 1347: sub start_strike {
 1348:     my ($target,$token) = @_;
 1349:     my $currentstring = '';
 1350:     if ($target eq 'web' || $target eq 'webgrade') {
 1351: 	$currentstring .= $token->[4];
 1352:     } elsif ($target eq 'tex') {
 1353: 	&Apache::lonxml::startredirection();
 1354:     } 
 1355:     return $currentstring;
 1356: }
 1357: 
 1358: sub end_strike {
 1359:     my ($target,$token) = @_;
 1360:     my $currentstring = '';
 1361:     if ($target eq 'web' || $target eq 'webgrade') {
 1362: 	$currentstring .= $token->[2];
 1363:     } elsif ($target eq 'tex') {
 1364: 	$currentstring=&Apache::lonxml::endredirection();
 1365: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g; 
 1366: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/; 
 1367: 	$currentstring=~s/(\S)\s*$/$1\}/;
 1368:     }
 1369:     return $currentstring;
 1370: }
 1371: 
 1372: #-- <s> tag (end tag required)
 1373: sub start_s {
 1374:     my ($target,$token) = @_;
 1375:     my $currentstring = '';
 1376:     if ($target eq 'web' || $target eq 'webgrade') {
 1377: 	$currentstring .= $token->[4];
 1378:     } elsif ($target eq 'tex') {
 1379: 	&Apache::lonxml::startredirection();
 1380:     } 
 1381:     return $currentstring;
 1382: }
 1383: 
 1384: sub end_s {
 1385:     my ($target,$token) = @_;
 1386:     my $currentstring = '';
 1387:     if ($target eq 'web' || $target eq 'webgrade') {
 1388: 	$currentstring .= $token->[2];
 1389:     } elsif ($target eq 'tex') {
 1390: 	$currentstring=&Apache::lonxml::endredirection();
 1391: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1392: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1393: 	$currentstring=~s/(\S)\s*$/$1\}/;	
 1394:     }
 1395:     return $currentstring;
 1396: }
 1397: 
 1398: #-- <sub> tag (end tag required)
 1399: sub start_sub {
 1400:     my ($target,$token) = @_;
 1401:     my $currentstring = '';
 1402:     if ($target eq 'web' || $target eq 'webgrade') {
 1403: 	$currentstring .= $token->[4];
 1404:     } elsif ($target eq 'tex') {
 1405: 	$currentstring .= '\ensuremath{_{';
 1406:     } 
 1407:     return $currentstring;
 1408: }
 1409: 
 1410: sub end_sub {
 1411:     my ($target,$token) = @_;
 1412:     my $currentstring = '';
 1413:     if ($target eq 'web' || $target eq 'webgrade') {
 1414: 	$currentstring .= $token->[2];
 1415:     } elsif ($target eq 'tex') {
 1416: 	$currentstring .= '}}';
 1417:     }
 1418:     return $currentstring;
 1419: }
 1420: 
 1421: #-- <sup> tag (end tag required)
 1422: sub start_sup {
 1423:     my ($target,$token) = @_;
 1424:     my $currentstring = '';
 1425:     if ($target eq 'web' || $target eq 'webgrade') {
 1426: 	$currentstring .= $token->[4];
 1427:     } elsif ($target eq 'tex') {
 1428: 	$currentstring .= '\ensuremath{^{';
 1429:     } 
 1430:     return $currentstring;
 1431: }
 1432: 
 1433: sub end_sup {
 1434:     my ($target,$token) = @_;
 1435:     my $currentstring = '';
 1436:     if ($target eq 'web' || $target eq 'webgrade') {
 1437: 	$currentstring .= $token->[2];
 1438:     } elsif ($target eq 'tex') {
 1439: 	$currentstring .= '}}';
 1440:     }
 1441:     return $currentstring;
 1442: }
 1443: 
 1444: #-- <hr> tag (end tag forbidden)
 1445: sub start_hr {
 1446:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1447:     my $currentstring = &end_p();	# End enclosing para.
 1448:     if ($target eq 'web' || $target eq 'webgrade') {
 1449: 	$currentstring .= $token->[4];
 1450:     } elsif ($target eq 'tex') {
 1451: 	my $LaTeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1452: 	if (defined $LaTeXwidth) {
 1453: 	    if ($LaTeXwidth=~/^%/) {
 1454: 		substr($LaTeXwidth,0,1)='';
 1455: 		$LaTeXwidth=($LaTeXwidth/100).'\textwidth';
 1456: 	    }
 1457: 	} else {
 1458: 	    $LaTeXwidth ='0.9\textwidth';
 1459: 	}
 1460: 	my ($pre,$post);
 1461: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1462: 	if (($align eq 'center') || (not defined $align)) {
 1463: 	    $pre=''; $post='';
 1464: 	} elsif ($align eq 'left') {
 1465: 	    $pre='\rlap{'; $post='} \hfill';
 1466: 	} elsif ($align eq 'right') {
 1467: 	    $pre=' \hfill \llap{'; $post='}';
 1468: 	}
 1469: 	$currentstring .= ' \vskip 0 mm \noindent\makebox['.$LaTeXwidth.']{'.$pre.'\makebox['.
 1470:                                     $LaTeXwidth.'][b]{\hrulefill}'.$post.'}\vskip 0 mm ';
 1471:     } 
 1472:     return $currentstring;
 1473: }
 1474: 
 1475: sub end_hr {
 1476:     my ($target,$token) = @_;
 1477:     my $currentstring = '';
 1478:     if ($target eq 'web' || $target eq 'webgrade') {
 1479: 	$currentstring .= $token->[2];
 1480:     }
 1481:     return $currentstring;
 1482: }
 1483: 
 1484: #-- <div> tag (end tag required)
 1485: {
 1486: 
 1487: #  Since div can be nested, the stack below is used
 1488: #  in 'tex' mode to store the ending strings
 1489: #  for the div stack.
 1490: 
 1491:     my @div_end_stack;
 1492: 
 1493: sub start_div {
 1494:     my ($target,$token, $tagstack, $parstack, $parser, $safeeval) = @_;
 1495:     my $currentstring = &end_p();	# Close enclosing para.
 1496:     if ($target eq 'web' || $target eq 'webgrade') {
 1497: 	$currentstring .= $token->[4];
 1498:     } 
 1499:     if ($target eq 'tex') {
 1500: 	# 4 possible alignments: left, right, center, and -missing-.
 1501: 
 1502: 	my $endstring = '';
 1503: 
 1504: 	my $align = lc(&Apache::lonxml::get_param('align', $parstack,
 1505: 						  $safeeval, undef, 1));
 1506: 	if ($align eq 'center') {
 1507: 	    $currentstring .= '\begin{center}';
 1508: 	    $endstring      = '\end{center}';
 1509: 	    if (&is_inside_of($tagstack, "table")) {
 1510: 		$currentstring = &center_correction().$currentstring;
 1511: 	    }
 1512: 	}
 1513: 	elsif ($align eq 'right') {
 1514: 	    $currentstring .= '\begin{flushright}';
 1515: 	    $endstring     .= '\end{flushright}';
 1516: 	} elsif ($align eq 'left') {
 1517: 	    $currentstring .= '\begin{flushleft}';
 1518: 	    $endstring     = '\end{flushleft}';
 1519: 	} else {
 1520: 	
 1521: 	}
 1522: 	$currentstring .= "\n";   # For human readability.
 1523: 	$endstring       = "\n$endstring\n"; # For human readability
 1524: 	push(@div_end_stack, $endstring);
 1525:     }
 1526:     return $currentstring;
 1527: }
 1528: 
 1529: sub end_div {
 1530:     my ($target,$token) = @_;
 1531:     my $currentstring = '';
 1532:     if ($target eq 'web' || $target eq 'webgrade') {
 1533: 	$currentstring .= $token->[2];
 1534:     }
 1535:     if ($target eq 'tex') {
 1536: 	my $endstring = pop @div_end_stack;
 1537: 	$currentstring .= $endstring;
 1538:     }
 1539:     return $currentstring;
 1540: }
 1541: }
 1542: 
 1543: #-- <a> tag (end tag required)
 1544: sub start_a {
 1545:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1546:     my $currentstring = '';
 1547:     if ($target eq 'web' || $target eq 'webgrade') {
 1548: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 1549: 					    undef,1);
 1550: 	$currentstring=&Apache::lonenc::encrypt_ref($token,{'href'=>$href});
 1551:     } elsif ($target eq 'tex') {
 1552: 	my $a=&Apache::lonxml::get_param('href',$parstack,$safeeval,undef,1);
 1553: 	my $b=&Apache::lonxml::get_param('name',$parstack,$safeeval,undef,1);
 1554: 	if ($a=~/\S/) {
 1555: 	    $a=~s/([^\\])%/$1\\\%/g;
 1556: 	    $currentstring .= '\ref{URI: '.$a.'}';
 1557: 	} elsif ($b=~/\S/) {
 1558: 	    $currentstring .= '\ref{Anchor: '.$b.'}';
 1559: 	} else {
 1560: 	    $currentstring.='';
 1561: 	}
 1562:     }
 1563:     return $currentstring;
 1564: }
 1565: 
 1566: sub end_a {
 1567:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1568:     my $currentstring = '';
 1569:     if ($target eq 'web' || $target eq 'webgrade') {
 1570: 	$currentstring .= $token->[2];
 1571:     }
 1572:     return $currentstring;
 1573: }
 1574: 
 1575: #-- <li> tag (end tag optional)
 1576: sub start_li {
 1577:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1578:     my $currentstring = '';
 1579:     if ($target eq 'web' || $target eq 'webgrade') {
 1580: 	$currentstring = $token->[4];     
 1581:     } elsif ($target eq 'tex') {
 1582: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1583: 	my $value=&Apache::lonxml::get_param('value',$parstack,$safeeval,undef,0);
 1584: 	#FIXME need to support types i and I 
 1585: 	if ($type=~/disc/) {
 1586: 	    $currentstring .= ' \item[$\bullet$] ';
 1587: 	} elsif ($type=~/circle/) {
 1588: 	    $currentstring .= ' \item[$\circ$] ';
 1589: 	} elsif ($type=~/square/) {
 1590: 	    $currentstring .= ' \item[$\diamond$] ';
 1591: 	} elsif ($type eq '1') {
 1592: 	    $currentstring .= ' \item['.($Apache::londefdef::list_index+1).'.]';
 1593: 	} elsif ($type eq 'A') {
 1594: 	    $currentstring .= ' \item['.('A'..'Z')[$Apache::londefdef::list_index].'.]';
 1595: 	} elsif ($type eq 'a') {
 1596: 	    $currentstring .= ' \item['.('a'..'z')[$Apache::londefdef::list_index].'.]';
 1597: 	} elsif ($value ne '') {
 1598: 	    $currentstring .= ' \item['.$value.'] ';
 1599: 	} else {
 1600: 	    $currentstring .= ' \item ';
 1601: 	}  
 1602: 	$Apache::londefdef::list_index++;
 1603:     }
 1604:     return $currentstring;
 1605: }
 1606: 
 1607: sub end_li {
 1608:     my ($target,$token) = @_;
 1609:     my $currentstring = &end_p();	# In case there's a <p> in the <li>
 1610:     if ($target eq 'web' || $target eq 'webgrade') {
 1611: 	$currentstring .= $token->[2];     
 1612:     } 
 1613:     return $currentstring;
 1614: }
 1615: 
 1616: #-- <u> tag (end tag required)
 1617: sub start_u {
 1618:     my ($target,$token) = @_;
 1619:     my $currentstring = '';
 1620:     if ($target eq 'web' || $target eq 'webgrade') {
 1621: 	$currentstring .= $token->[4];
 1622:     } elsif ($target eq 'tex') {
 1623: 	&Apache::lonxml::startredirection();
 1624:     } 
 1625:     return $currentstring;
 1626: }
 1627: 
 1628: sub end_u {
 1629:     my ($target,$token) = @_;
 1630:     my $currentstring = '';
 1631:     if ($target eq 'web' || $target eq 'webgrade') {
 1632: 	$currentstring .= $token->[2];
 1633:     } elsif ($target eq 'tex') {
 1634: 	$currentstring=&Apache::lonxml::endredirection();
 1635: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1636: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1637: 	$currentstring=~s/(\S)\s*$/$1\}/;		
 1638:     }
 1639:     return $currentstring;
 1640: }
 1641: 
 1642: #-- <ul> tag (end tag required)
 1643: sub start_ul {
 1644:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1645:     my $currentstring = &end_p();	# Close off enclosing list.
 1646:     if ($target eq 'web' || $target eq 'webgrade') {
 1647: 	$currentstring .= $token->[4];     
 1648:     } elsif ($target eq 'tex') {
 1649: 	my $TeXtype=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1650: 	$Apache::londefdef::list_index=0;
 1651: 	if ($TeXtype eq 'disc') {
 1652: 	    $currentstring .= '\renewcommand{\labelitemi}{$\bullet$}'.
 1653:                               '\renewcommand{\labelitemii}{$\bullet$}'. 
 1654:                               '\renewcommand{\labelitemiii}{$\bullet$}'.
 1655:                               '\renewcommand{\labelitemiv}{$\bullet$}';
 1656: 	} elsif ($TeXtype eq 'circle') {
 1657: 	    $currentstring .= '\renewcommand{\labelitemi}{$\circ$}'.
 1658:                               '\renewcommand{\labelitemii}{$\circ$}'. 
 1659:                               '\renewcommand{\labelitemiii}{$\circ$}'.
 1660:                               '\renewcommand{\labelitemiv}{$\circ$}';
 1661: 	} elsif ($TeXtype eq 'square') {
 1662: 	    $currentstring .= '\renewcommand{\labelitemi}{$\diamond$}'.
 1663:                               '\renewcommand{\labelitemii}{$\diamond$}'. 
 1664:                               '\renewcommand{\labelitemiii}{$\diamond$}'.
 1665:                               '\renewcommand{\labelitemiv}{$\diamond$}';
 1666: 	}
 1667: 	$currentstring .= '\strut \begin{itemize}';  
 1668:     } 
 1669:     return $currentstring;
 1670: }
 1671: 
 1672: sub end_ul {
 1673:     my ($target,$token) = @_;
 1674:     my $currentstring = '';
 1675:     if ($target eq 'web' || $target eq 'webgrade') {
 1676: 	$currentstring = $token->[2];     
 1677:     } elsif ($target eq 'tex') {
 1678: 	$currentstring = '\end{itemize} \renewcommand{\labelitemi}{$\bullet$}'.
 1679:                                '\renewcommand{\labelitemii}{$\bullet$}'. 
 1680:                                '\renewcommand{\labelitemiii}{$\bullet$}'.
 1681:                                '\renewcommand{\labelitemiv}{$\bullet$}\strut ';  
 1682:     } 
 1683:     return $currentstring;
 1684: }
 1685: 
 1686: #-- <menu> tag (end tag required)
 1687: sub start_menu {
 1688:     my ($target,$token) = @_;
 1689:     my $currentstring = '';
 1690:     if ($target eq 'web' || $target eq 'webgrade') {
 1691: 	$currentstring = $token->[4];     
 1692:     } elsif ($target eq 'tex') {
 1693: 	$currentstring = " \\begin{itemize} ";  
 1694:     } 
 1695:     return $currentstring;
 1696: }
 1697: 
 1698: sub end_menu {
 1699:     my ($target,$token) = @_;
 1700:     my $currentstring = '';
 1701:     if ($target eq 'web' || $target eq 'webgrade') {
 1702: 	$currentstring = $token->[2];     
 1703:     } elsif ($target eq 'tex') {
 1704: 	$currentstring = " \\end{itemize}";  
 1705:     } 
 1706:     return $currentstring;
 1707: }
 1708: 
 1709: #-- <dir> tag (end tag required)
 1710: sub start_dir {
 1711:     my ($target,$token) = @_;
 1712:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1713:     if ($target eq 'web' || $target eq 'webgrade') {
 1714: 	$currentstring .= $token->[4];     
 1715:     } elsif ($target eq 'tex') {
 1716: 	$currentstring .= " \\begin{itemize} ";  
 1717:     } 
 1718:     return $currentstring;
 1719: }
 1720: 
 1721: sub end_dir {
 1722:     my ($target,$token) = @_;
 1723:     my $currentstring = '';
 1724:     if ($target eq 'web' || $target eq 'webgrade') {
 1725: 	$currentstring = $token->[2];     
 1726:     } elsif ($target eq 'tex') {
 1727: 	$currentstring = " \\end{itemize}";  
 1728:     } 
 1729:     return $currentstring;
 1730: }
 1731: 
 1732: #-- <ol> tag (end tag required)
 1733: sub start_ol {
 1734:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1735:     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
 1736:     if ($target eq 'web' || $target eq 'webgrade') {
 1737: 	$currentstring .= $token->[4];     
 1738:     } elsif ($target eq 'tex') {
 1739: 	$Apache::londefdef::list_index=0;
 1740: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1741: 	if ($type eq '1') {
 1742: 	    $currentstring .= '\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1743:                               '\renewcommand{\labelenumii}{\arabic{enumii}.}'. 
 1744:                               '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1745:                               '\renewcommand{\labelenumiv}{\arabic{enumiv}.}';
 1746: 	} elsif ($type eq 'A') {
 1747: 	    $currentstring .= '\renewcommand{\labelenumi}{\Alph{enumi}.}'.
 1748:                               '\renewcommand{\labelenumii}{\Alph{enumii}.}'. 
 1749:                               '\renewcommand{\labelenumiii}{\Alph{enumiii}.}'.
 1750:                               '\renewcommand{\labelenumiv}{\Alph{enumiv}.}';
 1751: 	} elsif ($type eq 'a') {
 1752: 	    $currentstring .= '\renewcommand{\labelenumi}{\alph{enumi}.}'.
 1753:                               '\renewcommand{\labelenumii}{\alph{enumii}.}'.
 1754:                               '\renewcommand{\labelenumiii}{\alph{enumiii}.}'.
 1755:                               '\renewcommand{\labelenumiv}{\alph{enumiv}.}';
 1756: 	} elsif ($type eq 'i') {
 1757: 	    $currentstring .= '\renewcommand{\labelenumi}{\roman{enumi}.}'.
 1758:                               '\renewcommand{\labelenumii}{\roman{enumii}.}'.
 1759:                               '\renewcommand{\labelenumiii}{\roman{enumiii}.}'.
 1760:                               '\renewcommand{\labelenumiv}{\roman{enumiv}.}';
 1761: 	} elsif ($type eq 'I') {
 1762: 	    $currentstring .= '\renewcommand{\labelenumi}{\Roman{enumi}.}'.
 1763:                               '\renewcommand{\labelenumii}{\Roman{enumii}.}'.
 1764:                               '\renewcommand{\labelenumiii}{\Roman{enumiii}.}'.
 1765:                               '\renewcommand{\labelenumiv}{\Roman{enumiv}.}';
 1766: 	}
 1767: 	$currentstring .= '\strut \begin{enumerate}';  
 1768:     } 
 1769:     return $currentstring;
 1770: }
 1771: 
 1772: sub end_ol {
 1773:     my ($target,$token) = @_;
 1774:     my $currentstring = '';
 1775:     if ($target eq 'web' || $target eq 'webgrade') {
 1776: 	$currentstring = $token->[2];     
 1777:     } elsif ($target eq 'tex') {
 1778: 	$currentstring = '\end{enumerate}\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1779:                                         '\renewcommand{\labelenumii}{\arabic{enumii}.}'.
 1780:                                         '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1781:                                         '\renewcommand{\labelenumiv}{\arabic{enumiv}.}\strut ';  
 1782:     } 
 1783:     return $currentstring;
 1784: }
 1785: 
 1786: #-- <dl> tag (end tag required)
 1787: sub start_dl {
 1788:     my ($target,$token) = @_;
 1789:     my $currentstring = &end_p();	# In case there's a <p> unclosed prior to the list.
 1790:     if ($target eq 'web' || $target eq 'webgrade') {
 1791: 	$currentstring .= $token->[4];     
 1792:     } elsif ($target eq 'tex') {
 1793: 	$currentstring .= '\begin{description}';
 1794: 	$Apache::londefdef::DL++;
 1795: 	push(@Apache::londefdef::description,[]);
 1796: 	$Apache::londefdef::DD[$Apache::londefdef::DL]=0;
 1797: 	$Apache::londefdef::DT[$Apache::londefdef::DL]=0;
 1798: 	$Apache::londefdef::seenDT[$Apache::londefdef::DL]=0;
 1799:     } 
 1800:     return $currentstring;
 1801: }
 1802: 
 1803: sub end_dl {
 1804:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1805:     my $currentstring = '';
 1806:     if ($target eq 'web' || $target eq 'webgrade') {
 1807: 	$currentstring = $token->[2];     
 1808:     } elsif ($target eq 'tex') {
 1809: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1810: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1811: 	foreach my $element (@{$Apache::londefdef::description[-1]}) {
 1812: 	    $currentstring.=' '.$element.' ';
 1813: 	}
 1814: 	pop(@Apache::londefdef::description);
 1815: 	$currentstring.='\end{description}';  
 1816: 	delete($Apache::londefdef::DD[$Apache::londefdef::DL]);
 1817: 	delete($Apache::londefdef::DT[$Apache::londefdef::DL]);
 1818: 	delete($Apache::londefdef::seenDT[$Apache::londefdef::DL]);
 1819: 	$Apache::londefdef::DL--;
 1820:     } 
 1821:     return $currentstring;
 1822: }
 1823: 
 1824: #-- <dt> tag (end tag optional)
 1825: sub start_dt {
 1826:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1827:     my $currentstring='';
 1828:     if ($target eq 'web' || $target eq 'webgrade') {
 1829: 	$currentstring = $token->[4];     
 1830:     } elsif ($target eq 'tex') {
 1831: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1832: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 1833: 	&Apache::lonxml::startredirection();
 1834: 	$Apache::londefdef::DT[-1]++;
 1835: 	$Apache::londefdef::seenDT[-1]=1;
 1836:     } 
 1837:     return $currentstring;
 1838: }
 1839: 
 1840: sub end_dt {
 1841:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1842:     my $currentstring = '';
 1843:     if ($target eq 'web' || $target eq 'webgrade') {
 1844: 	$currentstring = $token->[2];    
 1845:     } elsif ($target eq 'tex') {
 1846: 	if ($Apache::londefdef::DT[-1]) {
 1847: 	    my $data=&item_cleanup();
 1848: 	    push(@{$Apache::londefdef::description[-1]},'\item['.$data.'] \strut \vskip 0mm');
 1849: 	    $Apache::londefdef::DT[-1]--;
 1850: 	}
 1851:     } 
 1852:     return $currentstring;
 1853: }
 1854: 
 1855: sub item_cleanup {
 1856:     my $item=&Apache::lonxml::endredirection();
 1857:     $item=~s/\\begin{center}//g;
 1858:     $item=~s/\\end{center}//g;
 1859:     return $item;
 1860: }
 1861: 
 1862: #-- <dd> tag (end tag optional)
 1863: sub start_dd {
 1864:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1865:     my $currentstring = '';
 1866:     if ($target eq 'web' || $target eq 'webgrade') {
 1867: 	$currentstring = $token->[4];     
 1868:     } elsif ($target eq 'tex') {
 1869: 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 1870: 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_);}
 1871: 	if (!$Apache::londefdef::seenDT[-1]) {
 1872: 	    push(@{$Apache::londefdef::description[-1]},'\item[\strut] \strut \vskip 0mm ');
 1873: 	}
 1874: 	push(@{$Apache::londefdef::description[-1]},'');
 1875: 	$Apache::londefdef::description[-1]->[-1].=' \strut ';
 1876: 	$Apache::londefdef::DD[-1]++;
 1877: 	&Apache::lonxml::startredirection();
 1878:     } 
 1879:     return $currentstring;
 1880: }
 1881: 
 1882: sub end_dd {
 1883:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1884:     my $currentstring = '';
 1885:     if ($target eq 'web' || $target eq 'webgrade') {
 1886: 	$currentstring = $token->[2];    
 1887:     }  elsif ($target eq 'tex') {
 1888: 	$Apache::londefdef::description[-1]->[-1].=
 1889: 	    &Apache::lonxml::endredirection().' \vskip 0mm ';
 1890: 	$Apache::londefdef::DD[-1]--;
 1891:     }
 1892:     return $currentstring;
 1893: }
 1894: 
 1895: #-- <table> tag (end tag required)
 1896: #       <table> also ends any prior <p> that is not closed.
 1897: #               but, unless I allow <p>'s to nest, that's the
 1898: #               only way I could think of to allow <p> in 
 1899: #               <tr> <th> bodies
 1900: #
 1901: #list of supported attributes: border,width,TeXwidth
 1902: sub start_table {
 1903:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1904:     my $textwidth = '';
 1905:     my $currentstring = &end_p();
 1906:     if ($target eq 'web' || $target eq 'webgrade') {
 1907: 	$currentstring .= $token->[4];     
 1908:     } elsif ($target eq 'tex') {
 1909: 	my $aa = {};
 1910: 	push @Apache::londefdef::table, $aa; 
 1911: 	$Apache::londefdef::table[-1]{'row_number'} = -1;
 1912:         #maximum table's width (default coincides with text line length)
 1913: 	if ($#Apache::londefdef::table==0) {
 1914: 	    $textwidth=&recalc($env{'form.textwidth'}); #result is always in mm
 1915: 	    $textwidth=~/(\d+\.?\d*)/;
 1916: 	    $textwidth=0.95*$1; #accounts "internal" LaTeX space for table frame
 1917: 	} else {
 1918: 	    if ($Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]=~/\d/) {
 1919: 		#the maximum width of nested table is determined by LATeX width of parent cell
 1920: 		$textwidth=$Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]; 
 1921: 	    } else {
 1922:               #try to use all space not used before (minus 5% for LaTeX table internal) - rather silly
 1923: 		$textwidth=$Apache::londefdef::table[-2]{'width'};
 1924: 		for (my $i=0;$i<$Apache::londefdef::table[-2]{'counter_columns'};$i++) {
 1925: 		    $textwidth=$textwidth-$Apache::londefdef::table[-2]{'TeXlen'}[0][$i];
 1926: 		}
 1927: 	    }
 1928: 	}
 1929: 
 1930: 	# width either comes forced from the TeXwidth or the width parameters.
 1931: 	# in either case it can be a percentage or absolute width.
 1932: 	# in the width case we ignore absolute width 
 1933: 	my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1934: 	if (!defined($TeXwidth)) {
 1935: 	    my $htmlwidth = &Apache::lonxml::get_param('width',$parstack,
 1936: 						       $safeeval,undef,1);
 1937: 	    if ($htmlwidth =~ /%/) {
 1938: 		$TeXwidth = $htmlwidth;
 1939: 	    } else { 
 1940: 		$TeXwidth = $textwidth;
 1941: 	    }
 1942: 	} else {
 1943: 	    $Apache::londefdef::table[-1]{'forcedtablewidth'} = 1;
 1944: 	}
 1945: 	if ($TeXwidth=~/%/) {
 1946: 	    $Apache::londefdef::table[-1]{'percent'}=1;
 1947: 	    $TeXwidth=~/(\d+)/;
 1948:             $Apache::londefdef::table[-1]{'width'}=$1*$textwidth/100;
 1949: 	} else {
 1950: 	    $Apache::londefdef::table[-1]{'width'}=$TeXwidth;
 1951: 	}
 1952:         #  In the end, however the table width cannot be wider than $textwidth...
 1953: 	
 1954: 	if ($Apache::londefdef::table[-1]{'width'} > $textwidth) {
 1955: 	    $Apache::londefdef::table[-1]{'width'} = $textwidth;
 1956: 	}
 1957: 
 1958:         #table's border
 1959: 	my $border = &Apache::lonxml::get_param('border',$parstack,$safeeval); 
 1960:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 1961: 	unless (defined $border) { $border = 0; }
 1962: 	if ($border) { 
 1963: 	    $Apache::londefdef::table[-1]{'hinc'} = '\hline '; 
 1964: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 1965: 	    $Apache::londefdef::table[-1]{'vvinc'} = '|';
 1966: 	} else {
 1967: 	    $Apache::londefdef::table[-1]{'hinc'} = ''; 
 1968: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 1969: 	    $Apache::londefdef::table[-1]{'vvinc'} = '';
 1970: 	}
 1971: 	if ($#Apache::londefdef::table==0) {
 1972: 	    #    Note that \newline seems to destroy the alignment envs.
 1973: 	    # $Apache::londefdef::table[-1]{'output'}='\strut\newline\strut\setlength{\tabcolsep}{1 mm}';
 1974: 	    $Apache::londefdef::table[-1]{'output'}='\strut'.'\\\\'."\n".'\strut\setlength{\tabcolsep}{1 mm}';
 1975: 	}
 1976: 	$Apache::londefdef::table[-1]{'output'}.=' \noindent \begin{tabular} ';
 1977:         $Apache::londefdef::table[-1]{'TeXlen'}=[];
 1978:         $Apache::londefdef::table[-1]{'objectlen'}=[];
 1979:         $Apache::londefdef::table[-1]{'objectsignal'}=[];
 1980:         $Apache::londefdef::table[-1]{'maxlen'}=[];
 1981:         $Apache::londefdef::table[-1]{'minlen'}=[];
 1982:         $Apache::londefdef::table[-1]{'content'}=[];
 1983:         $Apache::londefdef::table[-1]{'align'}=[];
 1984:         $currentstring.='\keephidden{NEW TABLE ENTRY}';
 1985: 
 1986: 
 1987:     }
 1988:     return $currentstring;
 1989: }
 1990:  
 1991: sub end_table {
 1992:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1993:     my $currentstring = '';
 1994:     if ($target eq 'web' || $target eq 'webgrade') {
 1995: 	$currentstring = $token->[2];     
 1996:     } elsif ($target eq 'tex') {
 1997: 	my $inmemory = '';
 1998: 	my $output = '';
 1999: 	my $WARNING='';
 2000:         #width of columns from TeXwidth attributes
 2001: 
 2002: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2003: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2004: 		if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]<$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn]) {
 2005: 		    $Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]=$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn];
 2006: 		}	
 2007: 	    }
 2008: 	}
 2009:         #free space and number of empty columns
 2010: 	my ($available_space,$empty_columns)=($Apache::londefdef::table[-1]{'width'},0);
 2011: 	if ($#Apache::londefdef::table ne 0) {$available_space=0.9*$available_space;} 
 2012: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2013: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]==0) {
 2014: 		$empty_columns++;
 2015: 	    } else {
 2016: 		$available_space=$available_space-$Apache::londefdef::table[-1]{'TeXlen'}[0][$jn];
 2017: 	    }
 2018: 	}
 2019:         #boundaries for contents columns
 2020: 	my @min_len=();#columns can not be narrower 
 2021: 	my @max_len=();#maximum length of column
 2022: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2023: 		my ($localmin,$localmax)=(0,0);
 2024: 		for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2025: 		    if ($localmin<$Apache::londefdef::table[-1]{'minlen'}[$in][$jn]) {
 2026: 			$localmin=$Apache::londefdef::table[-1]{'minlen'}[$in][$jn];
 2027: 		    }
 2028: 		    if ($localmax<$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn]) {
 2029: 			$localmax=$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn];
 2030: 		    }
 2031: 		}
 2032: 		push @min_len, $localmin;
 2033: 		push @max_len, $localmax;
 2034: 	}
 2035: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2036: 	    my $localmin=0,;
 2037: 	    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2038: 		if ($localmin<$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn]) {
 2039: 		    $localmin=$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn];
 2040: 		}
 2041: 	    }
 2042: 	    if ($max_len[$jn]<$localmin) {
 2043: 		$max_len[$jn]=$localmin;
 2044: 	    	$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 2045: 	    }#object size is bigger
 2046: 	    if ($min_len[$jn]<$localmin) {
 2047: 		$min_len[$jn]=$localmin;
 2048: 		$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 2049: 	    }#object size is bigger
 2050: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]!=0) {
 2051: 		$min_len[$jn]=0;
 2052: 		$max_len[$jn]=0;
 2053: 	    }
 2054: 	}
 2055:        #final adjustment of column width
 2056: 	my @fwidth=@{$Apache::londefdef::table[-1]{'TeXlen'}[0]};#final width array
 2057: 	my @adjust=();
 2058:         #step 1. adjustment by maximum value
 2059: 	my $space_neeeded=0;
 2060: 	for (my $jn=0;$jn<=$#max_len;$jn++) {
 2061: 	    $space_neeeded=$space_neeeded+$max_len[$jn];
 2062: 	}
 2063: 	if ($space_neeeded<=$available_space) {
 2064: 	    for (my $jn=0;$jn<=$#max_len;$jn++) {
 2065: 		if ($fwidth[$jn]==0) {
 2066: 		    $fwidth[$jn]=$max_len[$jn];
 2067: 		}
 2068: 	    }
 2069: 	} else {
 2070:         #step 2. adjustment by minimum value (estimation)
 2071: 	    $space_neeeded=0;
 2072: 	    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2073: 		$space_neeeded+=$min_len[$jn];
 2074: 	    }
 2075: 	    if ($space_neeeded>$available_space) {
 2076: 		$WARNING=' \textbf{NOT ENOUGH SPACE FOR TABLE} ';
 2077: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 2078: 		    if ($fwidth[$jn]==0) {
 2079: 			$fwidth[$jn]=$min_len[$jn];
 2080: 		    }
 2081: 		}
 2082: 		#check if we have objects which can be scaled
 2083: 		my $how_many_to_scale=0;
 2084: 		my @to_scale=();
 2085: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 2086: 		    if ($Apache::londefdef::table[-1]{'objectsignal'}[$jn] eq '1') {
 2087: 			$how_many_to_scale++;
 2088: 			push @to_scale, $jn;
 2089: 		    }
 2090: 		}
 2091: 		if ($how_many_to_scale>0) {
 2092: 		    my $space_to_adjust=($space_neeeded-$available_space)/$how_many_to_scale;
 2093: 		    foreach my $jn (@to_scale) {
 2094: 			for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2095: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/;
 2096: 			    if ($1 ne '') {
 2097: 				my $current_length=&recalc($1);
 2098: 				$current_length=~/(\d+\.?\d*)/;
 2099: 				$current_length=$current_length-$space_to_adjust;
 2100: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/width=$current_length mm/;
 2101: 			    }
 2102: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/\[(\d+\.?\d*)\s*mm\]/;
 2103: 			    if ($1 ne '') {
 2104: 				my $current_length=$1;
 2105: 				$current_length=$current_length-$space_to_adjust;
 2106: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/\[(\d+\.?\d*)\s*mm\]/\[$current_length mm\]/;
 2107: 			    }				
 2108: 			}
 2109: 			$fwidth[$jn]=$fwidth[$jn]-$space_to_adjust;
 2110: 		    }
 2111: 		}
 2112: 	    } else {
 2113: 	      #step 3. adjustment over minimal + corrections
 2114: 		my $enlarge_coef=$available_space/$space_neeeded;
 2115: 		my $acsessive=0;
 2116: 		for (my $jn=0;$jn<=$#min_len;$jn++) {
 2117: 		    $adjust[$jn]=$min_len[$jn]*$enlarge_coef;
 2118: 		    if ($adjust[$jn]>$max_len[$jn]) {
 2119: 			$fwidth[$jn]=$max_len[$jn];
 2120: 			$acsessive=$acsessive+$adjust[$jn]-$max_len[$jn];
 2121: 			$adjust[$jn]=0;
 2122: 		    }
 2123: 		}
 2124: 		if ($acsessive>0) {
 2125: 		#we have an excess of space and can redistribute it
 2126: 		    my $notempty_columns=0;
 2127: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2128: 			if ($adjust[$jn]!=0) {
 2129: 			    $notempty_columns++;
 2130: 			}
 2131: 		    }
 2132: 		    my $per_column=$acsessive/$notempty_columns;
 2133: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2134: 			if ($adjust[$jn]!=0) {
 2135: 			    $adjust[$jn]+=$per_column;
 2136: 			    $fwidth[$jn]=$adjust[$jn];
 2137: 			}
 2138: 		    }
 2139: 		} else {
 2140: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2141: 			$fwidth[$jn]=$adjust[$jn];
 2142: 		    }
 2143: 		}
 2144: 	    }
 2145: 	}
 2146:         #use all available width if it is defined in % or as TeXwidth
 2147:         if (($Apache::londefdef::table[-1]{'percent'}==1) || ($Apache::londefdef::table[-1]{'forcetablewidth'}==1)) {
 2148: 	    my $current=0; 
 2149: 	    for (my $i=0;$i<=$#fwidth;$i++) {  
 2150: 		$current+=$fwidth[$i];
 2151: 	    }
 2152: 	    my $coef=$Apache::londefdef::table[-1]{'width'}/$current;
 2153: 	    for (my $i=0;$i<=$#fwidth;$i++) {  
 2154: 		$fwidth[$i]*=$coef;
 2155: 	    }
 2156: 	}
 2157:         #removing of empty columns if allowed
 2158:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 2159: 	if ($permission eq 'yes') {
 2160: 	    my @cleaned_table=();
 2161:             my @cleaned_header=();
 2162: 	    my $colind=0;
 2163: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2164: 		if ($fwidth[$jn]!=0) {
 2165: 		    #we need to copy column
 2166: 		    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2167: 			$cleaned_table[$in][$colind]=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2168: 			$cleaned_header[$colind]=$fwidth[$jn];
 2169: 		    }
 2170: 		    $colind++;
 2171: 		}
 2172: 	    }
 2173: 	    $Apache::londefdef::table[-1]{'content'}=\@cleaned_table;
 2174: 	    @fwidth=@cleaned_header;
 2175: 	}
 2176: 	#construct header of the table
 2177: 	my $header_of_table = '{'.$Apache::londefdef::table[-1]{'vvinc'};
 2178: 	for (my $in=0;$in<=$#fwidth;$in++) {
 2179: 	    $header_of_table.='p{'.$fwidth[$in].' mm}'.$Apache::londefdef::table[-1]{'vvinc'};
 2180: 	}
 2181: 	$header_of_table .= '}';
 2182: 	#fill the table
 2183: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2184: 	    my $have_rowspan = 0;
 2185: 	    for (my $jn=0;$jn<=$#fwidth;$jn++) {
 2186: 
 2187: 		#-----------------------------------------------------------
 2188:                 #   I think this order of doing things will ensure that
 2189: 		#   single rowspan, columspan and combined row/colspans will
 2190:                 #   work correctly.  LaTeX is delicate here.
 2191: 		#    RF.
 2192: 
 2193: 		# Start a rowspan if necessary:
 2194: 
 2195: 		my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
 2196: 		my $colspan = $Apache::londefdef::table[-1]{'colspan'}[$in][$jn];
 2197: 		#
 2198: 		#  Do the appropriate magic if this has a colspan
 2199: 		# 
 2200: 
 2201: 		if ($colspan > 1) {
 2202: 		    $output .= '\multicolumn{'.
 2203: 			$colspan
 2204: 			.'}{|l|}{';
 2205: 		}
 2206: 
 2207: 		if ($rowspan > 1) {
 2208: 		    $have_rowspan++;
 2209: 		    $output .= '\multirow{'.$rowspan.'}[0]{'.$fwidth[$jn].'mm}{';
 2210: 		}
 2211: 		if (($rowspan eq '^') || ($rowspan eq '_')) {
 2212: 		    $have_rowspan++;
 2213: 		}
 2214: 		#--------------------------------------------------------------
 2215: 
 2216: 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2217: 		    $output.=&center_correction().'\begin{center}';
 2218: 		} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2219: 		    $output.=' \hfill \llap{'
 2220: 		}
 2221: 		$output.=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2222: 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2223: 		    $output.='\end{center}';
 2224: 		} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2225: 		    $output.='} ';
 2226: 		}
 2227: 		# Close off any open multirow:
 2228: 		
 2229: 		if ($rowspan > 1) {
 2230: 		    $output .= '}';
 2231: 		}
 2232: 		#  Close off the colspan...
 2233: 		#
 2234: 		if ($colspan > 1)  {
 2235: 		    $output .= '}';
 2236: 		    $jn += $colspan-1; # Adjust for number of rows really left.
 2237: 		}
 2238:                 if ($jn!=$#fwidth) {$output.=' '.$Apache::londefdef::table[-1]{'vinc'};}
 2239: 	    }
 2240: 	    #  If have_rowspan > 0, and borders are on, then 
 2241: 	    #  we need to do more than put an \hline at the bottom of row.
 2242: 	    #  we need to do the appropriate \cline to ensure that
 2243: 	    #  the spanned rows don't have \hlines through them.
 2244: 
 2245: 	    if (($Apache::londefdef::table[-1]{'hinc'} =~ /\\hline/) && $have_rowspan) {
 2246: 		$output .= ' \\\\ ';
 2247: 		for (my $jn=0; $jn<=$#fwidth;$jn++) {
 2248: 		    my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
 2249: 		    if ($rowspan ne "^") {
 2250: 			if (($rowspan <= 1) || ($rowspan eq '_')) {
 2251: 			    my $column = $jn+1;
 2252: 			    $output .= '\cline{'.$column.'-'.$column.'} ';
 2253: 			}
 2254: 		    }
 2255: 		}
 2256: 
 2257: 	    } else {
 2258: 		$output.=' \\\\ '.$Apache::londefdef::table[-1]{'hinc'}.' ';
 2259: 	    }
 2260: 	}
 2261: 	# Note that \newline destroys alignment env's produced  by e.g. <div>
 2262: 	# $Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut\newline\strut ';
 2263: 	$Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut'.'\\\\'."\n".'\strut ';
 2264: 	if ($#Apache::londefdef::table > 0) {	    
 2265: 	    my $inmemory = $Apache::londefdef::table[-1]{'output'};
 2266: 	    # Figure out max/and min width  by summing us and then
 2267: 	    # apply that to the current column of the table we nest in
 2268: 	    # if it's larger than the current width or the current width
 2269: 	    # is undefined.
 2270: 	    #
 2271: 	    my $min_nested_width = 0;
 2272: 	    my $max_nested_width = 0;
 2273: 	    for (my $col = 0; $col <= $Apache::londefdef::table[-1]{'counter_columns'}; $col++) {
 2274: 		$min_nested_width +=  $min_len[$col];
 2275: 		$max_nested_width +=  $max_len[$col];
 2276: 		
 2277: 	    }
 2278: 	    # Fudge in an extra 5 mm for borders etc:
 2279: 	    
 2280: 	    $min_nested_width += 5;
 2281: 	    $max_nested_width += 5;
 2282: 
 2283: 	    my $outer_column = $Apache::londefdef::table[-2]{'counter_columns'};
 2284: 	    my $outer_row    = $Apache::londefdef::table[-2]{'row_number'};
 2285: 	    if ($min_nested_width > $Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column]) {
 2286: 		$Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column] = $min_nested_width;
 2287: 	    }
 2288: 	    if ($max_nested_width > $Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column]) {
 2289: 		$Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column] = $max_nested_width;
 2290: 	    }
 2291: 
 2292: 	    pop @Apache::londefdef::table;
 2293: 	    push @{$Apache::londefdef::table[-1]{'include'}}, $inmemory;
 2294: 	} else {
 2295: 	    $currentstring .= $Apache::londefdef::table[-1]{'output'};
 2296: 	    pop @Apache::londefdef::table;
 2297: 	    undef @Apache::londefdef::table;
 2298: 	}
 2299:     }
 2300:     return $currentstring;
 2301: }
 2302: 
 2303: #-- <tr> tag (end tag optional)
 2304: sub start_tr {
 2305:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2306:     my $currentstring = '';
 2307:     if ($target eq 'web' || $target eq 'webgrade') {
 2308: 	$currentstring = $token->[4];     
 2309:     } elsif ($target eq 'tex') {
 2310: 	$Apache::londefdef::table[-1]{'row_number'}++;
 2311: 	my $alignchar=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 2312: 	if ($alignchar ne '') {
 2313: 	    push @ {$Apache::londefdef::table[-1]{'rows'} },substr($alignchar,0,1);
 2314: 	} else {
 2315: 	    push @ {$Apache::londefdef::table[-1]{'rows'} }, 'l';
 2316: 	}
 2317: 	push ( @{ $Apache::londefdef::table[-1]{'rowdata'} }, $Apache::londefdef::table[-1]{'hinc'});
 2318: 	#
 2319: 	#  Need to save the number of table columns to preserve the max # columns.
 2320: 	#
 2321: 	$Apache::londefdef::table[-1]{'prior_columns'}   = $Apache::londefdef::table[-1]{'counter_columns'};
 2322: 	$Apache::londefdef::table[-1]{'counter_columns'} = -1;
 2323: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}}, [];
 2324: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}}, [];
 2325: 	push @ {$Apache::londefdef::table[-1]{'minlen'}}, [];
 2326: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}}, [];
 2327: 	push @ {$Apache::londefdef::table[-1]{'content'}}, [];
 2328:     } 
 2329:     return $currentstring;
 2330: }
 2331:         
 2332: sub end_tr {
 2333:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2334:     my $currentstring = &end_p();	# Close any pending <p> in the row.
 2335:     if ($target eq 'web' || $target eq 'webgrade') {
 2336: 	$currentstring .= $token->[2];     
 2337:     } elsif ($target eq 'tex') {
 2338: 	if ($Apache::londefdef::TD_redirection) {
 2339: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2340: 	}
 2341: 	# Counter columns must be the maximum number of columns seen
 2342: 	# in the table so far so:
 2343: 	if ($Apache::londefdef::table[-1]{'prior_columns'} > $Apache::londefdef::table[-1]{'counter_columns'}) {
 2344: 	    $Apache::londefdef::table[-1]{'counter_columns'} = $Apache::londefdef::table[-1]{'prior_columns'};
 2345: 	}
 2346: 
 2347: 
 2348: 	
 2349:     }
 2350:     return $currentstring;
 2351: }
 2352: 
 2353: #-- <td> tag (end tag optional)
 2354: sub start_td {
 2355:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2356:     my $currentstring = '';
 2357:     if ($target eq 'web' || $target eq 'webgrade') {
 2358: 	$currentstring = $token->[4];     
 2359:     } elsif ($target eq 'tex') {
 2360: 	$Apache::londefdef::TD_redirection = 1;
 2361: 	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
 2362:     } 
 2363:     return $currentstring;
 2364: }   
 2365:     
 2366: sub tag_check {
 2367:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2368:     my @ar=@$parstack; 
 2369:     for (my $i=$#ar-1;$i>=0;$i--) {
 2370: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2371: 	    &start_td_tex($parstack,$parser,$safeeval);
 2372: 	    last;
 2373: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2374: 	    splice @ar, $i+1;
 2375: 	    &end_td_tex(\@ar,$parser,$safeeval);
 2376: 	    &start_td_tex($parstack,$parser,$safeeval);
 2377: 	    last;
 2378: 	}
 2379:     }
 2380:     return '';
 2381: }
 2382:  
 2383: sub start_td_tex {
 2384:     my ($parstack,$parser,$safeeval) = @_;
 2385:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2386:     if ($alignchar eq '') {
 2387: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2388:     }
 2389:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2390:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2391:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2392:     if (defined $TeXwidth) {		
 2393: 	my $current_length=&recalc($TeXwidth);
 2394: 	$current_length=~/(\d+\.?\d*)/;
 2395: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2396:     }
 2397:     &Apache::lonxml::startredirection();
 2398:     return '';
 2399: }
 2400: 
 2401: sub end_td_tex {
 2402:     my ($parstack,$parser,$safeeval) = @_;
 2403:     my $current_row    = $Apache::londefdef::table[-1]{'row_number'};
 2404:     my $current_column = $Apache::londefdef::table[-1]{'counter_columns'}; 
 2405:     my $data = &Apache::lonxml::endredirection();
 2406: 
 2407:     #  The rowspan array of the table indicates which cells are part of a span.
 2408:     #  n indicates the start of a span set of n rows.
 2409:     #  ^ indicates a cell that continues a span set.
 2410:     #  _ indicates the cell is at the bottom of a span set.
 2411:     #  If this and subsequent cells are part of a rowspan, we must
 2412:     #  push along the row until we find one that is not.
 2413: 
 2414:     while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column]) 
 2415: 	   && ($Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] =~ /[\^\_]/)) {
 2416: 	# Part of a span.
 2417: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1]}, '';
 2418: 	$current_column++;
 2419:     }
 2420:     $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
 2421:    
 2422: 
 2423:     # Get the column and row spans.
 2424:     # Colspan can be done via \multicolumn if I can figure out the data structs.
 2425: 
 2426:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
 2427:     if (!$colspan) {
 2428: 	$colspan = 1;
 2429:     }
 2430: 
 2431:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
 2432:     if (!$rowspan) {
 2433: 	$rowspan = 1;
 2434:     }
 2435: 
 2436: 
 2437: 
 2438:     for (my $c = 0; $c < $colspan; $c++) {
 2439: 	$Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column+$c] = $rowspan;
 2440: 	for (my $i = 1; $i < $rowspan; $i++) {
 2441: 	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '^';
 2442: 	    if ($i == ($rowspan-1)) {
 2443: 		$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '_';
 2444: 	    }
 2445: 	}
 2446:     }
 2447: 
 2448:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2449:     if (defined $TeXwidth) {		
 2450: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2451: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2452: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2453:     } else {
 2454: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2455: 	    my $garbage_data=$data;
 2456: 	    my $fwidth=0;
 2457:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2458: 		my $current_length=&recalc($1);
 2459: 		$current_length=~/(\d+\.?\d*)/;
 2460: 		if ($fwidth<$1) {$fwidth=$1;}
 2461: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2462: 	    }
 2463:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2464: 		my $current_length=$1;
 2465: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2466: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2467: 	    }
 2468: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2469: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2470: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2471: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2472: 	} 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)*/) {
 2473: 	    my $garbage_data=$data;
 2474: 	    my $fwidth=0;
 2475:             while ($garbage_data=~/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)\s*\}/) {
 2476: 		my $current_length=&recalc($1);
 2477: 		$current_length=~/(\d+\.?\d*)/;
 2478: 		if ($fwidth<$1) {$fwidth=$1;}
 2479: 		$garbage_data=~s/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2480: 	    }
 2481:             while ($garbage_data=~/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2482: 		my $current_length=&recalc($1);
 2483: 		$current_length=~/(\d+\.?\d*)/;
 2484: 		if ($fwidth<$1) {$fwidth=$1;}
 2485: 		$garbage_data=~s/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2486: 	    }
 2487: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2488: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2489: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2490: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2491: 	    $data=~s/\\\\\s*$//; 
 2492: 	} else {  
 2493: 	    $data=~s/^\s+(\S.*)/$1/; 
 2494: 	    $data=~s/(.*\S)\s+$/$1/;
 2495: 	    $data=~s/(\s)+/$1/;
 2496: 	    my ($current_length,$min_length)=(0,0);
 2497: 	    if ($data=~/\\vskip/) {
 2498:                 my $newdata=$data;
 2499: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2500: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2501: 		foreach my $elementdata (@newdata) {
 2502: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2503: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2504:                     my @words=split(/ /,$elementdata);
 2505: 		    foreach my $word (@words) {
 2506: 			my $lengthword=2.5*&LATEX_length($word);
 2507: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2508: 		    }
 2509: 		}
 2510: 	    } else {
 2511: 		$current_length=2.5*&LATEX_length($data);
 2512:                     my @words=split(/ /,$data);
 2513: 		    foreach my $word (@words) {
 2514: 			my $lengthword=2*&LATEX_length($word);
 2515: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2516: 		    }
 2517: 	    }
 2518: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2519: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2520: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2521: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2522: 	}        
 2523:     }
 2524:     # Substitute all of the tables nested in this cell in their appropriate places.
 2525: 
 2526: 
 2527:     my $nested_count = $#{$Apache::londefdef::table[-1]{'include'}}; # This one is constant...
 2528:     for (my $in=0; $in<=$nested_count; $in++) {    
 2529: 	my $nested = shift @{$Apache::londefdef::table[-1]{'include'}};
 2530: 	$nested =~ s/\\end\{tabular\}\\strut\\\\/\\end\{tabular\}/;
 2531: 	# $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2532: 	$data =~ s/\\keephidden\{NEW TABLE ENTRY\}/$nested/;
 2533: 
 2534:     }
 2535:     # Should be be killing off the 'include' elements as they're used up?
 2536: 
 2537:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2538: 
 2539: 
 2540: 
 2541: 
 2542:     #  the colspan array will indicate how many columns will be spanned by this
 2543:     #  cell..this requires that counter_columns also be adjusted accordingly
 2544:     #  so that the next bunch of text goes in the right cell.  Note that since
 2545:     #  counter_columns is incremented in the start_td_tex, we adjust by colspan-1.
 2546:     #
 2547: 
 2548:     $Apache::londefdef::table[-1]{'counter_columns'} += $colspan -1;
 2549:     for (my $i = 0; $i < ($colspan -1); $i++) {
 2550: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1] },'';
 2551:     }
 2552:     for (my $r = 0; $r < $rowspan; $r++) {
 2553: 	$Apache::londefdef::table[-1]{'colspan'}[$current_row+$r][$current_column] = $colspan;
 2554: 	# Put empty text in spanned cols.
 2555: 	
 2556:     }
 2557: 
 2558: 
 2559: 
 2560:     return '';
 2561: }
 2562: 
 2563: sub end_td {
 2564:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2565:     my $currentstring = '';
 2566:     if ($target eq 'web' || $target eq 'webgrade') {
 2567: 	$currentstring = $token->[2];     
 2568:     } elsif ($target eq 'tex') {
 2569:         $Apache::londefdef::TD_redirection =0;
 2570: 	&end_td_tex($parstack,$parser,$safeeval);
 2571:     }
 2572:     return $currentstring;
 2573: }
 2574: 
 2575: #-- <th> tag (end tag optional)
 2576: sub start_th {
 2577:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2578:     my $currentstring = '';
 2579:     if ($target eq 'web' || $target eq 'webgrade') {
 2580: 	$currentstring = $token->[4];     
 2581:     } elsif ($target eq 'tex') {
 2582: 	$Apache::londefdef::TD_redirection = 1;
 2583: 	&tagg_check('tr','th',$tagstack,$parstack,$parser,$safeeval);
 2584:     } 
 2585:     return $currentstring;
 2586: }   
 2587:     
 2588: sub tagg_check {
 2589:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2590:     my @ar=@$parstack; 
 2591:     for (my $i=$#ar-1;$i>=0;$i--) {
 2592: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2593: 	    &start_th_tex($parstack,$parser,$safeeval);
 2594: 	    last;
 2595: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2596: 	    splice @ar, $i+1;
 2597: 	    &end_th_tex(\@ar,$parser,$safeeval);
 2598: 	    &start_th_tex($parstack,$parser,$safeeval);
 2599: 	    last;
 2600: 	}
 2601:     }
 2602:     return '';
 2603: }
 2604:  
 2605: sub start_th_tex {
 2606:     my ($parstack,$parser,$safeeval) = @_;
 2607:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2608:     if ($alignchar eq '') {
 2609: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2610:     }
 2611:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2612:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2613:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2614:     if (defined $TeXwidth) {		
 2615: 	my $current_length=&recalc($TeXwidth);
 2616: 	$current_length=~/(\d+\.?\d*)/;
 2617: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2618:     }
 2619:     &Apache::lonxml::startredirection();
 2620:     return '';
 2621: }
 2622: 
 2623: sub end_th_tex {
 2624:     my ($parstack,$parser,$safeeval) = @_;
 2625:     my $current_row = $Apache::londefdef::table[-1]{'row_number'};
 2626:     my $data=&Apache::lonxml::endredirection();
 2627:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2628:     if (defined $TeXwidth) {		
 2629: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2630: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2631: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2632:     } else {
 2633: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2634: 	    my $garbage_data=$data;
 2635: 	    my $fwidth=0;
 2636:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2637: 		my $current_length=&recalc($1);
 2638: 		$current_length=~/(\d+\.?\d*)/;
 2639: 		if ($fwidth<$1) {$fwidth=$1;}
 2640: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2641: 	    }
 2642:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2643: 		my $current_length=$1;
 2644: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2645: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2646: 	    }
 2647: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2648: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2649: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2650: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2651: 	} else {  
 2652: 	    $data=~s/^\s+(\S.*)/$1/; 
 2653: 	    $data=~s/(.*\S)\s+$/$1/;
 2654: 	    $data=~s/(\s)+/$1/;
 2655: 	    my ($current_length,$min_length)=(0,0);
 2656: 	    if ($data=~/\\vskip/) {
 2657:                 my $newdata=$data;
 2658: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2659: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2660: 		foreach my $elementdata (@newdata) {
 2661: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2662: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2663:                     my @words=split(/ /,$elementdata);
 2664: 		    foreach my $word (@words) {
 2665: 			my $lengthword=2.5*&LATEX_length($word);
 2666: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2667: 		    }
 2668: 		}
 2669: 	    } else {
 2670: 		$current_length=2.5*&LATEX_length($data);
 2671:                     my @words=split(/ /,$data);
 2672: 		    foreach my $word (@words) {
 2673: 			my $lengthword=2*&LATEX_length($word);
 2674: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2675: 		    }
 2676: 	    }
 2677: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2678: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2679: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2680: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2681: 	}        
 2682:     }
 2683: 	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
 2684: 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2685: 	}
 2686:     #make data bold
 2687:     $data='\textbf{'.$data.'}';
 2688:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2689:     return'';
 2690: }
 2691: 
 2692: sub end_th {
 2693:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2694:     my $currentstring = &end_p();	# Close any open <p> in the row.
 2695:     if ($target eq 'web' || $target eq 'webgrade') {
 2696: 	$currentstring .= $token->[2];     
 2697:     } elsif ($target eq 'tex') {
 2698:         $Apache::londefdef::TD_redirection =0;
 2699: 	&end_th_tex($parstack,$parser,$safeeval);
 2700:     }
 2701:     return $currentstring;
 2702: }
 2703:      
 2704: #-- <img> tag (end tag forbidden)
 2705: #
 2706: #  Render the <IMG> tag.
 2707: #     <IMG> has the following attributes (in addition to the 
 2708: #     standard HTML ones:
 2709: #      TeXwrap   - Governs how the tex target will try to wrap text around
 2710: #                  horizontally aligned images.
 2711: #      TeXwidth  - The width of the image when rendered for print (mm).
 2712: #      TeXheight - The height of the image when rendered for print (mm)
 2713: #         (Note there seems to also be support for this as a % of page size)
 2714: #      
 2715: sub start_img {
 2716:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
 2717:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
 2718: 					 undef,1);
 2719:     if (! $src && 
 2720: 	($target eq 'web' || $target eq 'webgrade' || $target eq 'tex')
 2721: 	) { 
 2722: 	my $inside = &Apache::lonxml::get_all_text("/img",$parser,$style);
 2723: 	return '';
 2724:     }
 2725:     &Apache::lonxml::extlink($src);
 2726:     my $currentstring = '';
 2727:     my $scaling = .3;
 2728: 
 2729:    # Render unto browsers that which are the browser's...
 2730: 
 2731:     if ($target eq 'web' || $target eq 'webgrade') {
 2732: 	if ($env{'browser.imagesuppress'} ne 'on') {
 2733: 	    my $enc = ('yes' eq 
 2734: 		       lc(&Apache::lonxml::get_param('encrypturl',$parstack,
 2735: 						     $safeeval)));
 2736: 	    $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
 2737: 							 $enc);
 2738: 	} else {
 2739: 	    my $alttag = &Apache::lonxml::get_param('alt',$parstack,$safeeval,
 2740: 						    undef,1);
 2741: 	    if (!$alttag) {
 2742: 		$alttag = &Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 2743: 						   $src);
 2744: 	    }
 2745: 	    $currentstring.='[IMAGE: '.$alttag.']';
 2746: 	}
 2747: 
 2748: 	# and render unto TeX that which is LaTeX
 2749: 
 2750:     } elsif ($target eq 'tex') {
 2751: 	#
 2752: 	#  The alignment will require some superstructure to be put around
 2753: 	#  the \includegraphics stuff.  At present we can only partially
 2754: 	#  simulate the alignments offered by html.
 2755: 	#
 2756: 	#
 2757: 	my $align = lc(&Apache::lonxml::get_param('align', 
 2758: 						  $parstack,
 2759: 						  $safeeval,
 2760: 						  undef,1));
 2761: 	if(!$align) {
 2762: 		$align = "bottom";	# This is html's default so it's ours too.
 2763: 	}
 2764: 	#
 2765: 	&Apache::lonxml::debug("Alignemnt = $align");
 2766: 	#  LaTeX's image/text wrapping is really bad since it wants to
 2767: 	#  make figures float.  
 2768:         #   The user has the optional parameter (applicable only to l/r
 2769: 	# alignment to use the picins/parpic directive to get wrapped text
 2770: 	# this is also imperfect.. that's why we give them a choice...
 2771: 	# so they can't yell at us for our choice.
 2772: 	#
 2773: 	my $latex_rendering = &Apache::lonxml::get_param('TeXwrap',
 2774: 							    $parstack,
 2775: 							    $safeeval,
 2776: 							    undef,0);
 2777: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
 2778: 	if(!$latex_rendering) {
 2779: 		$latex_rendering = "texwrap";
 2780: 	}
 2781: 	# using texwrap inside a table does not work. So, if after all of this,
 2782: 	# texwrap is on, we turn it off if we detect we're in a table:
 2783: 	#
 2784: 	if (($latex_rendering eq 'texwrap') && &is_inside_of($tagstack, "table")) {
 2785: 	    $latex_rendering = 'parpic';
 2786: 	}
 2787: 
 2788: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
 2789: 
 2790: 	#if original gif/jpg/png file exist do following:
 2791: 	my $origsrc=$src;
 2792: 	my ($path,$file) = &get_eps_image($src);
 2793: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 2794: 	&Apache::lonxml::debug("path = $path file = $file src = $src");
 2795: 	if (-e $src) {
 2796: 	    &Apache::lonxml::debug("$src exists");
 2797: 	    my ($height_param,$width_param)=
 2798: 		&image_size($origsrc,0.3,$parstack,$safeeval);
 2799: 	    my $destpath = $path;
 2800: 	    $destpath    =~ s/ /\_/g; # Spaces in path cause LaTex to vomit.
 2801: 	    my $destfile = $file;
 2802: 	    $destfile    =~ s/ /\_/g;
 2803: 	    my $size;
 2804: 	    if ($width_param)  { $size.='width='.$width_param.' mm,'; }
 2805: 	    if ($height_param) { $size.='height='.$height_param.' mm]'; }
 2806: 	    $size='['.$size;
 2807: 	    $size=~s/,$/]/; 
 2808: 	    $currentstring .= '\graphicspath{{'.$destpath.'}}'
 2809: 		.'\includegraphics'.$size.'{'.$destfile.'} ';
 2810: 
 2811: 	    #    If there's an alignment specification we need to honor it here.
 2812: 	    #    For the horizontal alignments, we will also honor the
 2813: 	    #    value of the latex specfication.  The default is parbox,
 2814: 	    #    and that's used for illegal values too.  
 2815: 	    #    
 2816: 	    #    Even though we set a default alignment value, the user
 2817: 	    #    could have given us an illegal value.  In that case we
 2818: 	    #    just use the default alignment of bottom..
 2819: 	    if      ($align eq "top")    {
 2820: 		$currentstring = '\raisebox{-'.$height_param.'mm}{'.$currentstring.'}';
 2821: 	    } elsif (($align eq "center") || ($align eq "middle")) { # Being kind
 2822: 		my $offset = $height_param/2;
 2823: 		$currentstring = '\raisebox{-'.$offset.'mm}{'.$currentstring.'}';
 2824: 	    } elsif ($align eq "left")   { 
 2825: 		if ($latex_rendering eq "parpic") { 
 2826: 		    $currentstring = '\parpic[l]{'.$currentstring.'}';
 2827: 		} else {    	                                 # wrapfig render
 2828: 		    $currentstring = '\begin{wrapfigure}{l}{'.$width_param.'mm}'
 2829: 			.'\scalebox{1.0}{'.$currentstring.'}\end{wrapfigure}';
 2830: 		}
 2831: 	    } elsif ($align eq "right")  {   
 2832: 		if ($latex_rendering eq "parpic") {
 2833: 		    $currentstring = '\parpic[r]{'.$currentstring.'}';
 2834: 		} else {	                                 # wrapfig rendering
 2835: 		    $currentstring = '\begin{wrapfigure}{r}{'.$width_param.'mm}'
 2836: 			.'\scalebox{1.0}{'.$currentstring.'}\end{wrapfigure}';
 2837: 
 2838: 		}
 2839: 	    } else {		# Bottom is also default.
 2840: 		# $currentstring = '\raisebox{'.$height_param.'mm}{'.$currentstring.'}';
 2841: 	    }
 2842: 	} else {
 2843: 	    &Apache::lonxml::debug("$src does not exist");
 2844: 	    #original image file doesn't exist so check the alt attribute
 2845: 	    my $alt = 
 2846: 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
 2847: 	    unless ($alt) {
 2848: 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 2849: 	    }
 2850: 
 2851: 	    if ($alt) { $currentstring .= ' '.$alt.' '; }
 2852: 	}
 2853: 
 2854: 	# And here's where the semi-quote breaks down: allow the user
 2855:         # to edit the beast as well by rendering the problem for edit:
 2856:     } elsif ($target eq 'edit') {
 2857: 	$currentstring .=&Apache::edit::tag_start($target,$token);
 2858: 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
 2859: 	    &Apache::edit::browse('src',undef,'alt').' '.
 2860: 	    &Apache::edit::search('src',undef,'alt').'<br />';
 2861: 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
 2862: 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
 2863: 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
 2864: 	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
 2865: 	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
 2866: 	$currentstring .=&Apache::edit::select_arg('Alignment:','align',
 2867: 						   ['','bottom','middle','top','left','right'],$token,5);
 2868: 	$currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap',
 2869: 						   ['', 'parbox', 'parpic'], $token, 2);
 2870: 	$currentstring .=&Apache::edit::select_arg('Encrypt URL:','encrypturl',
 2871: 						   ['no','yes'], $token, 2);
 2872: 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 2873: 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
 2874: 	my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
 2875: 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
 2876: 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
 2877: 
 2878: 
 2879: 	$currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
 2880: 	if ($width) { $currentstring.=' width="'.$width.'" '; }
 2881: 	if ($height) { $currentstring.=' height="'.$height.'" '; }
 2882: 	$currentstring .= ' />';
 2883:     } elsif ($target eq 'modified') {
 2884: 	my ($osrc,$owidth,$oheight)=
 2885: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 2886: 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
 2887: 					     $safeeval,'src','alt','align',
 2888: 					     'TeXwidth','TeXheight', 'TeXwrap',
 2889: 					     'width','height','encrypturl');
 2890: 	my ($nsrc,$nwidth,$nheight)=
 2891: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 2892: 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
 2893: 	&image_replication($loc);
 2894: 	my ($iwidth,$iheight);
 2895: 	if (-e $loc) {
 2896: 	    my $image = Image::Magick->new;
 2897: 	    $image->Read($loc);
 2898: 	    ($iwidth, $iheight) = ($image->Get('width'),
 2899: 				   $image->Get('height'));
 2900: 	}
 2901: 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
 2902: 	    # changed image or no size specified,
 2903:             # if they didn't explicitly change the 
 2904:             # width or height use the ones from the image
 2905: 	    if ($iwidth && $iheight) {
 2906: 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
 2907: 		    $token->[2]{'width'} = $iwidth;$ctag=1;
 2908: 		}
 2909: 		if ($oheight == $nheight || (!$nwidth && !$nheight)) {
 2910: 		    $token->[2]{'height'}=$iheight;$ctag=1;
 2911: 		}
 2912: 	    }
 2913: 	}
 2914: 	my ($cwidth,$cheight)=($token->[2]{'width'},$token->[2]{'height'});
 2915: 	# if we don't have a width or height
 2916: 	if ($iwidth && $cwidth && !$cheight) {
 2917: 	    $token->[2]{'height'}=int(($cwidth/$iwidth)*$iheight);$ctag=1;
 2918: 	}
 2919: 	if ($iheight && $cheight && !$cwidth) {
 2920: 	    $token->[2]{'width'}=int(($cheight/$iheight)*$iwidth);$ctag=1;
 2921: 	}
 2922: 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
 2923:     }
 2924: 
 2925:     return $currentstring;
 2926: }
 2927: 
 2928: sub end_img {
 2929:     my ($target,$token) = @_;
 2930:     my $currentstring = '';
 2931:     if ($target eq 'web' || $target eq 'webgrade') {
 2932: 	$currentstring = $token->[2];
 2933:     } elsif ($target eq 'tex') {
 2934: 	$currentstring = '';
 2935:     }
 2936:     return $currentstring;
 2937: }
 2938: 
 2939: #-- <applet> tag (end tag required)
 2940: sub start_applet {
 2941:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2942:     
 2943:     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
 2944:     &Apache::lonxml::extlink($code);
 2945:     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
 2946: 					   undef,1);
 2947:     &Apache::lonxml::extlink($archive);
 2948:     my $currentstring = '';
 2949:     if ($target eq 'web' || $target eq 'webgrade') {
 2950: 	if ($env{'browser.appletsuppress'} ne 'on') {
 2951: 	    $currentstring = &Apache::lonenc::encrypt_ref($token,
 2952: 							  {'code'=>$code,
 2953: 							   'archive'=>$archive}
 2954: 							  );
 2955: 	} else {
 2956: 	    my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 2957: 						   $safeeval,undef,1);
 2958: 	    unless ($alttag) {
 2959: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 2960: 						 $code);
 2961: 	    }
 2962: 	    $currentstring='[APPLET: '.$alttag.']';
 2963: 	}
 2964:     } elsif ($target eq 'tex') {
 2965: 	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 2966: 					       $safeeval,undef,1);
 2967: 	unless ($alttag) {
 2968: 	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
 2969: 						undef,1);
 2970: 	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 2971: 					     $code);
 2972: 	}
 2973: 	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
 2974: 	    '.}\end{center}';
 2975:     } 
 2976:     return $currentstring;
 2977: }
 2978: 
 2979: sub end_applet {
 2980:     my ($target,$token) = @_;
 2981:     my $currentstring = '';
 2982:     if ($target eq 'web' || $target eq 'webgrade') {
 2983: 	$currentstring = $token->[2];
 2984:     } elsif ($target eq 'tex') {
 2985:     } 
 2986:     return $currentstring;
 2987: }
 2988: 
 2989: #-- <embed> tag (end tag optional/required)
 2990: sub start_embed {    
 2991:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2992:     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 2993:     &Apache::lonxml::extlink($src);
 2994:     my $currentstring = '';
 2995:     if ($target eq 'web' || $target eq 'webgrade') {
 2996: 	if ($env{'browser.embedsuppress'} ne 'on') {
 2997: 	    $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src});
 2998: 	} else {
 2999: 	    my $alttag=&Apache::lonxml::get_param
 3000: 		('alt',$parstack,$safeeval,undef,1);
 3001: 	    unless ($alttag) {
 3002: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 3003: 	    }
 3004: 	    $currentstring='[EMBED: '.$alttag.']';
 3005: 	}
 3006:     } elsif ($target eq 'tex') {
 3007:     } 
 3008:     return $currentstring;
 3009: }
 3010: 
 3011: sub end_embed {
 3012:     my ($target,$token) = @_;
 3013:     my $currentstring = '';
 3014:     if ($target eq 'web' || $target eq 'webgrade') {
 3015: 	$currentstring = $token->[2];     
 3016:     } elsif ($target eq 'tex') {  
 3017:     } 
 3018:     return $currentstring;
 3019: }
 3020: 
 3021: #-- <param> tag (end tag forbidden)
 3022: sub start_param {
 3023:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3024:     if (&Apache::lonxml::get_param('name',$parstack,
 3025: 				   $safeeval,undef,1)=~/^cabbase$/i) {
 3026: 	my $value=&Apache::lonxml::get_param('value',$parstack,
 3027: 					     $safeeval,undef,1);
 3028: 	&Apache::lonxml::extlink($value);
 3029:     } 
 3030:   
 3031:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3032:     &Apache::lonxml::extlink($src);
 3033:     my $currentstring = '';
 3034:     if ($target eq 'web' || $target eq 'webgrade') {
 3035: 	my %toconvert;
 3036: 	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3037: 	if ($src) { $toconvert{'src'}= $src; }
 3038: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval,
 3039: 					    undef,1);
 3040: 	if ($name=~/^cabbase$/i) {
 3041: 	    $toconvert{'value'}=&Apache::lonxml::get_param('value',$parstack,
 3042: 							   $safeeval,undef,1);
 3043: 	}
 3044: 	$currentstring = &Apache::lonenc::encrypt_ref($token,\%toconvert);
 3045:     } elsif ($target eq 'tex') {
 3046:     } 
 3047:     return $currentstring;
 3048: }
 3049: 
 3050: sub end_param {
 3051:     my ($target,$token) = @_;
 3052:     my $currentstring = '';
 3053:     if ($target eq 'web' || $target eq 'webgrade') {
 3054: 	$currentstring = $token->[2];     
 3055:     } elsif ($target eq 'tex') {
 3056:     } 
 3057:     return $currentstring;
 3058: }
 3059: 
 3060: #-- <allow> tag
 3061: sub start_allow {
 3062:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3063:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 3064:     &Apache::lonxml::extlink($src);
 3065: 
 3066:     if ($target eq 'tex') { &image_replication($src); }
 3067:     my $result;
 3068:     if ($target eq 'edit') {
 3069: 	$result .=&Apache::edit::tag_start($target,$token);
 3070: 	$result .=&Apache::edit::text_arg('File Spec:','src',$token,70);
 3071: 	$result .=&Apache::edit::end_row();#.&Apache::edit::start_spanning_row();
 3072:     } elsif ($target eq 'modified') {
 3073: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
 3074: 						     $safeeval,'src');
 3075: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 3076:     }
 3077:     return $result;
 3078: }
 3079: 
 3080: sub end_allow {
 3081:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3082:     if ( $target eq 'edit') { return (&Apache::edit::end_table()); }
 3083:     return '';
 3084: }
 3085: 
 3086: #-- Frames (end tag required)
 3087: #-- <frameset>
 3088: sub start_frameset {
 3089:     my ($target,$token) = @_;
 3090:     my $currentstring = '';	# Close any pending para.
 3091:     if ($target eq 'web' || $target eq 'webgrade') { 
 3092: 	$currentstring = 
 3093: 	    &Apache::loncommon::start_page($Apache::londefdef::title,
 3094: 					   $Apache::londefdef::head,
 3095: 					   {'add_entries'    => $token->[2],
 3096: 					    'no_title'       => 1,
 3097: 					    'force_register' => 1,
 3098: 					    'frameset'       => 1,});
 3099: 
 3100:     }
 3101:     return $currentstring;
 3102: }
 3103: 
 3104: sub end_frameset {
 3105:     my ($target,$token) = @_;
 3106:     my $currentstring = '';
 3107:     if ($target eq 'web' || $target eq 'webgrade') {
 3108: 	$currentstring = $token->[2];
 3109:     }
 3110:     return $currentstring;
 3111: }
 3112: 
 3113: #-- <xmp> (end tag required)
 3114: sub start_xmp {
 3115:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3116:     my $currentstring = '';
 3117:     if ($target eq 'web' || $target eq 'webgrade') {
 3118: 	$currentstring .= $token->[4];
 3119:     } elsif ($target eq 'tex') {
 3120: 	$currentstring .= '\begin{verbatim}';
 3121:     } 
 3122:     return $currentstring;
 3123: }
 3124: 
 3125: sub end_xmp {
 3126:     my ($target,$token) = @_;
 3127:     my $currentstring = '';
 3128:     if ($target eq 'web' || $target eq 'webgrade') {
 3129: 	$currentstring .= $token->[2];
 3130:     } elsif ($target eq 'tex') {
 3131: 	$currentstring .= '\end{verbatim}';
 3132:     }
 3133:     return $currentstring;
 3134: }
 3135: 
 3136: #-- <pre> (end tag required)
 3137: sub start_pre {
 3138:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3139:     my $currentstring = &end_p();	# close off pending <p>
 3140:     if ($target eq 'web' || $target eq 'webgrade') {
 3141: 	$currentstring .= $token->[4];
 3142:     } elsif ($target eq 'tex') {
 3143: 	$currentstring .= '\begin{verbatim}';
 3144: 	&Apache::lonxml::disable_LaTeX_substitutions();
 3145:     } 
 3146:     return $currentstring;
 3147: }
 3148: 
 3149: sub end_pre {
 3150:     my ($target,$token) = @_;
 3151:     my $currentstring = '';
 3152:     if ($target eq 'web' || $target eq 'webgrade') {
 3153: 	$currentstring .= $token->[2];
 3154:     } elsif ($target eq 'tex') {
 3155: 	$currentstring .= '\end{verbatim}';
 3156: 	&Apache::lonxml::enable_LaTeX_substitutions();
 3157:     }
 3158:     return $currentstring;
 3159: }
 3160: 
 3161: #-- <insert>
 3162: sub start_insert {
 3163:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3164:     my $currentstring = '';
 3165:     if ($target eq 'web' || $target eq 'webgrade') {
 3166: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3167: 	$currentstring .= '<b>'.$display.'</b>';;
 3168:     }
 3169:     return $currentstring;
 3170: }
 3171: 
 3172: sub end_insert {
 3173:     my ($target,$token) = @_;
 3174:     my $currentstring = '';
 3175:     if ($target eq 'web' || $target eq 'webgrade') {
 3176: 	$currentstring .= '';
 3177:     }
 3178:     return $currentstring;
 3179: }
 3180: 
 3181: #-- <externallink>
 3182: sub start_externallink {
 3183:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3184:     my $currentstring = '';
 3185:     if ($target eq 'web' || $target eq 'webgrade') {
 3186: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 3187: 	$currentstring .= '<b>'.$display.'</b>';;
 3188:     }
 3189:     return $currentstring;
 3190: }
 3191: 
 3192: sub end_externallink {
 3193:     my ($target,$token) = @_;
 3194:     my $currentstring = '';
 3195:     if ($target eq 'web' || $target eq 'webgrade') {
 3196: 	$currentstring .= '';
 3197:     }
 3198:     return $currentstring;
 3199: }
 3200: 
 3201: #-- <blankspace heigth="">
 3202: sub start_blankspace {
 3203:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3204:     my $currentstring = &end_p();	# closes off any unclosed <p>
 3205:     if ($target eq 'tex') {
 3206: 	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
 3207: 	$currentstring .= '\vskip '.$howmuch.' ';
 3208:     }
 3209:     return $currentstring;
 3210: }
 3211: 
 3212: sub end_blankspace {
 3213:     my ($target,$token) = @_;
 3214:     my $currentstring = '';
 3215:     if ($target eq 'tex') {
 3216: 	$currentstring .= '';
 3217:     }
 3218:     return $currentstring;
 3219: }
 3220: 
 3221: #-- <abbr> tag (end tag required)
 3222: sub start_abbr {
 3223:     my ($target,$token) = @_;
 3224:     my $currentstring = '';
 3225:     if ($target eq 'web' || $target eq 'webgrade') {
 3226: 	$currentstring = $token->[4];     
 3227:     } 
 3228:     return $currentstring;
 3229: }
 3230: 
 3231: sub end_abbr {
 3232:     my ($target,$token) = @_;
 3233:     my $currentstring = '';
 3234:     if ($target eq 'web' || $target eq 'webgrade') {
 3235: 	$currentstring = $token->[2];    
 3236:     } 
 3237:     return $currentstring;
 3238: }
 3239: 
 3240: #-- <acronym> tag (end tag required)
 3241: sub start_acronym {
 3242:     my ($target,$token) = @_;
 3243:     my $currentstring = '';
 3244:     if ($target eq 'web' || $target eq 'webgrade') {
 3245: 	$currentstring = $token->[4];     
 3246:     } 
 3247:     return $currentstring;
 3248: }
 3249: 
 3250: sub end_acronym {
 3251:     my ($target,$token) = @_;
 3252:     my $currentstring = '';
 3253:     if ($target eq 'web' || $target eq 'webgrade') {
 3254: 	$currentstring = $token->[2];    
 3255:     } 
 3256:     return $currentstring;
 3257: }
 3258: 
 3259: #-- <area> tag (end tag forbidden)
 3260: sub start_area {
 3261:     my ($target,$token) = @_;
 3262:     my $currentstring = '';
 3263:     if ($target eq 'web' || $target eq 'webgrade') {
 3264: 	$currentstring = $token->[4];     
 3265:     } 
 3266:     return $currentstring;
 3267: }
 3268: 
 3269: sub end_area {
 3270:     my ($target,$token) = @_;
 3271:     my $currentstring = '';
 3272:     if ($target eq 'web' || $target eq 'webgrade') {
 3273: 	$currentstring = $token->[2];    
 3274:     } 
 3275:     return $currentstring;
 3276: }
 3277: 
 3278: #-- <base> tag (end tag forbidden)
 3279: sub start_base {
 3280:     my ($target,$token) = @_;
 3281:     my $currentstring = '';
 3282:     if ($target eq 'web' || $target eq 'webgrade') {
 3283: 	$currentstring = $token->[4];     
 3284:     }
 3285:     return $currentstring;
 3286: }
 3287: 
 3288: sub end_base {
 3289:     my ($target,$token) = @_;
 3290:     my $currentstring = '';
 3291:     if ($target eq 'web' || $target eq 'webgrade') {
 3292: 	$currentstring = $token->[2];    
 3293:     } 
 3294:     return $currentstring;
 3295: }
 3296: 
 3297: #-- <bdo> tag (end tag required)
 3298: sub start_bdo {
 3299:     my ($target,$token) = @_;
 3300:     my $currentstring = '';
 3301:     if ($target eq 'web' || $target eq 'webgrade') {
 3302: 	$currentstring = $token->[4];     
 3303:     } 
 3304:     return $currentstring;
 3305: }
 3306: 
 3307: sub end_bdo {
 3308:     my ($target,$token) = @_;
 3309:     my $currentstring = '';
 3310:     if ($target eq 'web' || $target eq 'webgrade') {
 3311: 	$currentstring = $token->[2];    
 3312:     } 
 3313:     return $currentstring;
 3314: }
 3315: 
 3316: #-- <bgsound> tag (end tag optional)
 3317: sub start_bgsound {
 3318:     my ($target,$token) = @_;
 3319:     my $currentstring = '';
 3320:     if ($target eq 'web' || $target eq 'webgrade') {
 3321: 	$currentstring = $token->[4];     
 3322:     } 
 3323:     return $currentstring;
 3324: }
 3325: 
 3326: sub end_bgsound {
 3327:     my ($target,$token) = @_;
 3328:     my $currentstring = '';
 3329:     if ($target eq 'web' || $target eq 'webgrade') {
 3330: 	$currentstring = $token->[2];    
 3331:     } 
 3332:     return $currentstring;
 3333: }
 3334: 
 3335: #-- <blink> tag (end tag required)
 3336: sub start_blink {
 3337:     my ($target,$token) = @_;
 3338:     my $currentstring = '';
 3339:     if ($target eq 'web' || $target eq 'webgrade') {
 3340: 	$currentstring = $token->[4];     
 3341:     } 
 3342:     return $currentstring;
 3343: }
 3344: 
 3345: sub end_blink {
 3346:     my ($target,$token) = @_;
 3347:     my $currentstring = '';
 3348:     if ($target eq 'web' || $target eq 'webgrade') {
 3349: 	$currentstring = $token->[2];    
 3350:     } 
 3351:     return $currentstring;
 3352: }
 3353: 
 3354: #-- <blockquote> tag (end tag required)
 3355: sub start_blockquote {
 3356:     my ($target,$token) = @_;
 3357:     my $currentstring = &end_p();	# Close any unclosed <p>
 3358:     if ($target eq 'web' || $target eq 'webgrade') {
 3359: 	$currentstring .= $token->[4];     
 3360:     } 
 3361:     return $currentstring;
 3362: }
 3363: 
 3364: sub end_blockquote {
 3365:     my ($target,$token) = @_;
 3366:     my $currentstring = '';
 3367:     if ($target eq 'web' || $target eq 'webgrade') {
 3368: 	$currentstring = $token->[2];    
 3369:     } 
 3370:     return $currentstring;
 3371: }
 3372: 
 3373: #-- <button> tag (end tag required)
 3374: sub start_button {
 3375:     my ($target,$token) = @_;
 3376:     my $currentstring = '';
 3377:     if ($target eq 'web' || $target eq 'webgrade') {
 3378: 	$currentstring = $token->[4];     
 3379:     } 
 3380:     return $currentstring;
 3381: }
 3382: 
 3383: sub end_button {
 3384:     my ($target,$token) = @_;
 3385:     my $currentstring = '';
 3386:     if ($target eq 'web' || $target eq 'webgrade') {
 3387: 	$currentstring = $token->[2];    
 3388:     } 
 3389:     return $currentstring;
 3390: }
 3391: 
 3392: #-- <caption> tag (end tag required)
 3393: sub start_caption {
 3394:     my ($target,$token) = @_;
 3395:     my $currentstring = '';
 3396:     if ($target eq 'web' || $target eq 'webgrade') {
 3397: 	$currentstring = $token->[4];     
 3398:     } 
 3399:     return $currentstring;
 3400: }
 3401: 
 3402: sub end_caption {
 3403:     my ($target,$token) = @_;
 3404:     my $currentstring = '';
 3405:     if ($target eq 'web' || $target eq 'webgrade') {
 3406: 	$currentstring = $token->[2];    
 3407:     } 
 3408:     return $currentstring;
 3409: }
 3410: 
 3411: #-- <col> tag (end tag forbdden)
 3412: sub start_col {
 3413:     my ($target,$token) = @_;
 3414:     my $currentstring = '';
 3415:     if ($target eq 'web' || $target eq 'webgrade') {
 3416: 	$currentstring = $token->[4];     
 3417:     } 
 3418:     return $currentstring;
 3419: }
 3420: 
 3421: sub end_col {
 3422:     my ($target,$token) = @_;
 3423:     my $currentstring = '';
 3424:     if ($target eq 'web' || $target eq 'webgrade') {
 3425: 	$currentstring = $token->[2];    
 3426:     } 
 3427:     return $currentstring;
 3428: }
 3429: 
 3430: #-- <colgroup> tag (end tag optional)
 3431: sub start_colgroup {
 3432:     my ($target,$token) = @_;
 3433:     my $currentstring = '';
 3434:     if ($target eq 'web' || $target eq 'webgrade') {
 3435: 	$currentstring = $token->[4];     
 3436:     } 
 3437:     return $currentstring;
 3438: }
 3439: 
 3440: sub end_colgroup {
 3441:     my ($target,$token) = @_;
 3442:     my $currentstring = '';
 3443:     if ($target eq 'web' || $target eq 'webgrade') {
 3444: 	$currentstring = $token->[2];    
 3445:     } 
 3446:     return $currentstring;
 3447: }
 3448: 
 3449: #-- <del> tag (end tag required)
 3450: sub start_del {
 3451:     my ($target,$token) = @_;
 3452:     my $currentstring = '';
 3453:     if ($target eq 'web' || $target eq 'webgrade') {
 3454: 	$currentstring = $token->[4];     
 3455:     } 
 3456:     return $currentstring;
 3457: }
 3458: 
 3459: sub end_del {
 3460:     my ($target,$token) = @_;
 3461:     my $currentstring = '';
 3462:     if ($target eq 'web' || $target eq 'webgrade') {
 3463: 	$currentstring = $token->[2];    
 3464:     } 
 3465:     return $currentstring;
 3466: }
 3467: 
 3468: #-- <fieldset> tag (end tag required)
 3469: sub start_fieldset {
 3470:     my ($target,$token) = @_;
 3471:     my $currentstring = '';
 3472:     if ($target eq 'web' || $target eq 'webgrade') {
 3473: 	$currentstring = $token->[4];     
 3474:     } 
 3475:     return $currentstring;
 3476: }
 3477: 
 3478: sub end_fieldset {
 3479:     my ($target,$token) = @_;
 3480:     my $currentstring = '';
 3481:     if ($target eq 'web' || $target eq 'webgrade') {
 3482: 	$currentstring = $token->[2];    
 3483:     } 
 3484:     return $currentstring;
 3485: }
 3486: 
 3487: #-- <frame> tag (end tag forbidden)
 3488: sub start_frame {
 3489:     my ($target,$token) = @_;
 3490:     my $currentstring = '';
 3491:     if ($target eq 'web' || $target eq 'webgrade') {
 3492: 	$currentstring = $token->[4];     
 3493:     } 
 3494:     return $currentstring;
 3495: }
 3496: 
 3497: sub end_frame {
 3498:     my ($target,$token) = @_;
 3499:     my $currentstring = '';
 3500:     if ($target eq 'web' || $target eq 'webgrade') {
 3501: 	$currentstring = $token->[2];    
 3502:     } 
 3503:     return $currentstring;
 3504: }
 3505: 
 3506: #-- <iframe> tag (end tag required)
 3507: sub start_iframe {
 3508:     my ($target,$token) = @_;
 3509:     my $currentstring = '';
 3510:     if ($target eq 'web' || $target eq 'webgrade') {
 3511: 	$currentstring = $token->[4];     
 3512:     } 
 3513:     return $currentstring;
 3514: }
 3515: 
 3516: sub end_iframe {
 3517:     my ($target,$token) = @_;
 3518:     my $currentstring = '';
 3519:     if ($target eq 'web' || $target eq 'webgrade') {
 3520: 	$currentstring = $token->[2];    
 3521:     } 
 3522:     return $currentstring;
 3523: }
 3524: 
 3525: #-- <ins> tag (end tag required)
 3526: sub start_ins {
 3527:     my ($target,$token) = @_;
 3528:     my $currentstring = '';
 3529:     if ($target eq 'web' || $target eq 'webgrade') {
 3530: 	$currentstring = $token->[4];     
 3531:     } 
 3532:     return $currentstring;
 3533: }
 3534: 
 3535: sub end_ins {
 3536:     my ($target,$token) = @_;
 3537:     my $currentstring = '';
 3538:     if ($target eq 'web' || $target eq 'webgrade') {
 3539: 	$currentstring = $token->[2];    
 3540:     } 
 3541:     return $currentstring;
 3542: }
 3543: 
 3544: #-- <isindex> tag (end tag forbidden)
 3545: sub start_isindex {
 3546:     my ($target,$token) = @_;
 3547:     my $currentstring = '';
 3548:     if ($target eq 'web' || $target eq 'webgrade') {
 3549: 	$currentstring = $token->[4];     
 3550:     } 
 3551:     return $currentstring;
 3552: }
 3553: 
 3554: sub end_isindex {
 3555:     my ($target,$token) = @_;
 3556:     my $currentstring = '';
 3557:     if ($target eq 'web' || $target eq 'webgrade') {
 3558: 	$currentstring = $token->[2];    
 3559:     } 
 3560:     return $currentstring;
 3561: }
 3562: 
 3563: #-- <keygen> tag (end tag forbidden)
 3564: sub start_keygen {
 3565:     my ($target,$token) = @_;
 3566:     my $currentstring = '';
 3567:     if ($target eq 'web' || $target eq 'webgrade') {
 3568: 	$currentstring = $token->[4];     
 3569:     } 
 3570:     return $currentstring;
 3571: }
 3572: 
 3573: sub end_keygen {
 3574:     my ($target,$token) = @_;
 3575:     my $currentstring = '';
 3576:     if ($target eq 'web' || $target eq 'webgrade') {
 3577: 	$currentstring = $token->[2];    
 3578:     } 
 3579:     return $currentstring;
 3580: }
 3581: 
 3582: #-- <label> tag
 3583: sub start_label {
 3584:     my ($target,$token) = @_;
 3585:     my $currentstring = '';
 3586:     if ($target eq 'web' || $target eq 'webgrade') {
 3587: 	$currentstring = $token->[4];     
 3588:     } 
 3589:     return $currentstring;
 3590: }
 3591: 
 3592: sub end_label {
 3593:     my ($target,$token) = @_;
 3594:     my $currentstring = '';
 3595:     if ($target eq 'web' || $target eq 'webgrade') {
 3596: 	$currentstring = $token->[2];    
 3597:     } 
 3598:     return $currentstring;
 3599: }
 3600: 
 3601: #-- <layer> tag (end tag required)
 3602: sub start_layer {
 3603:     my ($target,$token) = @_;
 3604:     my $currentstring = '';
 3605:     if ($target eq 'web' || $target eq 'webgrade') {
 3606: 	$currentstring = $token->[4];     
 3607:     } 
 3608:     return $currentstring;
 3609: }
 3610: 
 3611: sub end_layer {
 3612:     my ($target,$token) = @_;
 3613:     my $currentstring = '';
 3614:     if ($target eq 'web' || $target eq 'webgrade') {
 3615: 	$currentstring = $token->[2];    
 3616:     } 
 3617:     return $currentstring;
 3618: }
 3619: 
 3620: #-- <legend> tag (end tag required)
 3621: sub start_legend {
 3622:     my ($target,$token) = @_;
 3623:     my $currentstring = '';
 3624:     if ($target eq 'web' || $target eq 'webgrade') {
 3625: 	$currentstring = $token->[4];     
 3626:     } 
 3627:     return $currentstring;
 3628: }
 3629: 
 3630: sub end_legend {
 3631:     my ($target,$token) = @_;
 3632:     my $currentstring = '';
 3633:     if ($target eq 'web' || $target eq 'webgrade') {
 3634: 	$currentstring = $token->[2];    
 3635:     } 
 3636:     return $currentstring;
 3637: }
 3638: 
 3639: #-- <link> tag (end tag forbidden)
 3640: sub start_link {
 3641:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 3642:     my $currentstring = '';
 3643:     if ($target eq 'web' || $target eq 'webgrade') {
 3644: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 3645: 					    undef,1);
 3646: 	&Apache::lonxml::extlink($href);
 3647: 	$currentstring = $token->[4];     
 3648:     } 
 3649:     return $currentstring;
 3650: }
 3651: 
 3652: sub end_link {
 3653:     my ($target,$token) = @_;
 3654:     my $currentstring = '';
 3655:     if ($target eq 'web' || $target eq 'webgrade') {
 3656: 	$currentstring = $token->[2];    
 3657:     } 
 3658:     return $currentstring;
 3659: }
 3660: 
 3661: #-- <marquee> tag (end tag optional)
 3662: sub start_marquee {
 3663:     my ($target,$token) = @_;
 3664:     my $currentstring = '';
 3665:     if ($target eq 'web' || $target eq 'webgrade') {
 3666: 	$currentstring = $token->[4];     
 3667:     } 
 3668:     return $currentstring;
 3669: }
 3670: 
 3671: sub end_marquee {
 3672:     my ($target,$token) = @_;
 3673:     my $currentstring = '';
 3674:     if ($target eq 'web' || $target eq 'webgrade') {
 3675: 	$currentstring = $token->[2];    
 3676:     } 
 3677:     return $currentstring;
 3678: }
 3679: 
 3680: #-- <multicol> tag (end tag required)
 3681: sub start_multicol {
 3682:     my ($target,$token) = @_;
 3683:     my $currentstring = &end_p();	# Close any pending <p>
 3684:     if ($target eq 'web' || $target eq 'webgrade') {
 3685: 	$currentstring .= $token->[4];     
 3686:     } 
 3687:     return $currentstring;
 3688: }
 3689: 
 3690: sub end_multicol {
 3691:     my ($target,$token) = @_;
 3692:     my $currentstring = '';
 3693:     if ($target eq 'web' || $target eq 'webgrade') {
 3694: 	$currentstring = $token->[2];    
 3695:     } 
 3696:     return $currentstring;
 3697: }
 3698: 
 3699: #-- <nobr> tag (end tag required)
 3700: sub start_nobr {
 3701:     my ($target,$token) = @_;
 3702:     my $currentstring = '';
 3703:     if ($target eq 'web' || $target eq 'webgrade') {
 3704: 	$currentstring = $token->[4];     
 3705:     }  elsif ($target eq 'tex') {
 3706: 	$currentstring='\mbox{';
 3707:     }
 3708:     return $currentstring;
 3709: }
 3710: 
 3711: sub end_nobr {
 3712:     my ($target,$token) = @_;
 3713:     my $currentstring = '';
 3714:     if ($target eq 'web' || $target eq 'webgrade') {
 3715: 	$currentstring = $token->[2];    
 3716:     }   elsif ($target eq 'tex') {
 3717: 	$currentstring='}';
 3718:     }
 3719:     return $currentstring;
 3720: }
 3721: 
 3722: #-- <noembed> tag (end tag required)
 3723: sub start_noembed {
 3724:     my ($target,$token) = @_;
 3725:     my $currentstring = '';
 3726:     if ($target eq 'web' || $target eq 'webgrade') {
 3727: 	$currentstring = $token->[4];     
 3728:     } 
 3729:     return $currentstring;
 3730: }
 3731: 
 3732: sub end_noembed {
 3733:     my ($target,$token) = @_;
 3734:     my $currentstring = '';
 3735:     if ($target eq 'web' || $target eq 'webgrade') {
 3736: 	$currentstring = $token->[2];    
 3737:     } 
 3738:     return $currentstring;
 3739: }
 3740: 
 3741: #-- <noframes> tag (end tag required)
 3742: sub start_noframes {
 3743:     my ($target,$token) = @_;
 3744:     my $currentstring = '';
 3745:     if ($target eq 'web' || $target eq 'webgrade') {
 3746: 	$currentstring = $token->[4];     
 3747:     } 
 3748:     return $currentstring;
 3749: }
 3750: 
 3751: sub end_noframes {
 3752:     my ($target,$token) = @_;
 3753:     my $currentstring = '';
 3754:     if ($target eq 'web' || $target eq 'webgrade') {
 3755: 	$currentstring = $token->[2];    
 3756:     } 
 3757:     return $currentstring;
 3758: }
 3759: 
 3760: #-- <nolayer> tag (end tag required)
 3761: sub start_nolayer {
 3762:     my ($target,$token) = @_;
 3763:     my $currentstring = '';
 3764:     if ($target eq 'web' || $target eq 'webgrade') {
 3765: 	$currentstring = $token->[4];     
 3766:     } 
 3767:     return $currentstring;
 3768: }
 3769: 
 3770: sub end_nolayer {
 3771:     my ($target,$token) = @_;
 3772:     my $currentstring = '';
 3773:     if ($target eq 'web' || $target eq 'webgrade') {
 3774: 	$currentstring = $token->[2];    
 3775:     } 
 3776:     return $currentstring;
 3777: }
 3778: 
 3779: #-- <noscript> tag (end tag required)
 3780: sub start_noscript {
 3781:     my ($target,$token) = @_;
 3782:     my $currentstring = '';
 3783:     if ($target eq 'web' || $target eq 'webgrade') {
 3784: 	$currentstring = $token->[4];     
 3785:     } 
 3786:     return $currentstring;
 3787: }
 3788: 
 3789: sub end_noscript {
 3790:     my ($target,$token) = @_;
 3791:     my $currentstring = '';
 3792:     if ($target eq 'web' || $target eq 'webgrade') {
 3793: 	$currentstring = $token->[2];    
 3794:     } 
 3795:     return $currentstring;
 3796: }
 3797: 
 3798: #-- <object> tag (end tag required)
 3799: sub start_object {
 3800:     my ($target,$token) = @_;
 3801:     my $currentstring = '';
 3802:     if ($target eq 'web' || $target eq 'webgrade') {
 3803: 	$currentstring = $token->[4];     
 3804:     } 
 3805:     return $currentstring;
 3806: }
 3807: 
 3808: sub end_object {
 3809:     my ($target,$token) = @_;
 3810:     my $currentstring = '';
 3811:     if ($target eq 'web' || $target eq 'webgrade') {
 3812: 	$currentstring = $token->[2];    
 3813:     } 
 3814:     return $currentstring;
 3815: }
 3816: 
 3817: #-- <optgroup> tag (end tag required)
 3818: sub start_optgroup {
 3819:     my ($target,$token) = @_;
 3820:     my $currentstring = '';
 3821:     if ($target eq 'web' || $target eq 'webgrade') {
 3822: 	$currentstring = $token->[4];     
 3823:     } 
 3824:     return $currentstring;
 3825: }
 3826: 
 3827: sub end_optgroup {
 3828:     my ($target,$token) = @_;
 3829:     my $currentstring = '';
 3830:     if ($target eq 'web' || $target eq 'webgrade') {
 3831: 	$currentstring = $token->[2];    
 3832:     } 
 3833:     return $currentstring;
 3834: }
 3835: 
 3836: #-- <samp> tag (end tag required)
 3837: sub start_samp {
 3838:     my ($target,$token) = @_;
 3839:     my $currentstring = '';
 3840:     if ($target eq 'web' || $target eq 'webgrade') {
 3841: 	$currentstring = $token->[4];     
 3842:     } elsif ($target eq 'tex') {
 3843: 	$currentstring='\texttt{';
 3844:     }
 3845:     return $currentstring;
 3846: }
 3847: 
 3848: sub end_samp {
 3849:     my ($target,$token) = @_;
 3850:     my $currentstring = '';
 3851:     if ($target eq 'web' || $target eq 'webgrade') {
 3852: 	$currentstring = $token->[2];    
 3853:     } elsif ($target eq 'tex') {
 3854: 	$currentstring='}';
 3855:     }
 3856:     return $currentstring;
 3857: }
 3858: 
 3859: #-- <server> tag
 3860: sub start_server {
 3861:     my ($target,$token) = @_;
 3862:     my $currentstring = '';
 3863:     if ($target eq 'web' || $target eq 'webgrade') {
 3864: 	$currentstring = $token->[4];     
 3865:     } 
 3866:     return $currentstring;
 3867: }
 3868: 
 3869: sub end_server {
 3870:     my ($target,$token) = @_;
 3871:     my $currentstring = '';
 3872:     if ($target eq 'web' || $target eq 'webgrade') {
 3873: 	$currentstring = $token->[2];    
 3874:     } 
 3875:     return $currentstring;
 3876: }
 3877: 
 3878: #-- <spacer> tag (end tag forbidden)
 3879: sub start_spacer {
 3880:     my ($target,$token) = @_;
 3881:     my $currentstring = &end_p();	# Close off any open <p> tag.
 3882:     if ($target eq 'web' || $target eq 'webgrade') {
 3883: 	$currentstring .= $token->[4];     
 3884:     } 
 3885:     return $currentstring;
 3886: }
 3887: 
 3888: sub end_spacer {
 3889:     my ($target,$token) = @_;
 3890:     my $currentstring = '';
 3891:     if ($target eq 'web' || $target eq 'webgrade') {
 3892: 	$currentstring = $token->[2];    
 3893:     } 
 3894:     return $currentstring;
 3895: }
 3896: 
 3897: #-- <span> tag (end tag required)
 3898: sub start_span {
 3899:     my ($target,$token) = @_;
 3900:     my $currentstring = '';
 3901:     if ($target eq 'web' || $target eq 'webgrade') {
 3902: 	$currentstring = $token->[4];     
 3903:     } 
 3904:     return $currentstring;
 3905: }
 3906: 
 3907: sub end_span {
 3908:     my ($target,$token) = @_;
 3909:     my $currentstring = '';
 3910:     if ($target eq 'web' || $target eq 'webgrade') {
 3911: 	$currentstring = $token->[2];    
 3912:     } 
 3913:     return $currentstring;
 3914: }
 3915: 
 3916: #-- <tbody> tag (end tag optional)
 3917: sub start_tbody {
 3918:     my ($target,$token) = @_;
 3919:     my $currentstring = '';
 3920:     if ($target eq 'web' || $target eq 'webgrade') {
 3921: 	$currentstring = $token->[4];     
 3922:     } 
 3923:     return $currentstring;
 3924: }
 3925: 
 3926: sub end_tbody {
 3927:     my ($target,$token) = @_;
 3928:     my $currentstring = '';
 3929:     if ($target eq 'web' || $target eq 'webgrade') {
 3930: 	$currentstring = $token->[2];    
 3931:     } 
 3932:     return $currentstring;
 3933: }
 3934: 
 3935: #-- <tfoot> tag (end tag optional)
 3936: sub start_tfoot {
 3937:     my ($target,$token) = @_;
 3938:     my $currentstring = '';
 3939:     if ($target eq 'web' || $target eq 'webgrade') {
 3940: 	$currentstring = $token->[4];     
 3941:     } 
 3942:     return $currentstring;
 3943: }
 3944: 
 3945: sub end_tfoot {
 3946:     my ($target,$token) = @_;
 3947:     my $currentstring = '';
 3948:     if ($target eq 'web' || $target eq 'webgrade') {
 3949: 	$currentstring = $token->[2];    
 3950:     } 
 3951:     return $currentstring;
 3952: }
 3953: 
 3954: #-- <thead> tag (end tag optional)
 3955: sub start_thead {
 3956:     my ($target,$token) = @_;
 3957:     my $currentstring = '';
 3958:     if ($target eq 'web' || $target eq 'webgrade') {
 3959: 	$currentstring = $token->[4];     
 3960:     } 
 3961:     return $currentstring;
 3962: }
 3963: 
 3964: sub end_thead {
 3965:     my ($target,$token) = @_;
 3966:     my $currentstring = '';
 3967:     if ($target eq 'web' || $target eq 'webgrade') {
 3968: 	$currentstring = $token->[2];    
 3969:     } 
 3970:     return $currentstring;
 3971: }
 3972: 
 3973: #-- <var> tag
 3974: sub start_var {
 3975:     my ($target,$token) = @_;
 3976:     my $currentstring = '';
 3977:     if ($target eq 'web' || $target eq 'webgrade') {
 3978: 	$currentstring = $token->[4];     
 3979:     } elsif ($target eq 'tex') {
 3980: 	$currentstring = '\textit{'; 
 3981:     }
 3982:     return $currentstring;
 3983: }
 3984: 
 3985: sub end_var {
 3986:     my ($target,$token) = @_;
 3987:     my $currentstring = '';
 3988:     if ($target eq 'web' || $target eq 'webgrade') {
 3989: 	$currentstring = $token->[2];
 3990:     } elsif ($target eq 'tex') {
 3991: 	$currentstring = '}'; 
 3992:     } 
 3993:     return $currentstring;
 3994: }
 3995: 
 3996: #-- <wbr> tag (end tag forbidden)
 3997: sub start_wbr {
 3998:     my ($target,$token) = @_;
 3999:     my $currentstring = '';
 4000:     if ($target eq 'web' || $target eq 'webgrade') {
 4001: 	$currentstring = $token->[4];     
 4002:     } 
 4003:     return $currentstring;
 4004: }
 4005: 
 4006: sub end_wbr {
 4007:     my ($target,$token) = @_;
 4008:     my $currentstring = '';
 4009:     if ($target eq 'web' || $target eq 'webgrade') {
 4010: 	$currentstring = $token->[2];    
 4011:     } 
 4012:     return $currentstring;
 4013: }
 4014: 
 4015: #-- <hideweboutput> tag
 4016: sub start_hideweboutput {
 4017:     my ($target,$token) = @_;
 4018:     if ($target eq 'web' || $target eq 'webgrade') {
 4019: 	&Apache::lonxml::startredirection();     
 4020:     } 
 4021:     return '';
 4022: }
 4023: 
 4024: sub end_hideweboutput {
 4025:     my ($target,$token) = @_;
 4026:     my $currentstring = '';
 4027:     if ($target eq 'web' || $target eq 'webgrade') {
 4028: 	$currentstring = &Apache::lonxml::endredirection();    
 4029:     } 
 4030:     return '';
 4031: }
 4032: 
 4033: 
 4034: sub image_replication {
 4035:     my $src = shift;
 4036:     if (not -e $src) { &Apache::lonnet::repcopy($src); }
 4037:     #replicates eps or ps 
 4038:     my $epssrc = my $pssrc = $src;
 4039:     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
 4040:     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
 4041:     if (not -e $epssrc && not -e $pssrc) {
 4042: 	my $result=&Apache::lonnet::repcopy($epssrc);
 4043: 	if ($result ne 'ok') { &Apache::lonnet::repcopy($pssrc); }
 4044:     }
 4045:     return '';
 4046: }
 4047: #
 4048: #   Get correct sizing parameter for an image given
 4049: #   it's initial ht. and wid.  This allows sizing of
 4050: #   images that are generated on-the-fly (e.g. gnuplot)
 4051: #   as well as serving as a utility for image_size.
 4052: # 
 4053: #  Parameter:
 4054: #        height_param
 4055: #        width_param    - Initial picture dimensions.
 4056: #        scaling        - A scale factor.
 4057: #        parstack,      - the current stack of tag attributes 
 4058: #                         from the xml parser
 4059: #        safeeval,      - pointer to the safespace
 4060: #        depth,         - from what level in the stack to look for attributes
 4061: #                         (assumes -1 if unspecified)
 4062: #        cis            - look for attrubutes case insensitively
 4063: #                         (assumes false)
 4064: #
 4065: # Returns:
 4066: #   height, width   - new dimensions.
 4067: #
 4068: sub resize_image {
 4069:     my ($height_param, $width_param, $scaling,
 4070: 	$parstack, $safeeval, $depth, $cis) = @_;
 4071: 
 4072:     # First apply the scaling...
 4073: 
 4074:     $height_param = $height_param * $scaling;
 4075:     $width_param  = $width_param  * $scaling;
 4076: 
 4077:     #do we have any specified LaTeX size of the picture?
 4078:     my $toget='TeXwidth'; 
 4079:     if ($cis) { 
 4080: 	$toget=lc($toget); 
 4081:     }
 4082:     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
 4083: 					      $safeeval,$depth,$cis);
 4084:     $toget='TeXheight'; if ($cis) { $toget=lc($toget); }
 4085:     my $TeXheight = &Apache::lonxml::get_param($toget,$parstack,
 4086: 					       $safeeval,$depth,$cis);
 4087:     #do we have any specified web size of the picture?
 4088:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
 4089: 					   $depth,1);
 4090:     if ($TeXwidth) { 
 4091: 	my $old_width_param=$width_param;
 4092: 	if ($TeXwidth=~/(\d+)\s*\%/) {
 4093: 	    $width_param = $1*$env{'form.textwidth'}/100;
 4094: 	} else { 
 4095: 	    $width_param = $TeXwidth;
 4096: 	}
 4097: 	if ($TeXheight) {
 4098: 	    $height_param = $TeXheight;
 4099: 	} elsif ($old_width_param) {
 4100: 	    $height_param=$TeXwidth/$old_width_param*$height_param;
 4101: 	}
 4102:     } elsif ($TeXheight) {
 4103: 	$height_param = $TeXheight;
 4104: 	if ($height_param) {
 4105: 	    $width_param  = $TeXheight/$height_param*$width_param;
 4106: 	}
 4107:     } elsif ($width) {
 4108: 	my $old_width_param=$width_param;
 4109: 	$width_param = $width*$scaling;
 4110: 	if ($old_width_param) {
 4111: 	    $height_param=$width_param/$old_width_param*$height_param;
 4112: 	}
 4113:     }
 4114:     if ($width_param > $env{'form.textwidth'}) {
 4115:         my $old_width_param=$width_param;
 4116: 	$width_param =0.95*$env{'form.textwidth'};
 4117: 	if ($old_width_param) {
 4118: 	    $height_param=$width_param/$old_width_param*$height_param;
 4119: 	}
 4120:     }
 4121: 
 4122:     return ($height_param, $width_param);
 4123: }
 4124: 
 4125: sub image_size {
 4126:     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
 4127: 
 4128:     #size of image from gif/jpg/jpeg/png 
 4129:     my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4130:     if (-e $ressrc) {
 4131: 	$src = $ressrc;
 4132:     }
 4133:     my $image = Image::Magick->new;
 4134:     my $current_figure = $image->Read($src);
 4135:     my $width_param = $image->Get('width');
 4136:     my $height_param = $image->Get('height');
 4137:     &Apache::lonxml::debug("Image magick says: $src :  Height = $height_param width = $width_param");
 4138:     undef($image);
 4139: 
 4140:     ($height_param, $width_param) = &resize_image($height_param, $width_param,
 4141: 						  $scaling, $parstack, $safeeval, 
 4142: 						  $depth, $cis);
 4143: 
 4144:     return ($height_param, $width_param);
 4145: }
 4146: 
 4147: sub image_width {
 4148:     my ($height, $width) = &image_size(@_);
 4149:     return $width;
 4150: }
 4151: #  Not yet 100% sure this is correct in all circumstances..
 4152: #  due to my uncertainty about mods to image_size.
 4153: #
 4154: sub image_height {
 4155:     my ($height, $width) = &image_size(@_);
 4156:     return $height;
 4157: }
 4158: 
 4159: sub get_eps_image {
 4160:     my ($src)=@_;
 4161:     my $orig_src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1], $src);
 4162: 
 4163:     # In order to prevent the substitution of the alt text, we need to
 4164:     # be sure the orig_src file is on system now so:
 4165: 
 4166:     if (! -e $orig_src) {
 4167: 	&Apache::lonnet::repcopy($orig_src); # Failure is not completely fatal.
 4168:     }
 4169:     &Apache::lonxml::debug("get_eps_image: Original image: $orig_src");
 4170:     my ($spath, $sname, $sext) = fileparse($src, qr/\.(gif|png|jpg|jpeg)/i);
 4171:     $src=~s/\.(gif|png|jpg|jpeg)$/\.eps/i;
 4172:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 4173:     &Apache::lonxml::debug("Filelocation gives: $src");
 4174:     if (! -e $src) {
 4175: 	&Apache::lonxml::debug("$src does not exist");
 4176: 	if (&Apache::lonnet::repcopy($src) ne 'ok' ) {
 4177: 	    &Apache::lonxml::debug("Repcopy of $src failed (1)");
 4178: 	    #if replication failed try to find ps file
 4179: 	    $src=~s/\.eps$/\.ps/;
 4180: 	    &Apache::lonxml::debug("Now looking for $src");
 4181: 	    #if no ps file try to replicate it.
 4182: 	    my $didrepcopy = &Apache::lonnet::repcopy($src);
 4183: 	    &Apache::lonxml::debug("repcopy of $src ... $didrepcopy");
 4184: 	    if ( (not -e $src) ||
 4185: 		($didrepcopy ne 'ok')) {
 4186: 		&Apache::lonxml::debug("Failed to find or replicate $src");
 4187: 
 4188: 		#if replication failed try to produce eps file dynamically
 4189: 		$src=~s/\.ps$/\.eps/;
 4190: 		my $temp_file;
 4191: 		open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
 4192: 		my $newsrc=$orig_src;
 4193: 		$newsrc =~ s|(.*)/res/|/home/httpd/html/res/|;
 4194: 		&Apache::lonxml::debug("queueing $newsrc for dynamic eps production.");
 4195: 		print FILE "$newsrc\n";
 4196: 		close FILE;
 4197: 		$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
 4198: 		$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
 4199: 		if ($sext ne "") {	 # Put the ext. back in to uniquify.
 4200: 		    $src =~ s/\.eps$/$sext.eps/;
 4201: 		}
 4202: 	    }
 4203: 	}
 4204:     }
 4205:     my ($path,$file)=($src=~m|(.*)/([^/]*)$|);
 4206:     &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />");
 4207:     return ($path.'/',$file);
 4208: }
 4209: 
 4210: sub eps_generation {
 4211:     my ($src,$file,$width_param) = @_;	     
 4212:     my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
 4213:     my $temp_file = Apache::File->new('>>'.$filename); 
 4214:     print $temp_file "$src\n";
 4215:     my $newsrc = $src;
 4216:     $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
 4217:     $newsrc=~s/\/home\/httpd\/html\/res//;
 4218:     $newsrc=~s/\/home\/([^\/]*)\/public_html\//\/$1\//;
 4219:     $newsrc=~s/\/\.\//\//;
 4220:     $newsrc=~s/\/([^\/]+)\.(ps|eps)/\//;
 4221:     if ($newsrc=~/\/home\/httpd\/lonUsers\//) {
 4222: 	$newsrc=~s/\/home\/httpd\/lonUsers//;
 4223: 	$newsrc=~s/\/([^\/]+)\/(\w)\/(\w)\/(\w)\//\/$1\//;
 4224:     }
 4225:     if ($newsrc=~/\/userfiles\//) {
 4226: 	return ' \graphicspath{{'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4227:     } else {
 4228: 	return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 4229:     }
 4230: }
 4231: 
 4232: sub file_path {     
 4233:     my $src=shift;
 4234:     my ($file,$path); 
 4235:     if ($src =~ m!(.*)/([^/]*)$!) {
 4236: 	$file = $2; 
 4237: 	$path = $1.'/'; 
 4238:     } 
 4239:     return $file,$path;
 4240: }
 4241: #  Converts a measurement in to mm from any of 
 4242: #  the other valid LaTeX units of measure.
 4243: #  If the units of measure are missing from the 
 4244: #  parameter, it is assumed to be in and returned
 4245: #  with mm units of measure
 4246: sub recalc {
 4247:     my $argument = shift;
 4248:     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
 4249:     $argument=~/\s*(\d+\.?\d*)\s*(mm|cm|in|pc|pt)/;
 4250:     my $value=$1;
 4251:     my $units=$2;
 4252:     if ($units eq 'cm') {
 4253: 	$value*=10;
 4254:     } elsif ($units eq 'in') {
 4255: 	$value*=25.4;
 4256:     } elsif ($units eq 'pc') {
 4257: 	$value*=(25.4*12/72.27);
 4258:     } elsif ($units eq 'pt') {
 4259: 	$value*=(25.4/72.27);
 4260:     }
 4261:     return $value.' mm';
 4262: }
 4263: 
 4264: sub LATEX_length {
 4265:     my $garbage=shift;
 4266:     $garbage=~s/^\s+$//;
 4267:     $garbage=~s/^\s+(\S.*)/$1/;#space before 
 4268:     $garbage=~s/(.*\S)\s+$/$1/;#space after 
 4269:     $garbage=~s/(\s)+/$1/;#only one space
 4270:     $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
 4271:     $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
 4272:     $garbage=~s/([^\\])\$/$1/g;#$
 4273:     $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
 4274:    $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;
 4275:     $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;
 4276:     $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;
 4277:     $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;
 4278:     $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;
 4279:     $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;
 4280:     $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
 4281:     #remove some other LaTeX command
 4282:     $garbage=~s|\\(\w+)\\|\\|g;	 
 4283:     $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
 4284:     $garbage=~s|\+|11|g;
 4285:     my  $value=length($garbage);
 4286:     return $value;
 4287: }
 4288: 
 4289: 
 4290: # is_inside_of $tagstack $tag
 4291: #    This sub returns true if the current state of Xml processing
 4292: #    is inside of the tag.   
 4293: # Parameters:
 4294: #     tagstack   - The tagstack from the parser.
 4295: #     tag        - The tag (without the <>'s.).
 4296: # Sample usage:
 4297: #     if (is_inside_of($tagstack "table")) {
 4298: #          # I'm in a table....
 4299: #      }
 4300: sub is_inside_of {
 4301:     my ($tagstack, $tag) = @_;
 4302:     my @stack = @$tagstack;
 4303:     for (my $i = ($#stack - 1); $i >= 0; $i--) {
 4304: 	if ($stack[$i] eq $tag) {
 4305: 	    return 1;
 4306: 	}
 4307:     }
 4308:     return 0;
 4309: }
 4310: 
 4311: 
 4312: 1;
 4313: __END__

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