File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.324: download - view: text, annotated - select for diffs
Thu Mar 23 23:47:12 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- enabling wider support for webgrade

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

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