File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.329: download - view: text, annotated - select for diffs
Wed May 17 22:08:08 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: version_2_1_99_0, HEAD
- calling init_tth from start_page (effectively)
- reducing dependencies from lontextconvert.pm

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

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