File:  [LON-CAPA] / loncom / xml / londefdef.pm
Revision 1.230: download - view: text, annotated - select for diffs
Thu Jul 15 17:05:20 2004 UTC (19 years, 10 months ago) by sakharuk
Branches: MAIN
CVS tags: version_1_1_99_3, HEAD
Bug 3198 (<th>no workie) is fixed.

    1: # The LearningOnline Network with CAPA
    2: # Tags Default Definition Module 
    3: #
    4: # $Id: londefdef.pm,v 1.230 2004/07/15 17:05:20 sakharuk 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: # last modified 06/26/00 by Alexander Sakharuk
   41: # 11/6,11/30,02/01/01,5/4 Gerd Kortemeyer
   42: # 01/18 Alex Sakharuk
   43: 
   44: package Apache::londefdef; 
   45: 
   46: use Apache::lonnet();
   47: use strict;
   48: use Apache::lonxml;
   49: use Apache::File();
   50: use Image::Magick;
   51: use Apache::lonmenu();
   52: use Apache::lonmeta();
   53: use Apache::Constants qw(:common);
   54: 
   55: 
   56: BEGIN {
   57: 
   58:     &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'));
   59: 
   60: }
   61: 
   62: sub initialize_londefdef {
   63:     $Apache::londefdef::TD_redirection=0;
   64:     @Apache::londefdef::table = ();
   65:     $Apache::londefdef::select=0;
   66:     @Apache::londefdef::description=();
   67:     $Apache::londefdef::DD_redirection=0;
   68:     $Apache::londefdef::DT_redirection=0;
   69: }
   70: 
   71: #======================= TAG SUBROUTINES =====================
   72: #-- <output>
   73: sub start_output {
   74:     my ($target) = @_;
   75:     if ($target eq 'meta') { $Apache::lonxml::metamode--; }
   76:     return '';
   77: }
   78: sub end_output {
   79:     my ($target) = @_;
   80:     if ($target eq 'meta') { $Apache::lonxml::metamode++; }
   81:     return '';
   82: }
   83: #-- <m> tag
   84: sub start_m {
   85:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   86:     my $currentstring = '';
   87:     my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
   88:     if ($target eq 'web' || $target eq 'analyze') {
   89: 	$inside ='\\documentstyle{article}'.$inside;
   90: 	&Apache::lonxml::debug("M is starting with:$inside:");
   91: 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
   92: 	if ($eval eq 'on') {
   93: 	    $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
   94: 	    #&Apache::lonxml::debug("M is evaulated to:$inside:");
   95: 	}
   96: 	$currentstring = &Apache::lontexconvert::converted(\$inside);
   97: 	if ($Apache::lontexconvert::errorstring) {
   98: 	    &Apache::lonxml::warning("tth error: ".
   99: 				     $Apache::lontexconvert::errorstring);
  100: 	    $Apache::lontexconvert::errorstring='';
  101: 	}
  102: 	#&Apache::lonxml::debug("M is ends with:$currentstring:");
  103: 	$Apache::lonxml::post_evaluate=0;
  104:     } elsif ($target eq 'tex') {
  105: 	$currentstring = $inside;
  106: 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
  107: 	if ($eval eq 'on') {
  108: 	    $currentstring=&Apache::run::evaluate($currentstring,$safeeval,$$parstack[-1]);
  109: 	}
  110: 	if ($currentstring=~/^(\s*\\\\\s*)*$/) {$currentstring = ' \vskip 0 mm ';}
  111: 	$Apache::lonxml::post_evaluate=0;
  112:     }
  113:     return $currentstring;
  114: }
  115: 
  116: sub end_m {
  117:     my ($target,$token) = @_;
  118:     my $currentstring = '';
  119:     if ($target eq 'tex') {
  120: 	$currentstring = "";
  121:     }
  122:     return $currentstring;
  123: }
  124: 
  125: sub start_tthoption {
  126:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  127:     my $result;
  128:     if ($target eq 'web') {
  129: 	my $inside = &Apache::lonxml::get_all_text("/tthoption",$parser);
  130: 	$inside=~s/^\s*//;
  131: 	if ($ENV{'browser.mathml'}) {
  132: 	    &tth::ttmoptions($inside);
  133: 	} else {
  134: 	    &tth::tthoptions($inside);
  135: 	}
  136:     }
  137:     return $result;
  138: }
  139: 
  140: sub end_tthoption {
  141:     my ($target,$token) = @_;
  142:     my $result;
  143:     return $result;
  144: }
  145: 
  146: #-- <html> tag (end tag optional)
  147: sub start_html {
  148:     my ($target,$token) = @_;
  149:     my $currentstring = '';
  150:     my $options=$ENV{'course.'.$ENV{'request.course.id'}.'.tthoptions'};
  151:     &Apache::lontexconvert::init_tth();
  152:     if ($target eq 'web' || $target eq 'edit') {
  153: 	$currentstring = &Apache::lonxml::xmlbegin().
  154: 	    &Apache::lonxml::fontsettings();     
  155:     } elsif ($target eq 'tex') {
  156: 	@Apache::londefdef::table = ();
  157: 	$currentstring .= '\documentclass[letterpaper]{article}';
  158: 	if ($ENV{'form.latex_type'}=~'batchmode') {$currentstring .='\batchmode';} 
  159: 	$currentstring .= '\newcommand{\keephidden}[1]{}'.
  160:                           '\renewcommand{\deg}{$^{\circ}$}'.
  161:                           '\usepackage{longtable}'.
  162:                           '\usepackage{textcomp}'.
  163:                           '\usepackage{makeidx}'.
  164:                           '\usepackage[dvips]{graphicx}'.
  165:                           '\usepackage{epsfig}'.
  166:                           '\usepackage{calc}'.
  167:                           '\usepackage{amsmath}'.
  168:                           '\usepackage{amssymb}'.
  169:                           '\usepackage{amsfonts}'.
  170:                           '\usepackage{amsthm}'.
  171:                           '\usepackage{amscd}'.
  172:                           '\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}}'.
  173:                           '\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}}';
  174:     }
  175:     return $currentstring;
  176: }
  177: 
  178: sub end_html {
  179:     my ($target,$token) = @_;
  180:     my $currentstring = '';
  181:     if ($target eq 'web') {
  182: 	$currentstring = &Apache::lonxml::xmlend();
  183:     }
  184:     return $currentstring;
  185: }
  186: 
  187: #-- <head> tag (end tag optional)
  188: sub start_head {
  189:     my ($target,$token) = @_;
  190:     my $currentstring = '';
  191:     if ($target eq 'web') {
  192: 	$currentstring = $token->[4];     
  193:     } 
  194:     return $currentstring;
  195: }
  196: 
  197: sub end_head {
  198:     my ($target,$token) = @_;
  199:     my $currentstring = '';
  200:     if ($target eq 'web' && $ENV{'request.state'} eq 'published') {
  201: 	$currentstring = &Apache::lonmenu::registerurl(undef,$target).
  202: 	    $token->[2];    
  203:     } 
  204:     return $currentstring;
  205: }
  206: 
  207: #-- <map> tag (end tag required)
  208: sub start_map {
  209:     my ($target,$token) = @_;
  210:     my $currentstring = '';
  211:     if ($target eq 'web') {
  212: 	$currentstring = $token->[4];     
  213:     } 
  214:     return $currentstring;
  215: }
  216: 
  217: sub end_map {
  218:     my ($target,$token) = @_;
  219:     my $currentstring = '';
  220:     if ($target eq 'web') {
  221: 	$currentstring = $token->[2];    
  222:     } 
  223:     return $currentstring;
  224: }
  225: 
  226: #-- <select> tag (end tag required)
  227: sub start_select {
  228:     my ($target,$token) = @_;
  229:     my $currentstring = '';
  230:     if ($target eq 'web') {
  231: 	$currentstring = $token->[4];     
  232:     }  elsif ($target eq 'tex') {
  233: 	$Apache::londefdef::select=0;
  234:     }
  235:     return $currentstring;
  236: }
  237: 
  238: sub end_select {
  239:     my ($target,$token) = @_;
  240:     my $currentstring = '';
  241:     if ($target eq 'web') {
  242: 	$currentstring = $token->[2];    
  243:     } 
  244:     return $currentstring;
  245: }
  246: 
  247: #-- <option> tag (end tag optional)
  248: sub start_option {
  249:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  250:     my $currentstring = '';
  251:     if ($target eq 'web') {
  252: 	$currentstring = $token->[4];     
  253:     } elsif ($target eq 'tex') {
  254: 	$Apache::londefdef::select++;
  255: 	if ($Apache::londefdef::select == 1) {
  256: 	    $currentstring='\noindent\fbox{'.&Apache::lonxml::get_param('value',$parstack,$safeeval).'}\keephidden{';
  257: 	} else {
  258: 	    $currentstring='\keephidden{';
  259: 	}
  260:     }
  261:     return $currentstring;
  262: }
  263: 
  264: sub end_option {
  265:     my ($target,$token) = @_;
  266:     my $currentstring = '';
  267:     if ($target eq 'web') {
  268: 	$currentstring = $token->[2];    
  269:     }  elsif ($target eq 'tex') {
  270: 	$currentstring='}';
  271:     }
  272:     return $currentstring;
  273: }
  274: 
  275: #-- <input> tag (end tag forbidden)
  276: sub start_input {
  277:     my ($target,$token) = @_;
  278:     my $currentstring = '';
  279:     if ($target eq 'web') {
  280: 	$currentstring = $token->[4];     
  281:     } 
  282:     return $currentstring;
  283: }
  284: 
  285: sub end_input {
  286:     my ($target,$token) = @_;
  287:     my $currentstring = '';
  288:     if ($target eq 'web') {
  289: 	$currentstring = $token->[2];    
  290:     } 
  291:     return $currentstring;
  292: }
  293: 
  294: #-- <textarea> tag (end tag required)
  295: sub start_textarea {
  296:     my ($target,$token) = @_;
  297:     my $currentstring = '';
  298:     if ($target eq 'web') {
  299: 	$currentstring = $token->[4];     
  300:     } 
  301:     return $currentstring;
  302: }
  303: 
  304: sub end_textarea {
  305:     my ($target,$token) = @_;
  306:     my $currentstring = '';
  307:     if ($target eq 'web') {
  308: 	$currentstring = $token->[2];    
  309:     } 
  310:     return $currentstring;
  311: }
  312: 
  313: #-- <form> tag (end tag required)
  314: sub start_form {
  315:     my ($target,$token) = @_;
  316:     my $currentstring = '';
  317:     if ($target eq 'web') {
  318: 	$currentstring = $token->[4];     
  319:     } 
  320:     return $currentstring;
  321: }
  322: 
  323: sub end_form {
  324:     my ($target,$token) = @_;
  325:     my $currentstring = '';
  326:     if ($target eq 'web') {
  327: 	$currentstring = $token->[2];    
  328:     } 
  329:     return $currentstring;
  330: }
  331: 
  332: #-- <title> tag (end tag required)
  333: sub start_title {
  334:     my ($target,$token) = @_;
  335:     my $currentstring = '';
  336:     if ($target eq 'web') {
  337: 	$currentstring = $token->[4];     
  338:     } elsif ($target eq 'tex') {
  339: 	$currentstring .= '\keephidden{Title of the document:  ' 
  340:     }
  341:     if ($target eq 'meta') {
  342: 	$currentstring='<title>';
  343: 	&start_output($target);
  344:     }
  345:     return $currentstring;
  346: }
  347: 
  348: sub end_title {
  349:     my ($target,$token) = @_;
  350:     my $currentstring = '';
  351:     if ($target eq 'web') {
  352: 	$currentstring = $token->[2];    
  353:     } elsif ($target eq 'tex') {
  354: 	$currentstring .= '}';
  355:     }  
  356:     if ($target eq 'meta') {
  357: 	&end_output($target);
  358: 	$currentstring='</title>';
  359:     } 
  360:     return $currentstring;
  361: }
  362: 
  363: #-- <meta> tag (end tag forbidden)
  364: sub start_meta {
  365:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  366:     my $currentstring = '';
  367:     if ($target eq 'web') {
  368: 	my $args='';
  369: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  370: 	if ($args eq '') {
  371: 	    &Apache::lonxml::get_all_text("/meta",$parser);
  372: 	} else {
  373: 	    $currentstring = $token->[4];
  374: 	}
  375:     } elsif ($target eq 'meta') {
  376: 	unless (&Apache::lonxml::get_param
  377: 		('http-equiv',$parstack,$safeeval,undef,1)) {
  378: 	    my $name=$token->[2]->{'name'};
  379: 	    $name=~tr/A-Z/a-z/;
  380: 	    $name=~s/\s/\_/gs;
  381: 	    $name=~s/\W//gs;
  382: 	    if ($name) {
  383: 		$currentstring='<'.$name;
  384:                  my $display=&Apache::lonxml::get_param
  385: 		('display',$parstack,$safeeval,undef,1);
  386:                 if ($display) {
  387:                     $display=~s/\"/\'/g;
  388: 		    $currentstring.=' display="'.$display.'"';
  389:                 }
  390: 		$currentstring.='>'.
  391: 		    &Apache::lonxml::get_param
  392: 			('content',$parstack,$safeeval,undef,1).
  393: 			'</'.$name.'>';
  394: 	    }
  395:             my $display=&Apache::lonxml::get_param
  396: 		('display',$parstack,$safeeval,undef,1);
  397:             if ($display) {
  398: 		$display=&HTML::Entities::encode($display,'<>&"');
  399: 		$currentstring.='<'.$name.'.display>'.$display.
  400:                                '</'.$name.'.display>';
  401:             }
  402: 	}
  403:     } elsif ($target eq 'tex') {
  404: 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
  405: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
  406: 	if ((not defined $content) && (not defined $name)) {
  407: 	    &Apache::lonxml::startredirection();
  408: 	}
  409:     }
  410:     return $currentstring;
  411: }
  412: 
  413: sub end_meta {
  414:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  415:     my $currentstring = '';
  416:     if ($target eq 'web') {
  417: 	my $args='';
  418: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  419: 	if ($args ne '') {
  420: 	    $currentstring = $token->[4];
  421: 	}
  422:     } elsif ($target eq 'tex') {
  423: 	my $content=&Apache::lonxml::get_param('content',$parstack,$safeeval);
  424: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
  425: 	if ((not defined $content) && (not defined $name)) {
  426: 	    &Apache::lonxml::endredirection();
  427: 	}
  428:     }
  429:     return $currentstring;
  430: }
  431: 
  432: # accessrule
  433: sub start_accessrule {
  434:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  435:     my $currentstring = '';
  436:     my $eff=&Apache::lonxml::get_param
  437: 	('effect',$parstack,$safeeval,undef,1);
  438:     my $realm=&Apache::lonxml::get_param
  439: 	('realm',$parstack,$safeeval,undef,1);
  440:     my $role=&Apache::lonxml::get_param
  441: 	('role',$parstack,$safeeval,undef,1);
  442:     $realm=~s/\s+//g;
  443:     $realm=~s/\//\_/g;
  444:     $realm=~s/^\_//;
  445:     $realm=~s/\W/\;/g;
  446:     $role=~s/\s+//g;
  447:     $role=~s/\//\_/g;
  448:     $role=~s/\W/\;/g;
  449:     if ($target eq 'web') {
  450: 	my $args='';
  451: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  452: 	if ($args eq '') {
  453: 	    &Apache::lonxml::get_all_text("/accessrule",$parser);
  454: 	} else {
  455: 	    $currentstring = $token->[4];
  456: 	}
  457:     }
  458:     if ($target eq 'meta') {
  459: 	$currentstring='<rule>'.$eff.':'.$realm.':'.$role.'</rule>';
  460:     }
  461:     return $currentstring;
  462: }
  463: 
  464: sub end_accessrule {
  465:     my ($target,$token,$tagstack,$parstack,$parser) = @_;
  466:     my $currentstring = '';
  467:     if ($target eq 'web') {
  468: 	my $args='';
  469: 	if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  470: 	if ($args ne '') {
  471: 	    $currentstring = $token->[4];
  472: 	}
  473:     } 
  474:     return $currentstring;
  475: }
  476: 
  477: #-- <body> tag (end tag required)
  478: sub start_body {
  479:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  480:     my $currentstring = '';
  481:     if ($target eq 'web') {
  482: 	if ($Apache::lonhomework::parsing_a_problem) {
  483: 	    &Apache::lonxml::warning("<body> tag found inside of <problem> tag this can cause problems.");
  484: 	    return '';
  485: 	}
  486: 	if (!$Apache::lonxml::registered && 
  487: 	    $ENV{'request.state'} eq 'published') {
  488: 	    $currentstring.='<head>'.
  489: 		&Apache::lonmenu::registerurl(undef,$target).'</head>';
  490: 	}
  491: # Accessibility
  492: 	if ($ENV{'browser.imagesuppress'} eq 'on') {
  493: 	    delete($token->[2]->{'background'});
  494: 	}
  495: 	if ($ENV{'browser.fontenhance'} eq 'on') {
  496: 	    my $style='';
  497: 	    foreach my $key (keys(%{$token->[2]})) {
  498: 		if ($key =~ /^style$/i) {
  499: 		    $style.=$token->[2]->{$key}.';';
  500: 		    delete($token->[2]->{$key});
  501: 		}
  502: 	    }
  503: 	    $token->[2]->{'style'}=$style.'; font-size: x-large;';
  504: 	}
  505: 	if ($ENV{'browser.blackwhite'} eq 'on') {
  506: 	    delete($token->[2]->{'font'});
  507: 	    delete($token->[2]->{'link'});
  508: 	    delete($token->[2]->{'alink'});
  509: 	    delete($token->[2]->{'vlink'});
  510: 	    delete($token->[2]->{'bgcolor'});
  511: 	    delete($token->[2]->{'background'});
  512: 	}
  513: # Overload loads
  514: 	my $onLoad='';
  515: 	foreach my $key (keys(%{$token->[2]})) {
  516: 	    if ($key =~ /^onload$/i) {
  517: 		$onLoad.=$token->[2]->{$key}.';';
  518: 		delete($token->[2]->{$key});
  519: 	    }
  520: 	}
  521: 	$token->[2]->{'onload'}=&Apache::lonmenu::loadevents().';'.$onLoad;
  522: 	my $onUnload='';
  523: 	foreach my $key (keys(%{$token->[2]})) {
  524: 	    if ($key =~ /^onunload$/i) {
  525: 		$onUnload.=$token->[2]->{$key}.';';
  526: 		delete($token->[2]->{$key});
  527: 	    }
  528: 	}
  529: 	$token->[2]->{'onunload'}=&Apache::lonmenu::unloadevents().
  530: 	    ';'.$onUnload;
  531: 	
  532: 	$currentstring .= '<'.$token->[1];
  533: 	foreach (keys %{$token->[2]}) {
  534: 	    $currentstring.=' '.$_.'="'.$token->[2]->{$_}.'"';
  535: 	}
  536: 	$currentstring.='>';
  537: 	if ($ENV{'request.state'} ne 'published') {
  538: 	    $currentstring.=(<<EDITBUTTON);
  539: 		<form method="post">
  540: 		<input type="submit" name="editmode" accesskey="e" value="Edit" />
  541: 		</form>
  542: EDITBUTTON
  543: 	} else {
  544: 	    $currentstring.=&Apache::lonmenu::menubuttons(undef,$target,1);
  545: 	}
  546: 	$currentstring.=&Apache::lonxml::message_location();
  547:     } elsif ($target eq 'tex') {
  548: 	$currentstring = '\begin{document}';  
  549:     } 
  550:     return $currentstring;
  551: }
  552: 
  553: sub end_body {
  554:     my ($target,$token) = @_;
  555:     my $currentstring = '';
  556:     if ($target eq 'web') {
  557: 	$currentstring = $token->[2];     
  558:     } elsif ($target eq 'tex') {
  559: 	$currentstring = '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';  
  560:     } 
  561:     return $currentstring;
  562: }
  563: 
  564: #-- <center> tag (end tag required)
  565: sub start_center {
  566:     my ($target,$token) = @_;
  567:     my $currentstring = '';
  568:     if ($target eq 'web') {
  569: 	$currentstring = $token->[4];     
  570:     } elsif ($target eq 'tex') {
  571: 	$currentstring = '\begin{center}';  
  572:     }
  573:     return $currentstring;
  574: }
  575: 
  576: sub end_center {
  577:     my ($target,$token) = @_;
  578:     my $currentstring = '';
  579:     if ($target eq 'web') {
  580: 	$currentstring = $token->[2];     
  581:     } elsif ($target eq 'tex') {
  582: 	$currentstring = '\end{center}';  
  583:     }
  584:     return $currentstring;
  585: }
  586: 
  587: #-- <b> tag (end tag required)
  588: sub start_b {
  589:     my ($target,$token) = @_;
  590:     my $currentstring = '';
  591:     if ($target eq 'web') {
  592: 	$currentstring = $token->[4];     
  593:     } elsif ($target eq 'tex') {
  594: 	$currentstring = '\textbf{';  
  595:     } 
  596:     return $currentstring;
  597: }
  598: 
  599: sub end_b {
  600:     my ($target,$token) = @_;
  601:     my $currentstring = '';
  602:     if ($target eq 'web') {
  603: 	$currentstring = $token->[2];     
  604:     } elsif ($target eq 'tex') {
  605: 	$currentstring = '}';  
  606:     } 
  607:     return $currentstring;
  608: }
  609: 
  610: #-- <strong> tag (end tag required)
  611: sub start_strong {
  612:     my ($target,$token) = @_;
  613:     my $currentstring = '';
  614:     if ($target eq 'web') {
  615: 	$currentstring = $token->[4];     
  616:     } elsif ($target eq 'tex') {
  617: 	$currentstring = '\textbf{';  
  618:     } 
  619:     return $currentstring;
  620: }
  621: 
  622: sub end_strong {
  623:     my ($target,$token) = @_;
  624:     my $currentstring = '';
  625:     if ($target eq 'web') {	
  626: 	$currentstring = $token->[2];     
  627:     } elsif ($target eq 'tex') {
  628: 	$currentstring = '}';  
  629:     }
  630:     return $currentstring;
  631: }
  632: 
  633: #-- <h1> tag (end tag required)
  634: sub start_h1 {
  635:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  636:     my $currentstring = '';
  637:     if ($target eq 'web') {
  638: 	$currentstring .= $token->[4];
  639:     } elsif ($target eq 'tex') {
  640: 	my $pre;
  641: 	my $align=lc(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1));
  642: 	if ($align eq 'center') {
  643: 	    $pre='\begin{center}';
  644: 	} elsif ($align eq 'left') {
  645: 	    $pre='\rlap{';
  646: 	} elsif ($align eq 'right') {
  647: 	    $pre=' \hfill \llap{';
  648: 	}
  649: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  650: 	if (not defined $TeXsize) {$TeXsize="large";}
  651: 	$currentstring .= $pre.'{\\'.$TeXsize.' \textbf{'; 
  652:     } elsif ($target eq 'meta') {
  653: 	$currentstring='<subject>';
  654: 	&start_output($target);
  655:     }
  656:     return $currentstring;
  657: }
  658: 
  659: sub end_h1 {
  660:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  661:     my $currentstring = '';
  662:     if ($target eq 'web') {
  663: 	$currentstring .= $token->[2];
  664:     } elsif ($target eq 'tex') {
  665: 	my $post='\vskip 0 mm ';
  666: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  667: 	if ($align eq 'center') {
  668: 	    $post='\end{center}';
  669: 	} elsif ($align eq 'left') {
  670: 	    $post='} \hfill'.'\vskip 0 mm ';
  671: 	} elsif ($align eq 'right') {
  672: 	    $post='}'.'\vskip 0 mm ';
  673: 	}
  674: 	$currentstring .= '}}'.$post;
  675:     } elsif ($target eq 'meta') {
  676: 	&end_output($target);
  677: 	$currentstring='</subject>';
  678:     } 
  679:     return $currentstring;
  680: }
  681: 
  682: #-- <h2> tag
  683: sub start_h2 {
  684:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  685:     my $currentstring = '';
  686:     if ($target eq 'web') {
  687: 	$currentstring .= $token->[4];
  688:     } elsif ($target eq 'tex') {
  689: 	my $pre;
  690: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  691: 	if ($align eq 'center') {
  692: 	    $pre='\begin{center}';
  693: 	} elsif ($align eq 'left') {
  694: 	    $pre='\rlap{';
  695: 	} elsif ($align eq 'right') {
  696: 	    $pre=' \hfill \llap{';
  697: 	}
  698: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  699: 	if (not defined $TeXsize) {$TeXsize="large";}
  700: 	$currentstring .= $pre.'{\\'.$TeXsize.' \textbf{'; 
  701:     } 
  702:     return $currentstring;
  703: }
  704: 
  705: sub end_h2 {
  706:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  707:     my $currentstring = '';
  708:     if ($target eq 'web') {
  709: 	$currentstring .= $token->[2];
  710:     } elsif ($target eq 'tex') {
  711: 	my $post='\vskip 0 mm ';
  712: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  713: 	if ($align eq 'center') {
  714: 	    $post='\end{center}';
  715: 	} elsif ($align eq 'left') {
  716: 	    $post='} \hfill'.'\vskip 0 mm ';
  717: 	} elsif ($align eq 'right') {
  718: 	    $post='}'.'\vskip 0 mm ';
  719: 	}
  720: 	$currentstring .= '}}'.$post;
  721:     } 
  722:     return $currentstring;
  723: }
  724: 
  725: #-- <h3> tag
  726: sub start_h3 {
  727:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  728:     my $currentstring = '';
  729:     if ($target eq 'web') {
  730: 	$currentstring .= $token->[4];
  731:     } elsif ($target eq 'tex') {
  732: 	my $pre;
  733: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  734: 	if ($align eq 'center') {
  735: 	    $pre='\begin{center}';
  736: 	} elsif ($align eq 'left') {
  737: 	    $pre='\rlap{';
  738: 	} elsif ($align eq 'right') {
  739: 	    $pre=' \hfill \llap{';
  740: 	}
  741: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  742: 	if (not defined $TeXsize) {$TeXsize="large";}
  743: 	$currentstring .= $pre.'{\\'.$TeXsize.' \textbf{'; 
  744:     } 
  745:     return $currentstring;
  746: }
  747: 
  748: sub end_h3 {
  749:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  750:     my $currentstring = '';
  751:     if ($target eq 'web') {
  752: 	$currentstring .= $token->[2];
  753:     } elsif ($target eq 'tex') {
  754: 	my $post='\vskip 0 mm ';
  755: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  756: 	if ($align eq 'center') {
  757: 	    $post='\end{center}';
  758: 	} elsif ($align eq 'left') {
  759: 	    $post='} \hfill'.'\vskip 0 mm ';
  760: 	} elsif ($align eq 'right') {
  761: 	    $post='}'.'\vskip 0 mm ';
  762: 	}
  763: 	$currentstring .= '}}'.$post;
  764:     } 
  765:     return $currentstring;
  766: }
  767: 
  768: #-- <h4> tag
  769: sub start_h4 {
  770:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  771:     my $currentstring = '';
  772:     if ($target eq 'web') {
  773: 	$currentstring .= $token->[4];
  774:     } elsif ($target eq 'tex') {
  775: 	my $pre;
  776: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  777: 	if ($align eq 'center') {
  778: 	    $pre='\begin{center}';
  779: 	} elsif ($align eq 'left') {
  780: 	    $pre='\rlap{';
  781: 	} elsif ($align eq 'right') {
  782: 	    $pre=' \hfill \llap{';
  783: 	}
  784: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  785: 	if (not defined $TeXsize) {$TeXsize="large";}
  786: 	$currentstring .= $pre.'{\\'.$TeXsize.' \textbf{'; 
  787:     } 
  788:     return $currentstring;
  789: }
  790: 
  791: sub end_h4 {
  792:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  793:     my $currentstring = '';
  794:     if ($target eq 'web') {
  795: 	$currentstring .= $token->[2];
  796:     } elsif ($target eq 'tex') {
  797: 	my $post='\vskip 0 mm ';
  798: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  799: 	if ($align eq 'center') {
  800: 	    $post='\end{center}';
  801: 	} elsif ($align eq 'left') {
  802: 	    $post='} \hfill'.'\vskip 0 mm ';
  803: 	} elsif ($align eq 'right') {
  804: 	    $post='}'.'\vskip 0 mm ';
  805: 	}
  806: 	$currentstring .= '}}'.$post;
  807:     } 
  808:     return $currentstring;
  809: }
  810: 
  811: #-- <h5> tag
  812: sub start_h5 {
  813:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  814:     my $currentstring = '';
  815:     if ($target eq 'web') {
  816: 	$currentstring .= $token->[4];
  817:     } elsif ($target eq 'tex') {
  818: 	my $pre;
  819: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  820: 	if ($align eq 'center') {
  821: 	    $pre='\begin{center}';
  822: 	} elsif ($align eq 'left') {
  823: 	    $pre='\rlap{';
  824: 	} elsif ($align eq 'right') {
  825: 	    $pre=' \hfill \llap{';
  826: 	}
  827: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  828: 	if (not defined $TeXsize) {$TeXsize="large";}
  829: 	$currentstring .= $pre.'{\\'.$TeXsize.' \textbf{'; 
  830:     } 
  831:     return $currentstring;
  832: }
  833: 
  834: sub end_h5 {
  835:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  836:     my $currentstring = '';
  837:     if ($target eq 'web') {
  838: 	$currentstring .= $token->[2];
  839:     } elsif ($target eq 'tex') {
  840: 	my $post='\vskip 0 mm ';
  841: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  842: 	if ($align eq 'center') {
  843: 	    $post='\end{center}';
  844: 	} elsif ($align eq 'left') {
  845: 	    $post='} \hfill'.'\vskip 0 mm ';
  846: 	} elsif ($align eq 'right') {
  847: 	    $post='}'.'\vskip 0 mm ';
  848: 	}
  849: 	$currentstring .= '}}'.$post;
  850:     } 
  851:     return $currentstring;
  852: }
  853: 
  854: #-- <h6> tag
  855: sub start_h6 {
  856:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  857:     my $currentstring = '';
  858:     if ($target eq 'web') {
  859: 	$currentstring .= $token->[4];
  860:     } elsif ($target eq 'tex') {
  861: 	my $pre;
  862: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  863: 	if ($align eq 'center') {
  864: 	    $pre='\begin{center}';
  865: 	} elsif ($align eq 'left') {
  866: 	    $pre='\rlap{';
  867: 	} elsif ($align eq 'right') {
  868: 	    $pre=' \hfill \llap{';
  869: 	}
  870: 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
  871: 	if (not defined $TeXsize) {$TeXsize="large";}
  872: 	$currentstring .= $pre.'{\\'.$TeXsize.' \textbf{'; 
  873:     } 
  874:     return $currentstring;
  875: }
  876: 
  877: sub end_h6 {
  878:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
  879:     my $currentstring = '';
  880:     if ($target eq 'web') {
  881: 	$currentstring .= $token->[2];
  882:     } elsif ($target eq 'tex') {
  883: 	my $post='\vskip 0 mm ';
  884: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
  885: 	if ($align eq 'center') {
  886: 	    $post='\end{center}';
  887: 	} elsif ($align eq 'left') {
  888: 	    $post='} \hfill'.'\vskip 0 mm ';
  889: 	} elsif ($align eq 'right') {
  890: 	    $post='}'.'\vskip 0 mm ';
  891: 	}
  892: 	$currentstring .= '}}'.$post;
  893:     } 
  894:     return $currentstring;
  895: }
  896: 
  897: #--- <cite> tag (end tag required)
  898: sub start_cite {
  899:     my ($target,$token) = @_;
  900:     my $currentstring = '';
  901:     if ($target eq 'web') {
  902: 	$currentstring .= $token->[4];
  903:     } elsif ($target eq 'tex') {
  904: 	$currentstring .= '\textit{';
  905:     }
  906:     return $currentstring;
  907: }
  908: 
  909: sub end_cite {
  910:     my ($target,$token) = @_;
  911:     my $currentstring = '';
  912:     if ($target eq 'web') {
  913: 	$currentstring .= $token->[2];
  914:     } elsif ($target eq 'tex') {
  915: 	$currentstring .= '}';
  916:     }
  917:     return $currentstring;
  918: }
  919: 
  920: #-- <i> tag (end tag required)
  921: sub start_i {
  922:     my ($target,$token) = @_;
  923:     my $currentstring = '';
  924:     if ($target eq 'web') {
  925: 	$currentstring .= $token->[4];
  926:     } elsif ($target eq 'tex') {
  927: 	$currentstring .= '\textit{';
  928:     }
  929:     return $currentstring;
  930: }
  931: 
  932: sub end_i {
  933:     my ($target,$token) = @_;
  934:     my $currentstring = '';
  935:     if ($target eq 'web') {
  936: 	$currentstring .= $token->[2];
  937:     } elsif ($target eq 'tex') {
  938: 	$currentstring .= '}';
  939:     } 
  940:     return $currentstring;
  941: }
  942: 
  943: #-- <address> tag (end tag required)
  944: sub start_address {
  945:     my ($target,$token) = @_;
  946:     my $currentstring = '';
  947:     if ($target eq 'web') {
  948: 	$currentstring .= $token->[4];
  949:     } elsif ($target eq 'tex') {
  950: 	$currentstring .= '\textit{';
  951:     }
  952:     return $currentstring;
  953: }
  954: 
  955: sub end_address {
  956:     my ($target,$token) = @_;
  957:     my $currentstring = '';
  958:     if ($target eq 'web') {
  959: 	$currentstring .= $token->[2];
  960:     } elsif ($target eq 'tex') {
  961: 	$currentstring .= '}';
  962:     }
  963:     return $currentstring;
  964: }
  965: 
  966: #-- <dfn> tag (end tag required)
  967: sub start_dfn {
  968:     my ($target,$token) = @_;
  969:     my $currentstring = '';
  970:     if ($target eq 'web') {
  971: 	$currentstring .= $token->[4];
  972:     } elsif ($target eq 'tex') {
  973: 	$currentstring .= '\textit{';
  974:     } 
  975:     return $currentstring;
  976: }
  977: 
  978: sub end_dfn {
  979:     my ($target,$token) = @_;
  980:     my $currentstring = '';
  981:     if ($target eq 'web') {
  982: 	$currentstring .= $token->[2];
  983:     } elsif ($target eq 'tex') {
  984: 	$currentstring .= '}';
  985:     }
  986:     return $currentstring;
  987: }
  988: 
  989: #-- <tt> tag (end tag required)
  990: sub start_tt {
  991:     my ($target,$token) = @_;
  992:     my $currentstring = '';
  993:     if ($target eq 'web') {
  994: 	$currentstring .= $token->[4];
  995:     } elsif ($target eq 'tex') {
  996: 	$currentstring .= '\texttt{';
  997:     }
  998:     return $currentstring;
  999: }
 1000: 
 1001: sub end_tt {
 1002:     my ($target,$token) = @_;
 1003:     my $currentstring = '';
 1004:     if ($target eq 'web') {
 1005: 	$currentstring .= $token->[2];
 1006:     } elsif ($target eq 'tex') {
 1007: 	$currentstring .= '}';
 1008:     }
 1009:     return $currentstring;
 1010: }
 1011: 
 1012: #-- <kbd> tag (end tag required)
 1013: sub start_kbd {
 1014:     my ($target,$token) = @_;
 1015:     my $currentstring = '';
 1016:     if ($target eq 'web') {
 1017: 	$currentstring .= $token->[4];
 1018:     } elsif ($target eq 'tex') {
 1019: 	$currentstring .= '\texttt{';
 1020:     }
 1021:     return $currentstring;
 1022: }
 1023: 
 1024: sub end_kbd {
 1025:     my ($target,$token) = @_;
 1026:     my $currentstring = '';
 1027:     if ($target eq 'web') {
 1028: 	$currentstring .= $token->[2];
 1029:     } elsif ($target eq 'tex') {
 1030: 	$currentstring .= '}';
 1031:     }
 1032:     return $currentstring;
 1033: }
 1034: 
 1035: #-- <code> tag (end tag required)
 1036: sub start_code {
 1037:     my ($target,$token) = @_;
 1038:     my $currentstring = '';
 1039:     if ($target eq 'web') {
 1040: 	$currentstring .= $token->[4];
 1041:     } elsif ($target eq 'tex') {
 1042: 	$currentstring .= '\texttt{';
 1043:     } 
 1044:     return $currentstring;
 1045: }
 1046: 
 1047: sub end_code {
 1048:     my ($target,$token) = @_;
 1049:     my $currentstring = '';
 1050:     if ($target eq 'web') {
 1051: 	$currentstring .= $token->[2];
 1052:     } elsif ($target eq 'tex') {
 1053: 	$currentstring .= '}';
 1054:     } 
 1055:     return $currentstring;
 1056: }
 1057: 
 1058: #-- <em> tag (end tag required)
 1059: sub start_em {
 1060:     my ($target,$token) = @_;
 1061:     my $currentstring = '';
 1062:     if ($target eq 'web') {
 1063: 	$currentstring .= $token->[4];
 1064:     } elsif ($target eq 'tex') {
 1065: 	$currentstring .= '\emph{';
 1066:     }
 1067:     return $currentstring;
 1068: }
 1069: 
 1070: sub end_em {
 1071:     my ($target,$token) = @_;
 1072:     my $currentstring = '';
 1073:     if ($target eq 'web') {
 1074: 	$currentstring .= $token->[2];
 1075:     } elsif ($target eq 'tex') {
 1076: 	$currentstring .= '}';
 1077:     } 
 1078:     return $currentstring;
 1079: }
 1080: 
 1081: #-- <q> tag (end tag required)
 1082: sub start_q {
 1083:     my ($target,$token) = @_;
 1084:     my $currentstring = '';
 1085:     if ($target eq 'web') {
 1086: 	$currentstring .= $token->[4];
 1087:     } elsif ($target eq 'tex') {
 1088: 	$currentstring .= '\emph{';
 1089:     }
 1090:     return $currentstring;
 1091: }
 1092: 
 1093: sub end_q {
 1094:     my ($target,$token) = @_;
 1095:     my $currentstring = '';
 1096:     if ($target eq 'web') {
 1097: 	$currentstring .= $token->[2];
 1098:     } elsif ($target eq 'tex') {
 1099: 	$currentstring .= '}';
 1100:     } 
 1101:     return $currentstring;
 1102: }
 1103: 
 1104: #-- <p> tag (end tag optional)
 1105: #optional attribute - align="center|left|right"
 1106: sub start_p {
 1107:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1108:     my $currentstring = '';
 1109:     if ($target eq 'web') {
 1110: 	$currentstring .= $token->[4];
 1111:     } elsif ($target eq 'tex') {
 1112: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1113: 	if ($align eq 'center') {
 1114: 	    $currentstring='\begin{center}\par';
 1115: 	} elsif ($align eq 'right') {
 1116: 	    $currentstring='\makebox['.$ENV{'form.textwidth'}.']{\hfill\llap{';
 1117: 	} elsif ($align eq 'left') {
 1118: 	    $currentstring='\noindent\makebox['.$ENV{'form.textwidth'}.']{\rlap{';
 1119: 	} else {
 1120:             $currentstring='\par ';
 1121:         }
 1122: 	my $signal=1;#<p> does not work inside <b>...</b> 
 1123: 	foreach my $tag (@$tagstack) {if (lc($tag) eq 'b') {$signal=0;}
 1124: 	if (!$signal) {$currentstring = '';}
 1125: 	}
 1126:     }
 1127:     return $currentstring;
 1128: }
 1129: 
 1130: sub end_p {
 1131:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1132:     my $currentstring = '';
 1133:     if ($target eq 'web') {
 1134: 	$currentstring .= $token->[2];
 1135:     } elsif ($target eq 'tex') {
 1136: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1137: 	if ($align eq 'center') {		
 1138: 	    $currentstring .= '\end{center}';
 1139: 	} elsif ($align eq 'right') {
 1140: 	    $currentstring .= '}}';
 1141: 	} elsif ($align eq 'left') {
 1142: 	    $currentstring .= '}\hfill}';
 1143: 	}
 1144:     }
 1145:     return $currentstring;
 1146: }
 1147: 
 1148: #-- <br> tag (end tag forbidden)
 1149: sub start_br {
 1150:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1151:     my $currentstring = '';
 1152:     if ($target eq 'web') {
 1153: 	$currentstring .= $token->[4];
 1154:     } elsif ($target eq 'tex') {
 1155: 	my @tempo=@$tagstack;
 1156: 	my $signal=0;
 1157: 	for (my $i=$#tempo;$i>=0;$i--) {
 1158: 	    if (($tempo[$i] eq 'b') || ($tempo[$i] eq 'strong') ||
 1159:                 ($tempo[$i] eq 'ol') || ($tempo[$i] eq 'ul') ||
 1160:                 ($tempo[$i] eq 'td') || ($tempo[$i] eq 'th'))  {
 1161: 		$signal=1;
 1162: 		last;
 1163: 	    }
 1164: 	}
 1165: 	if ($signal) {
 1166: 	    $currentstring .= ' \vskip 0 mm ';
 1167: 	} elsif ($$tagstack[-2] ne 'sub' && $$tagstack[-2] ne 'sup') {
 1168: 	    $currentstring .= '\strut \\\\ \strut ';
 1169: 	}
 1170:     }
 1171:     return $currentstring;
 1172: }
 1173: 
 1174: sub end_br {
 1175:     my ($target,$token) = @_;
 1176:     my $currentstring = '';
 1177:     if ($target eq 'web') {
 1178: 	$currentstring .= $token->[2];
 1179:     }
 1180:     return $currentstring;
 1181: }
 1182: 
 1183: #-- <big> tag (end tag required)
 1184: sub start_big {
 1185:     my ($target,$token) = @_;
 1186:     my $currentstring = '';
 1187:     if ($target eq 'web') {
 1188: 	$currentstring .= $token->[4];
 1189:     } elsif ($target eq 'tex') {
 1190: 	$currentstring .= '{\large ';
 1191:     } 
 1192:     return $currentstring;
 1193: }
 1194: 
 1195: sub end_big {
 1196:     my ($target,$token) = @_;
 1197:     my $currentstring = '';
 1198:     if ($target eq 'web') {
 1199: 	$currentstring .= $token->[2];
 1200:     } elsif ($target eq 'tex') {
 1201: 	$currentstring .= '}';
 1202:     }
 1203:     return $currentstring;
 1204: }
 1205: 
 1206: #-- <small> tag (end tag required)
 1207: sub start_small {
 1208:     my ($target,$token) = @_;
 1209:     my $currentstring = '';
 1210:     if ($target eq 'web') {
 1211: 	$currentstring .= $token->[4];
 1212:     } elsif ($target eq 'tex') {
 1213: 	$currentstring .= '{\footnotesize ';
 1214:     }
 1215:     return $currentstring;
 1216: }
 1217: 
 1218: sub end_small {
 1219:     my ($target,$token) = @_;
 1220:     my $currentstring = '';
 1221:     if ($target eq 'web') {
 1222: 	$currentstring .= $token->[2];
 1223:     } elsif ($target eq 'tex') {
 1224: 	$currentstring .= '}';
 1225:     }
 1226:     return $currentstring;
 1227: }
 1228: 
 1229: #-- <basefont> tag (end tag forbidden)
 1230: sub start_basefont {
 1231:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1232:     my $currentstring = '';
 1233:     if ($target eq 'web') {
 1234: 	$currentstring = $token->[4];     
 1235:     } elsif ($target eq 'tex') {
 1236: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1237: 	if (defined $basesize) {
 1238: 	    $currentstring = '{\\'.$basesize.' ';
 1239: 	}
 1240:     }
 1241:     return $currentstring;
 1242: }
 1243: 
 1244: sub end_basefont {
 1245:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1246:     my $currentstring = '';
 1247:     if ($target eq 'web') {
 1248: 	$currentstring = $token->[4];     
 1249:     } elsif ($target eq 'tex') {
 1250: 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1251: 	if (defined $basesize) {
 1252: 	    $currentstring = '}';
 1253: 	}
 1254:     }
 1255:     return $currentstring;
 1256: }
 1257: 
 1258: #-- <font> tag (end tag required)
 1259: sub start_font {
 1260:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1261:     my $currentstring = '';
 1262:     if ($target eq 'web') {
 1263: 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
 1264: 	if ($face!~/symbol/i) {
 1265: 	    if (($ENV{'browser.fontenhance'} eq 'on') || 
 1266: 		($ENV{'browser.blackwhite'} eq 'on')) { return ''; }
 1267: 	}
 1268: 	$currentstring = $token->[4];     
 1269:     }  elsif ($target eq 'tex') {
 1270: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1271: 	if (defined $fontsize) {
 1272: 	    $currentstring = '{\\'.$fontsize.' ';
 1273: 	}
 1274:     }
 1275:     return $currentstring;
 1276: }
 1277: 
 1278: sub end_font {
 1279:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1280:     my $currentstring = '';
 1281:     if ($target eq 'web') {
 1282: 	$currentstring = $token->[2];    
 1283:     }  elsif ($target eq 'tex') {
 1284: 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 1285: 	if (defined $fontsize) {
 1286: 	    $currentstring = '}';
 1287: 	}
 1288:     }
 1289:     return $currentstring;
 1290: }
 1291:  
 1292: #-- <strike> tag (end tag required)
 1293: sub start_strike {
 1294:     my ($target,$token) = @_;
 1295:     my $currentstring = '';
 1296:     if ($target eq 'web') {
 1297: 	$currentstring .= $token->[4];
 1298:     } elsif ($target eq 'tex') {
 1299: 	&Apache::lonxml::startredirection();
 1300:     } 
 1301:     return $currentstring;
 1302: }
 1303: 
 1304: sub end_strike {
 1305:     my ($target,$token) = @_;
 1306:     my $currentstring = '';
 1307:     if ($target eq 'web') {
 1308: 	$currentstring .= $token->[2];
 1309:     } elsif ($target eq 'tex') {
 1310: 	$currentstring=&Apache::lonxml::endredirection();
 1311: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g; 
 1312: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/; 
 1313: 	$currentstring=~s/(\S)\s*$/$1\}/;
 1314:     }
 1315:     return $currentstring;
 1316: }
 1317: 
 1318: #-- <s> tag (end tag required)
 1319: sub start_s {
 1320:     my ($target,$token) = @_;
 1321:     my $currentstring = '';
 1322:     if ($target eq 'web') {
 1323: 	$currentstring .= $token->[4];
 1324:     } elsif ($target eq 'tex') {
 1325: 	&Apache::lonxml::startredirection();
 1326:     } 
 1327:     return $currentstring;
 1328: }
 1329: 
 1330: sub end_s {
 1331:     my ($target,$token) = @_;
 1332:     my $currentstring = '';
 1333:     if ($target eq 'web') {
 1334: 	$currentstring .= $token->[2];
 1335:     } elsif ($target eq 'tex') {
 1336: 	$currentstring=&Apache::lonxml::endredirection();
 1337: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1338: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1339: 	$currentstring=~s/(\S)\s*$/$1\}/;	
 1340:     }
 1341:     return $currentstring;
 1342: }
 1343: 
 1344: #-- <sub> tag (end tag required)
 1345: sub start_sub {
 1346:     my ($target,$token) = @_;
 1347:     my $currentstring = '';
 1348:     if ($target eq 'web') {
 1349: 	$currentstring .= $token->[4];
 1350:     } elsif ($target eq 'tex') {
 1351: 	$currentstring .= '\ensuremath{_{';
 1352:     } 
 1353:     return $currentstring;
 1354: }
 1355: 
 1356: sub end_sub {
 1357:     my ($target,$token) = @_;
 1358:     my $currentstring = '';
 1359:     if ($target eq 'web') {
 1360: 	$currentstring .= $token->[2];
 1361:     } elsif ($target eq 'tex') {
 1362: 	$currentstring .= '}}';
 1363:     }
 1364:     return $currentstring;
 1365: }
 1366: 
 1367: #-- <sup> tag (end tag required)
 1368: sub start_sup {
 1369:     my ($target,$token) = @_;
 1370:     my $currentstring = '';
 1371:     if ($target eq 'web') {
 1372: 	$currentstring .= $token->[4];
 1373:     } elsif ($target eq 'tex') {
 1374: 	$currentstring .= '\ensuremath{^{';
 1375:     } 
 1376:     return $currentstring;
 1377: }
 1378: 
 1379: sub end_sup {
 1380:     my ($target,$token) = @_;
 1381:     my $currentstring = '';
 1382:     if ($target eq 'web') {
 1383: 	$currentstring .= $token->[2];
 1384:     } elsif ($target eq 'tex') {
 1385: 	$currentstring .= '}}';
 1386:     }
 1387:     return $currentstring;
 1388: }
 1389: 
 1390: #-- <hr> tag (end tag forbidden)
 1391: sub start_hr {
 1392:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1393:     my $currentstring = '';
 1394:     if ($target eq 'web') {
 1395: 	$currentstring .= $token->[4];
 1396:     } elsif ($target eq 'tex') {
 1397: 	my $LaTeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1398: 	if (defined $LaTeXwidth) {
 1399: 	    if ($LaTeXwidth=~/^%/) {
 1400: 		substr($LaTeXwidth,0,1)='';
 1401: 		$LaTeXwidth=($LaTeXwidth/100).'\textwidth';
 1402: 	    }
 1403: 	} else {
 1404: 	    $LaTeXwidth ='0.9\textwidth';
 1405: 	}
 1406: 	my ($pre,$post);
 1407: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 1408: 	if (($align eq 'center') || (not defined $align)) {
 1409: 	    $pre=''; $post='';
 1410: 	} elsif ($align eq 'left') {
 1411: 	    $pre='\rlap{'; $post='} \hfill';
 1412: 	} elsif ($align eq 'right') {
 1413: 	    $pre=' \hfill \llap{'; $post='}';
 1414: 	}
 1415: 	$currentstring .= ' \vskip 0 mm \noindent\makebox['.$LaTeXwidth.']{'.$pre.'\makebox['.
 1416:                                     $LaTeXwidth.'][b]{\hrulefill}'.$post.'}\vskip 0 mm ';
 1417:     } 
 1418:     return $currentstring;
 1419: }
 1420: 
 1421: sub end_hr {
 1422:     my ($target,$token) = @_;
 1423:     my $currentstring = '';
 1424:     if ($target eq 'web') {
 1425: 	$currentstring .= $token->[2];
 1426:     }
 1427:     return $currentstring;
 1428: }
 1429: 
 1430: #-- <div> tag (end tag required)
 1431: sub start_div {
 1432:     my ($target,$token) = @_;
 1433:     my $currentstring = '';
 1434:     if ($target eq 'web') {
 1435: 	$currentstring .= $token->[4];
 1436:     } 
 1437:     return $currentstring;
 1438: }
 1439: 
 1440: sub end_div {
 1441:     my ($target,$token) = @_;
 1442:     my $currentstring = '';
 1443:     if ($target eq 'web') {
 1444: 	$currentstring .= $token->[2];
 1445:     } 
 1446:     return $currentstring;
 1447: }
 1448: 
 1449: #-- <a> tag (end tag required)
 1450: sub start_a {
 1451:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1452:     my $currentstring = '';
 1453:     if ($target eq 'web') {
 1454: 	$currentstring .= $token->[4];
 1455:     } elsif ($target eq 'tex') {
 1456: 	my $a=&Apache::lonxml::get_param('href',$parstack,$safeeval,undef,1);
 1457: 	my $b=&Apache::lonxml::get_param('name',$parstack,$safeeval,undef,1);
 1458: 	if ($a=~/\S/) {
 1459: 	    $a=~s/([^\\])%/$1\\\%/g;
 1460: 	    $currentstring .= '\ref{URI: '.$a.'}';
 1461: 	} elsif ($b=~/\S/) {
 1462: 	    $currentstring .= '\ref{Anchor: '.$b.'}';
 1463: 	} else {
 1464: 	    $currentstring.='';
 1465: 	}
 1466:     }
 1467:     return $currentstring;
 1468: }
 1469: 
 1470: sub end_a {
 1471:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1472:     my $currentstring = '';
 1473:     if ($target eq 'web') {
 1474: 	$currentstring .= $token->[2];
 1475:     }
 1476:     return $currentstring;
 1477: }
 1478: 
 1479: #-- <li> tag (end tag optional)
 1480: sub start_li {
 1481:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1482:     my $currentstring = '';
 1483:     if ($target eq 'web') {
 1484: 	$currentstring = $token->[4];     
 1485:     } elsif ($target eq 'tex') {
 1486: 	my  $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,1);
 1487: 	if ($type=~/circle/) {
 1488: 	    $currentstring .= ' \item[o] ';
 1489: 	} elsif ($type=~/square/) {
 1490: 	    $currentstring .= ' \item[$\Box$] ';
 1491: 	} elsif ($type ne '') { 
 1492: 	    $currentstring .= ' \item['.$type.'] ';
 1493: 	} else {
 1494: 	    $currentstring .= ' \item ';
 1495: 	}  
 1496:     } 
 1497:     return $currentstring;
 1498: }
 1499: 
 1500: sub end_li {
 1501:     my ($target,$token) = @_;
 1502:     my $currentstring = '';
 1503:     if ($target eq 'web') {
 1504: 	$currentstring = $token->[2];     
 1505:     } 
 1506:     return $currentstring;
 1507: }
 1508: 
 1509: #-- <u> tag (end tag required)
 1510: sub start_u {
 1511:     my ($target,$token) = @_;
 1512:     my $currentstring = '';
 1513:     if ($target eq 'web') {
 1514: 	$currentstring .= $token->[4];
 1515:     } elsif ($target eq 'tex') {
 1516: 	&Apache::lonxml::startredirection();
 1517:     } 
 1518:     return $currentstring;
 1519: }
 1520: 
 1521: sub end_u {
 1522:     my ($target,$token) = @_;
 1523:     my $currentstring = '';
 1524:     if ($target eq 'web') {
 1525: 	$currentstring .= $token->[2];
 1526:     } elsif ($target eq 'tex') {
 1527: 	$currentstring=&Apache::lonxml::endredirection();
 1528: 	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
 1529: 	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 1530: 	$currentstring=~s/(\S)\s*$/$1\}/;		
 1531:     }
 1532:     return $currentstring;
 1533: }
 1534: 
 1535: #-- <ul> tag (end tag required)
 1536: sub start_ul {
 1537:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1538:     my $currentstring = '';
 1539:     if ($target eq 'web') {
 1540: 	$currentstring = $token->[4];     
 1541:     } elsif ($target eq 'tex') {
 1542: 	my $TeXtype=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1543: 	if ($TeXtype eq 'disc') {
 1544: 	    $currentstring .= '\renewcommand{\labelitemi}{$\bullet$}'.
 1545:                               '\renewcommand{\labelitemii}{$\bullet$}'. 
 1546:                               '\renewcommand{\labelitemiii}{$\bullet$}'.
 1547:                               '\renewcommand{\labelitemiv}{$\bullet$}';
 1548: 	} elsif ($TeXtype eq 'circle') {
 1549: 	    $currentstring .= '\renewcommand{\labelitemi}{$\circ$}'.
 1550:                               '\renewcommand{\labelitemii}{$\circ$}'. 
 1551:                               '\renewcommand{\labelitemiii}{$\circ$}'.
 1552:                               '\renewcommand{\labelitemiv}{$\circ$}';
 1553: 	} elsif ($TeXtype eq 'square') {
 1554: 	    $currentstring .= '\renewcommand{\labelitemi}{$\diamond$}'.
 1555:                               '\renewcommand{\labelitemii}{$\diamond$}'. 
 1556:                               '\renewcommand{\labelitemiii}{$\diamond$}'.
 1557:                               '\renewcommand{\labelitemiv}{$\diamond$}';
 1558: 	}
 1559: 	$currentstring .= '\strut \begin{itemize}';  
 1560:     } 
 1561:     return $currentstring;
 1562: }
 1563: 
 1564: sub end_ul {
 1565:     my ($target,$token) = @_;
 1566:     my $currentstring = '';
 1567:     if ($target eq 'web') {
 1568: 	$currentstring = $token->[2];     
 1569:     } elsif ($target eq 'tex') {
 1570: 	$currentstring = '\end{itemize} \renewcommand{\labelitemi}{$\bullet$}'.
 1571:                                '\renewcommand{\labelitemii}{$\bullet$}'. 
 1572:                                '\renewcommand{\labelitemiii}{$\bullet$}'.
 1573:                                '\renewcommand{\labelitemiv}{$\bullet$}\strut ';  
 1574:     } 
 1575:     return $currentstring;
 1576: }
 1577: 
 1578: #-- <menu> tag (end tag required)
 1579: sub start_menu {
 1580:     my ($target,$token) = @_;
 1581:     my $currentstring = '';
 1582:     if ($target eq 'web') {
 1583: 	$currentstring = $token->[4];     
 1584:     } elsif ($target eq 'tex') {
 1585: 	$currentstring = " \\begin{itemize} ";  
 1586:     } 
 1587:     return $currentstring;
 1588: }
 1589: 
 1590: sub end_menu {
 1591:     my ($target,$token) = @_;
 1592:     my $currentstring = '';
 1593:     if ($target eq 'web') {
 1594: 	$currentstring = $token->[2];     
 1595:     } elsif ($target eq 'tex') {
 1596: 	$currentstring = " \\end{itemize}";  
 1597:     } 
 1598:     return $currentstring;
 1599: }
 1600: 
 1601: #-- <dir> tag (end tag required)
 1602: sub start_dir {
 1603:     my ($target,$token) = @_;
 1604:     my $currentstring = '';
 1605:     if ($target eq 'web') {
 1606: 	$currentstring = $token->[4];     
 1607:     } elsif ($target eq 'tex') {
 1608: 	$currentstring = " \\begin{itemize} ";  
 1609:     } 
 1610:     return $currentstring;
 1611: }
 1612: 
 1613: sub end_dir {
 1614:     my ($target,$token) = @_;
 1615:     my $currentstring = '';
 1616:     if ($target eq 'web') {
 1617: 	$currentstring = $token->[2];     
 1618:     } elsif ($target eq 'tex') {
 1619: 	$currentstring = " \\end{itemize}";  
 1620:     } 
 1621:     return $currentstring;
 1622: }
 1623: 
 1624: #-- <ol> tag (end tag required)
 1625: sub start_ol {
 1626:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1627:     my $currentstring = '';
 1628:     if ($target eq 'web') {
 1629: 	$currentstring = $token->[4];     
 1630:     } elsif ($target eq 'tex') {
 1631: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 1632: 	if ($type eq '1') {
 1633: 	    $currentstring .= '\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1634:                               '\renewcommand{\labelenumii}{\arabic{enumii}.}'. 
 1635:                               '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1636:                               '\renewcommand{\labelenumiv}{\arabic{enumiv}.}';
 1637: 	} elsif ($type eq 'A') {
 1638: 	    $currentstring .= '\renewcommand{\labelenumi}{\Alph{enumi}.}'.
 1639:                               '\renewcommand{\labelenumii}{\Alph{enumii}.}'. 
 1640:                               '\renewcommand{\labelenumiii}{\Alph{enumiii}.}'.
 1641:                               '\renewcommand{\labelenumiv}{\Alph{enumiv}.}';
 1642: 	} elsif ($type eq 'a') {
 1643: 	    $currentstring .= '\renewcommand{\labelenumi}{\alph{enumi}.}'.
 1644:                               '\renewcommand{\labelenumii}{\alph{enumii}.}'.
 1645:                               '\renewcommand{\labelenumiii}{\alph{enumiii}.}'.
 1646:                               '\renewcommand{\labelenumiv}{\alph{enumiv}.}';
 1647: 	} elsif ($type eq 'i') {
 1648: 	    $currentstring .= '\renewcommand{\labelenumi}{\roman{enumi}.}'.
 1649:                               '\renewcommand{\labelenumii}{\roman{enumii}.}'.
 1650:                               '\renewcommand{\labelenumiii}{\roman{enumiii}.}'.
 1651:                               '\renewcommand{\labelenumiv}{\roman{enumiv}.}';
 1652: 	} elsif ($type eq 'I') {
 1653: 	    $currentstring .= '\renewcommand{\labelenumi}{\Roman{enumi}.}'.
 1654:                               '\renewcommand{\labelenumii}{\Roman{enumii}.}'.
 1655:                               '\renewcommand{\labelenumiii}{\Roman{enumiii}.}'.
 1656:                               '\renewcommand{\labelenumiv}{\Roman{enumiv}.}';
 1657: 	}
 1658: 	$currentstring .= '\strut \begin{enumerate}';  
 1659:     } 
 1660:     return $currentstring;
 1661: }
 1662: 
 1663: sub end_ol {
 1664:     my ($target,$token) = @_;
 1665:     my $currentstring = '';
 1666:     if ($target eq 'web') {
 1667: 	$currentstring = $token->[2];     
 1668:     } elsif ($target eq 'tex') {
 1669: 	$currentstring = '\end{enumerate}\renewcommand{\labelenumi}{\arabic{enumi}.}'.
 1670:                                         '\renewcommand{\labelenumii}{\arabic{enumii}.}'.
 1671:                                         '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
 1672:                                         '\renewcommand{\labelenumiv}{\arabic{enumiv}.}\strut ';  
 1673:     } 
 1674:     return $currentstring;
 1675: }
 1676: 
 1677: #-- <dl> tag (end tag required)
 1678: sub start_dl {
 1679:     my ($target,$token) = @_;
 1680:     my $currentstring = '';
 1681:     if ($target eq 'web') {
 1682: 	$currentstring = $token->[4];     
 1683:     } elsif ($target eq 'tex') {
 1684: 	$currentstring = '\begin{description}';
 1685: 	@Apache::londefdef::description=();
 1686: 	$Apache::londefdef::DD_redirection=0;
 1687: 	$Apache::londefdef::DT_redirection=0;
 1688:     } 
 1689:     return $currentstring;
 1690: }
 1691: 
 1692: sub end_dl {
 1693:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1694:     my $currentstring = '';
 1695:     if ($target eq 'web') {
 1696: 	$currentstring = $token->[2];     
 1697:     } elsif ($target eq 'tex') {
 1698: 	if ($Apache::londefdef::DT_redirection) {
 1699: 	    my $data=&item_cleanup;
 1700: 	    push @Apache::londefdef::description,'\item['.$data.']';
 1701: 	    $Apache::londefdef::DT_redirection=0;
 1702: 	} elsif ($Apache::londefdef::DD_redirection) {
 1703: 	    $Apache::londefdef::description[-1].=&Apache::lonxml::endredirection();
 1704: 	}
 1705: 	foreach my $element (@Apache::londefdef::description) {
 1706: 	    $currentstring.=' '.$element.' ';
 1707: 	}
 1708: 	@Apache::londefdef::description=();
 1709: 	$currentstring.='\end{description}';  
 1710:     } 
 1711:     return $currentstring;
 1712: }
 1713: 
 1714: #-- <dt> tag (end tag optional)
 1715: sub start_dt {
 1716:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1717:     my $currentstring='';
 1718:     if ($target eq 'web') {
 1719: 	$currentstring = $token->[4];     
 1720:     } elsif ($target eq 'tex') {
 1721: 	if ($Apache::londefdef::DT_redirection) {
 1722: 	    my $data=&item_cleanup;
 1723: 	    push @Apache::londefdef::description,'\item['.$data.']';
 1724: 	    $Apache::londefdef::DT_redirection=0;
 1725: 	} elsif ($Apache::londefdef::DD_redirection) {
 1726: 	    $Apache::londefdef::description[-1].=&Apache::lonxml::endredirection();
 1727: 	    $Apache::londefdef::DD_redirection=0;
 1728: 	}
 1729: 	&Apache::lonxml::startredirection();
 1730: 	$Apache::londefdef::DT_redirection=1;
 1731:     } 
 1732:     return $currentstring;
 1733: }
 1734: 
 1735: sub end_dt {
 1736:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1737:     my $currentstring = '';
 1738:     if ($target eq 'web') {
 1739: 	$currentstring = $token->[2];    
 1740:     } elsif ($target eq 'tex') {
 1741: 	my $data=&item_cleanup;
 1742: 	push @Apache::londefdef::description,'\item['.$data.']';
 1743: 	$Apache::londefdef::DT_redirection=0;
 1744:     } 
 1745:     return $currentstring;
 1746: }
 1747: 
 1748: sub item_cleanup {
 1749:     my $item=&Apache::lonxml::endredirection();
 1750:     $item=~s/\\begin{center}//g;
 1751:     $item=~s/\\end{center}//g;
 1752:     return $item;
 1753: }
 1754: 
 1755: #-- <dd> tag (end tag optional)
 1756: sub start_dd {
 1757:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1758:     my $currentstring = '';
 1759:     if ($target eq 'web') {
 1760: 	$currentstring = $token->[4];     
 1761:     } elsif ($target eq 'tex') {
 1762: 	if ($Apache::londefdef::DT_redirection) {
 1763: 	    my $data=&item_cleanup;
 1764: 	    push @Apache::londefdef::description,'\item['.$data.']';
 1765: 	    $Apache::londefdef::DT_redirection=0;
 1766: 	}
 1767: 	$Apache::londefdef::DD_redirection=1;
 1768: 	&Apache::lonxml::startredirection();
 1769:     } 
 1770:     return $currentstring;
 1771: }
 1772: 
 1773: sub end_dd {
 1774:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1775:     my $currentstring = '';
 1776:     if ($target eq 'web') {
 1777: 	$currentstring = $token->[2];    
 1778:     }  elsif ($target eq 'tex') {
 1779: 	$Apache::londefdef::description[-1].=&Apache::lonxml::endredirection();
 1780: 	$Apache::londefdef::DD_redirection=0;
 1781:     }
 1782:     return $currentstring;
 1783: }
 1784: 
 1785: #-- <table> tag (end tag required)
 1786: #list of supported attributes: border,width,TeXwidth
 1787: sub start_table {
 1788:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1789:     my ($textwidth,$currentstring)=('','');
 1790:     if ($target eq 'web') {
 1791: 	$currentstring = $token->[4];     
 1792:     } elsif ($target eq 'tex') {
 1793: 	my $aa = {};
 1794: 	push @Apache::londefdef::table, $aa; 
 1795: 	$Apache::londefdef::table[-1]{'row_number'} = -1;
 1796:         #maximum table's width (default coincides with text line length)
 1797: 	if ($#Apache::londefdef::table==0) {
 1798: 	    $textwidth=&recalc($ENV{'form.textwidth'}); #result is always in mm
 1799: 	    $textwidth=~/(\d+\.?\d*)/;
 1800: 	    $textwidth=0.95*$1; #accounts "internal" LaTeX space for table frame
 1801: 	} else {
 1802: 	    if ($Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]=~/\d/) {
 1803: 		#the maximum width of nested table is determined by LATeX width of parent cell
 1804: 		$textwidth=$Apache::londefdef::table[-2]{'TeXlen'}[$Apache::londefdef::table[-2]{'row_number'}][$Apache::londefdef::table[-2]{'counter_columns'}]; 
 1805: 	    } else {
 1806:               #try to use all space not used before (minus 5% for LaTeX table internal) - rather silly
 1807: 		$textwidth=$Apache::londefdef::table[-2]{'width'};
 1808: 		for (my $i=0;$i<$Apache::londefdef::table[-2]{'counter_columns'};$i++) {
 1809: 		    $textwidth=$textwidth-$Apache::londefdef::table[-2]{'TeXlen'}[0][$i];
 1810: 		}
 1811: 	    }
 1812: 	}
 1813: 	my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 1814: 	if (not defined $TeXwidth) {
 1815: 	    my $htmlwidth = &Apache::lonxml::get_param('width',$parstack,$safeeval,undef,1);
 1816: 	    if ($htmlwidth=~/%/) {
 1817:                 $Apache::londefdef::table[-1]{'percent'}=1;
 1818: 		$htmlwidth=~/(\d+)/;
 1819: 		$Apache::londefdef::table[-1]{'width'}=$1*$textwidth/100;;
 1820: 	    } else {
 1821: 		$Apache::londefdef::table[-1]{'width'}=$textwidth;
 1822: 	    }
 1823: 	} elsif ($TeXwidth=~/%/) {
 1824: 	    $Apache::londefdef::table[-1]{'percent'}=1;
 1825: 	    $TeXwidth=~/(\d+)/;
 1826:             $Apache::londefdef::table[-1]{'width'}=$1*$textwidth/100;
 1827: 	} else {
 1828: 	    $Apache::londefdef::table[-1]{'forcetablewidth'}=1;
 1829: 	    $Apache::londefdef::table[-1]{'width'}=$TeXwidth;
 1830: 	}        
 1831:         #table's border
 1832: 	my $border = &Apache::lonxml::get_param('border',$parstack,$safeeval); 
 1833:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 1834: 	unless (defined $border) { $border = 0; }
 1835: 	if ($border) { 
 1836: 	    $Apache::londefdef::table[-1]{'hinc'} = '\hline '; 
 1837: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 1838: 	    $Apache::londefdef::table[-1]{'vvinc'} = '|';
 1839: 	} else {
 1840: 	    $Apache::londefdef::table[-1]{'hinc'} = ''; 
 1841: 	    $Apache::londefdef::table[-1]{'vinc'} = '&'; 
 1842: 	    $Apache::londefdef::table[-1]{'vvinc'} = '';
 1843: 	}
 1844: 	if ($#Apache::londefdef::table==0) {
 1845: 	    $Apache::londefdef::table[-1]{'output'}='\strut\newline\strut\setlength{\tabcolsep}{1 mm}';
 1846: 	}
 1847: 	$Apache::londefdef::table[-1]{'output'}.=' \noindent \begin{tabular} ';
 1848:         $Apache::londefdef::table[-1]{'TeXlen'}=[];
 1849:         $Apache::londefdef::table[-1]{'objectlen'}=[];
 1850:         $Apache::londefdef::table[-1]{'objectsignal'}=[];
 1851:         $Apache::londefdef::table[-1]{'maxlen'}=[];
 1852:         $Apache::londefdef::table[-1]{'minlen'}=[];
 1853:         $Apache::londefdef::table[-1]{'content'}=[];
 1854:         $Apache::londefdef::table[-1]{'align'}=[];
 1855:         $currentstring='\keephidden{NEW TABLE ENTRY}';
 1856:    }
 1857:     return $currentstring;
 1858: }
 1859:  
 1860: sub end_table {
 1861:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 1862:     my $currentstring = '';
 1863:     if ($target eq 'web') {
 1864: 	$currentstring = $token->[2];     
 1865:     } elsif ($target eq 'tex') {
 1866: 	my $inmemory = '';
 1867: 	my $output = '';
 1868: 	my $WARNING='';
 1869:         #width of columns from TeXwidth attributes
 1870: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 1871: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 1872: 		if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]<$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn]) {
 1873: 		    $Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]=$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn];
 1874: 		}	
 1875: 	    }
 1876: 	}
 1877:         #free space and number of empty columns
 1878: 	my ($available_space,$empty_columns)=($Apache::londefdef::table[-1]{'width'},0);
 1879: 	if ($#Apache::londefdef::table ne 0) {$available_space=0.9*$available_space;} 
 1880: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 1881: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]==0) {
 1882: 		$empty_columns++;
 1883: 	    } else {
 1884: 		$available_space=$available_space-$Apache::londefdef::table[-1]{'TeXlen'}[0][$jn];
 1885: 	    }
 1886: 	}
 1887:         #boundaries for contents columns
 1888: 	my @min_len=();#columns can not be narrower 
 1889: 	my @max_len=();#maximum length of column
 1890: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 1891: 		my ($localmin,$localmax)=(0,0);
 1892: 		for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 1893: 		    if ($localmin<$Apache::londefdef::table[-1]{'minlen'}[$in][$jn]) {
 1894: 			$localmin=$Apache::londefdef::table[-1]{'minlen'}[$in][$jn];
 1895: 		    }
 1896: 		    if ($localmax<$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn]) {
 1897: 			$localmax=$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn];
 1898: 		    }
 1899: 		}
 1900: 		push @min_len, $localmin;
 1901: 		push @max_len, $localmax;
 1902: 	}
 1903: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 1904: 	    my $localmin=0,;
 1905: 	    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 1906: 		if ($localmin<$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn]) {
 1907: 		    $localmin=$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn];
 1908: 		}
 1909: 	    }
 1910: 	    if ($max_len[$jn]<$localmin) {
 1911: 		$max_len[$jn]=$localmin;
 1912: 	    	$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 1913: 	    }#object size is bigger
 1914: 	    if ($min_len[$jn]<$localmin) {
 1915: 		$min_len[$jn]=$localmin;
 1916: 		$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
 1917: 	    }#object size is bigger
 1918: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]!=0) {
 1919: 		$min_len[$jn]=0;
 1920: 		$max_len[$jn]=0;
 1921: 	    }
 1922: 	}
 1923:        #final adjustment of column width
 1924: 	my @fwidth=@{$Apache::londefdef::table[-1]{'TeXlen'}[0]};#final width array
 1925: 	my @adjust=();
 1926:         #step 1. adjustment by maximum value
 1927: 	my $space_neeeded=0;
 1928: 	for (my $jn=0;$jn<=$#max_len;$jn++) {
 1929: 	    $space_neeeded=$space_neeeded+$max_len[$jn];
 1930: 	}
 1931: 	if ($space_neeeded<=$available_space) {
 1932: 	    for (my $jn=0;$jn<=$#max_len;$jn++) {
 1933: 		if ($fwidth[$jn]==0) {
 1934: 		    $fwidth[$jn]=$max_len[$jn];
 1935: 		}
 1936: 	    }
 1937: 	} else {
 1938:         #step 2. adjustment by minimum value (estimation)
 1939: 	    $space_neeeded=0;
 1940: 	    for (my $jn=0;$jn<=$#min_len;$jn++) {
 1941: 		$space_neeeded+=$min_len[$jn];
 1942: 	    }
 1943: 	    if ($space_neeeded>$available_space) {
 1944: 		$WARNING=' \textbf{NOT ENOUGH SPACE FOR TABLE} ';
 1945: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 1946: 		    if ($fwidth[$jn]==0) {
 1947: 			$fwidth[$jn]=$min_len[$jn];
 1948: 		    }
 1949: 		}
 1950: 		#check if we have objects which can be scaled
 1951: 		my $how_many_to_scale=0;
 1952: 		my @to_scale=();
 1953: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
 1954: 		    if ($Apache::londefdef::table[-1]{'objectsignal'}[$jn] eq '1') {
 1955: 			$how_many_to_scale++;
 1956: 			push @to_scale, $jn;
 1957: 		    }
 1958: 		}
 1959: 		if ($how_many_to_scale>0) {
 1960: 		    my $space_to_adjust=($space_neeeded-$available_space)/$how_many_to_scale;
 1961: 		    foreach my $jn (@to_scale) {
 1962: 			for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 1963: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/;
 1964: 			    if ($1 ne '') {
 1965: 				my $current_length=&recalc($1);
 1966: 				$current_length=~/(\d+\.?\d*)/;
 1967: 				$current_length=$current_length-$space_to_adjust;
 1968: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/width=$current_length mm/;
 1969: 			    }
 1970: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/\[(\d+\.?\d*)\s*mm\]/;
 1971: 			    if ($1 ne '') {
 1972: 				my $current_length=$1;
 1973: 				$current_length=$current_length-$space_to_adjust;
 1974: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/\[(\d+\.?\d*)\s*mm\]/\[$current_length mm\]/;
 1975: 			    }				
 1976: 			}
 1977: 			$fwidth[$jn]=$fwidth[$jn]-$space_to_adjust;
 1978: 		    }
 1979: 		}
 1980: 	    } else {
 1981: 	      #step 3. adjustment over minimal + corrections
 1982: 		my $enlarge_coef=$available_space/$space_neeeded;
 1983: 		my $acsessive=0;
 1984: 		for (my $jn=0;$jn<=$#min_len;$jn++) {
 1985: 		    $adjust[$jn]=$min_len[$jn]*$enlarge_coef;
 1986: 		    if ($adjust[$jn]>$max_len[$jn]) {
 1987: 			$fwidth[$jn]=$max_len[$jn];
 1988: 			$acsessive=$acsessive+$adjust[$jn]-$max_len[$jn];
 1989: 			$adjust[$jn]=0;
 1990: 		    }
 1991: 		}
 1992: 		if ($acsessive>0) {
 1993: 		#we have an excess of space and can redistribute it
 1994: 		    my $notempty_columns=0;
 1995: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 1996: 			if ($adjust[$jn]!=0) {
 1997: 			    $notempty_columns++;
 1998: 			}
 1999: 		    }
 2000: 		    my $per_column=$acsessive/$notempty_columns;
 2001: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2002: 			if ($adjust[$jn]!=0) {
 2003: 			    $adjust[$jn]+=$per_column;
 2004: 			    $fwidth[$jn]=$adjust[$jn];
 2005: 			}
 2006: 		    }
 2007: 		} else {
 2008: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
 2009: 			$fwidth[$jn]=$adjust[$jn];
 2010: 		    }
 2011: 		}
 2012: 	    }
 2013: 	}
 2014:         #use all available width if it is defined in % or as TeXwidth
 2015:         if (($Apache::londefdef::table[-1]{'percent'}==1) || ($Apache::londefdef::table[-1]{'forcetablewidth'}==1)) {
 2016: 	    my $current=0; 
 2017: 	    for (my $i=0;$i<=$#fwidth;$i++) {  
 2018: 		$current+=$fwidth[$i];
 2019: 	    }
 2020: 	    my $coef=$Apache::londefdef::table[-1]{'width'}/$current;
 2021: 	    for (my $i=0;$i<=$#fwidth;$i++) {  
 2022: 		$fwidth[$i]*=$coef;
 2023: 	    }
 2024: 	}
 2025:         #removing of empty columns if allowed
 2026:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
 2027: 	if ($permission eq 'yes') {
 2028: 	    my @cleaned_table=();
 2029:             my @cleaned_header=();
 2030: 	    my $colind=0;
 2031: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
 2032: 		if ($fwidth[$jn]!=0) {
 2033: 		    #we need to copy column
 2034: 		    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2035: 			$cleaned_table[$in][$colind]=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2036: 			$cleaned_header[$colind]=$fwidth[$jn];
 2037: 		    }
 2038: 		    $colind++;
 2039: 		}
 2040: 	    }
 2041: 	    $Apache::londefdef::table[-1]{'content'}=\@cleaned_table;
 2042: 	    @fwidth=@cleaned_header;
 2043: 	}
 2044: 	#construct header of the table
 2045: 	my $header_of_table = '{'.$Apache::londefdef::table[-1]{'vvinc'};
 2046: 	for (my $in=0;$in<=$#fwidth;$in++) {
 2047: 	    $header_of_table.='p{'.$fwidth[$in].' mm}'.$Apache::londefdef::table[-1]{'vvinc'};
 2048: 	}
 2049: 	$header_of_table .= '}';
 2050: 	#fill the table
 2051: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
 2052: 	    for (my $jn=0;$jn<=$#fwidth;$jn++) {
 2053: 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2054: 		    $output.='\vspace*{-6 mm}\begin{center}';
 2055: 		} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2056: 		    $output.=' \hfill \llap{'
 2057: 		}
 2058: 		$output.=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
 2059: 		if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
 2060: 		    $output.='\end{center}\vspace*{-6 mm}';
 2061: 		} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
 2062: 		    $output.='} ';
 2063: 		}
 2064:                 if ($jn!=$#fwidth) {$output.=' '.$Apache::londefdef::table[-1]{'vinc'};}
 2065: 	    }
 2066: 	    $output.=' \\\\ '.$Apache::londefdef::table[-1]{'hinc'}.' ';
 2067: 	}
 2068: 	$Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut\newline\strut ';
 2069: 	if ($#Apache::londefdef::table > 0) {	    
 2070: 	    my $inmemory = $Apache::londefdef::table[-1]{'output'};
 2071: 	    pop @Apache::londefdef::table;
 2072: 	    push @{$Apache::londefdef::table[-1]{'include'}}, $inmemory;
 2073: 	} else {
 2074: 	    $currentstring .= $Apache::londefdef::table[-1]{'output'};
 2075: 	    pop @Apache::londefdef::table;
 2076: 	    undef @Apache::londefdef::table;
 2077: 	}
 2078:     }
 2079:     return $currentstring;
 2080: }
 2081: 
 2082: #-- <tr> tag (end tag optional)
 2083: sub start_tr {
 2084:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2085:     my $currentstring = '';
 2086:     if ($target eq 'web') {
 2087: 	$currentstring = $token->[4];     
 2088:     } elsif ($target eq 'tex') {
 2089: 	$Apache::londefdef::table[-1]{'row_number'}++;
 2090: 	my $alignchar=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
 2091: 	if ($alignchar ne '') {
 2092: 	    push @ {$Apache::londefdef::table[-1]{'rows'} },substr($alignchar,0,1);
 2093: 	} else {
 2094: 	    push @ {$Apache::londefdef::table[-1]{'rows'} }, 'l';
 2095: 	}
 2096: 	push ( @{ $Apache::londefdef::table[-1]{'rowdata'} }, $Apache::londefdef::table[-1]{'hinc'});
 2097: 	$Apache::londefdef::table[-1]{'counter_columns'} = -1;
 2098: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}}, [];
 2099: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}}, [];
 2100: 	push @ {$Apache::londefdef::table[-1]{'minlen'}}, [];
 2101: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}}, [];
 2102: 	push @ {$Apache::londefdef::table[-1]{'content'}}, [];
 2103:     } 
 2104:     return $currentstring;
 2105: }
 2106:         
 2107: sub end_tr {
 2108:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2109:     my $currentstring = '';
 2110:     if ($target eq 'web') {
 2111: 	$currentstring = $token->[2];     
 2112:     } elsif ($target eq 'tex') {
 2113: 	if ($Apache::londefdef::TD_redirection) {
 2114: 	    &end_td_tex($parstack,$parser,$safeeval);    
 2115: 	}
 2116:     }
 2117:     return $currentstring;
 2118: }
 2119: 
 2120: #-- <td> tag (end tag optional)
 2121: sub start_td {
 2122:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2123:     my $currentstring = '';
 2124:     if ($target eq 'web') {
 2125: 	$currentstring = $token->[4];     
 2126:     } elsif ($target eq 'tex') {
 2127: 	$Apache::londefdef::TD_redirection = 1;
 2128: 	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
 2129:     } 
 2130:     return $currentstring;
 2131: }   
 2132:     
 2133: sub tag_check {
 2134:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2135:     my @ar=@$parstack; 
 2136:     for (my $i=$#ar-1;$i>=0;$i--) {
 2137: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2138: 	    &start_td_tex($parstack,$parser,$safeeval);
 2139: 	    last;
 2140: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2141: 	    splice @ar, $i+1;
 2142: 	    &end_td_tex(\@ar,$parser,$safeeval);
 2143: 	    &start_td_tex($parstack,$parser,$safeeval);
 2144: 	    last;
 2145: 	}
 2146:     }
 2147:     return '';
 2148: }
 2149:  
 2150: sub start_td_tex {
 2151:     my ($parstack,$parser,$safeeval) = @_;
 2152:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2153:     if ($alignchar eq '') {
 2154: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2155:     }
 2156:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2157:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2158:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2159:     if (defined $TeXwidth) {		
 2160: 	my $current_length=&recalc($TeXwidth);
 2161: 	$current_length=~/(\d+\.?\d*)/;
 2162: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2163:     }
 2164:     &Apache::lonxml::startredirection();
 2165:     return '';
 2166: }
 2167: 
 2168: sub end_td_tex {
 2169:     my ($parstack,$parser,$safeeval) = @_;
 2170:     my $current_row = $Apache::londefdef::table[-1]{'row_number'};
 2171:     my $data=&Apache::lonxml::endredirection();
 2172:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2173:     if (defined $TeXwidth) {		
 2174: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2175: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2176: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2177:     } else {
 2178: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2179: 	    my $garbage_data=$data;
 2180: 	    my $fwidth=0;
 2181:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2182: 		my $current_length=&recalc($1);
 2183: 		$current_length=~/(\d+\.?\d*)/;
 2184: 		if ($fwidth<$1) {$fwidth=$1;}
 2185: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2186: 	    }
 2187:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2188: 		my $current_length=$1;
 2189: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2190: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2191: 	    }
 2192: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2193: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2194: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2195: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2196: 	} else {  
 2197: 	    $data=~s/^\s+(\S.*)/$1/; 
 2198: 	    $data=~s/(.*\S)\s+$/$1/;
 2199: 	    $data=~s/(\s)+/$1/;
 2200: 	    my ($current_length,$min_length)=(0,0);
 2201: 	    if ($data=~/\\vskip/) {
 2202:                 my $newdata=$data;
 2203: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2204: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2205: 		foreach my $elementdata (@newdata) {
 2206: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2207: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2208:                     my @words=split(/ /,$elementdata);
 2209: 		    foreach my $word (@words) {
 2210: 			my $lengthword=2.5*&LATEX_length($word);
 2211: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2212: 		    }
 2213: 		}
 2214: 	    } else {
 2215: 		$current_length=2.5*&LATEX_length($data);
 2216:                     my @words=split(/ /,$data);
 2217: 		    foreach my $word (@words) {
 2218: 			my $lengthword=2*&LATEX_length($word);
 2219: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2220: 		    }
 2221: 	    }
 2222: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2223: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2224: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2225: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2226: 	}        
 2227:     }
 2228: 	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
 2229: 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2230: 	}
 2231:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2232:     return'';
 2233: }
 2234: 
 2235: sub end_td {
 2236:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2237:     my $currentstring = '';
 2238:     if ($target eq 'web') {
 2239: 	$currentstring = $token->[2];     
 2240:     } elsif ($target eq 'tex') {
 2241:         $Apache::londefdef::TD_redirection =0;
 2242: 	&end_td_tex($parstack,$parser,$safeeval);
 2243:     }
 2244:     return $currentstring;
 2245: }
 2246: 
 2247: #-- <th> tag (end tag optional)
 2248: sub start_th {
 2249:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2250:     my $currentstring = '';
 2251:     if ($target eq 'web') {
 2252: 	$currentstring = $token->[4];     
 2253:     } elsif ($target eq 'tex') {
 2254: 	$Apache::londefdef::TD_redirection = 1;
 2255: 	&tagg_check('tr','th',$tagstack,$parstack,$parser,$safeeval);
 2256:     } 
 2257:     return $currentstring;
 2258: }   
 2259:     
 2260: sub tagg_check {
 2261:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
 2262:     my @ar=@$parstack; 
 2263:     for (my $i=$#ar-1;$i>=0;$i--) {
 2264: 	if (lc($$tagstack[$i]) eq $good_tag) {
 2265: 	    &start_th_tex($parstack,$parser,$safeeval);
 2266: 	    last;
 2267: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
 2268: 	    splice @ar, $i+1;
 2269: 	    &end_th_tex(\@ar,$parser,$safeeval);
 2270: 	    &start_th_tex($parstack,$parser,$safeeval);
 2271: 	    last;
 2272: 	}
 2273:     }
 2274:     return '';
 2275: }
 2276:  
 2277: sub start_th_tex {
 2278:     my ($parstack,$parser,$safeeval) = @_;
 2279:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
 2280:     if ($alignchar eq '') {
 2281: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
 2282:     }
 2283:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
 2284:     $Apache::londefdef::table[-1]{'counter_columns'}++;
 2285:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2286:     if (defined $TeXwidth) {		
 2287: 	my $current_length=&recalc($TeXwidth);
 2288: 	$current_length=~/(\d+\.?\d*)/;
 2289: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
 2290:     }
 2291:     &Apache::lonxml::startredirection();
 2292:     return '';
 2293: }
 2294: 
 2295: sub end_th_tex {
 2296:     my ($parstack,$parser,$safeeval) = @_;
 2297:     my $current_row = $Apache::londefdef::table[-1]{'row_number'};
 2298:     my $data=&Apache::lonxml::endredirection();
 2299:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
 2300:     if (defined $TeXwidth) {		
 2301: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2302: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2303: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2304:     } else {
 2305: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
 2306: 	    my $garbage_data=$data;
 2307: 	    my $fwidth=0;
 2308:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
 2309: 		my $current_length=&recalc($1);
 2310: 		$current_length=~/(\d+\.?\d*)/;
 2311: 		if ($fwidth<$1) {$fwidth=$1;}
 2312: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
 2313: 	    }
 2314:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
 2315: 		my $current_length=$1;
 2316: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
 2317: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
 2318: 	    }
 2319: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2320: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 2321: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2322: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2323: 	} else {  
 2324: 	    $data=~s/^\s+(\S.*)/$1/; 
 2325: 	    $data=~s/(.*\S)\s+$/$1/;
 2326: 	    $data=~s/(\s)+/$1/;
 2327: 	    my ($current_length,$min_length)=(0,0);
 2328: 	    if ($data=~/\\vskip/) {
 2329:                 my $newdata=$data;
 2330: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
 2331: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
 2332: 		foreach my $elementdata (@newdata) {
 2333: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
 2334: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
 2335:                     my @words=split(/ /,$elementdata);
 2336: 		    foreach my $word (@words) {
 2337: 			my $lengthword=2.5*&LATEX_length($word);
 2338: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2339: 		    }
 2340: 		}
 2341: 	    } else {
 2342: 		$current_length=2.5*&LATEX_length($data);
 2343:                     my @words=split(/ /,$data);
 2344: 		    foreach my $word (@words) {
 2345: 			my $lengthword=2*&LATEX_length($word);
 2346: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
 2347: 		    }
 2348: 	    }
 2349: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2350: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 2351: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 2352: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 2353: 	}        
 2354:     }
 2355: 	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
 2356: 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 2357: 	}
 2358:     #make data bold
 2359:     $data='\textbf{'.$data.'}';
 2360:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
 2361:     return'';
 2362: }
 2363: 
 2364: sub end_th {
 2365:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2366:     my $currentstring = '';
 2367:     if ($target eq 'web') {
 2368: 	$currentstring = $token->[2];     
 2369:     } elsif ($target eq 'tex') {
 2370:         $Apache::londefdef::TD_redirection =0;
 2371: 	&end_th_tex($parstack,$parser,$safeeval);
 2372:     }
 2373:     return $currentstring;
 2374: }
 2375:      
 2376: #-- <img> tag (end tag forbidden)
 2377: sub start_img {
 2378:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2379:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
 2380: 					 undef,1);
 2381:     if (not $src and ($target eq 'web' or $target eq 'tex')) { 
 2382: 	my $inside = &Apache::lonxml::get_all_text("/img",$parser);
 2383: 	return '';
 2384:     }
 2385:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$src;
 2386:     my $currentstring = '';
 2387:     my $scaling = .3;
 2388:     if ($target eq 'web') {
 2389: 	if ($ENV{'browser.imagesuppress'} ne 'on') {
 2390: 	    $currentstring.= $token->[4];
 2391: 	} else {
 2392: 	    my $alttag= &Apache::lonxml::get_param
 2393: 		('alt',$parstack,$safeeval,undef,1);
 2394: 	    unless ($alttag) {
 2395: 		$alttag=&Apache::lonmeta::alttag
 2396: 		    ($Apache::lonxml::pwd[-1],$src);
 2397: 	    }
 2398: 	    $currentstring.='[IMAGE: '.$alttag.']';
 2399: 	}
 2400:     } elsif ($target eq 'tex') {
 2401: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 2402:         #if uploaded restore the path
 2403: 	if ($src=~/^\/uploaded\/([^\/]+)\/([^\/]+)\/simplepage\/([^\/]+)$/) {
 2404: 	    $src=&Apache::loncommon::propath($1,$2).'/userfiles/simplepage/'.$3;
 2405: 	} elsif ($src=~/^\/uploaded\/([^\/]+)\/([^\/]+)\/aboutme\/([^\/]+)$/) {
 2406: 	    $src=&Apache::loncommon::propath($1,$2).'/userfiles/aboutme/'.$3;
 2407: 	}
 2408: 	#if original gif/jpg/png file exist do following:
 2409: 	if (-e $src) {
 2410: 	    #what is the image size?
 2411: 	    my $width_param=&image_size($src,$scaling,$parstack,$safeeval);
 2412:             my ($file,$path)=&file_path($src); 
 2413: 	    my $newsrc = $src;
 2414: 	    $newsrc =~ s/\.(gif|jpg|png)$/.eps/i;
 2415: 	    $file=~s/\.(gif|jpg|png)$/.eps/i;
 2416: 	    #where can we find the picture?
 2417: 	    if (-e $newsrc) {
 2418: 		#eps counterpart for image exist 
 2419: 		if ($path) {
 2420: 		    $currentstring .= '\vskip 1 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 2421: 		}
 2422: 	    } else {
 2423: 		#there is no eps counterpart for image - check for ps one
 2424: 		$newsrc =~ s/\.eps$/\.ps/;
 2425: 		if (-e $newsrc) {
 2426: 		    #ps counterpart for image exist 
 2427: 		    $file =~ s/\.eps$/\.ps/;
 2428: 		    if ($path) {
 2429: 			$currentstring .= '\vskip 1 mm \noindent\graphicspath{{'.$path.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 2430: 		    }
 2431: 		} else {
 2432: 		    #care about eps dynamical generation
 2433: 		    $currentstring.='\vskip 1 mm '.&eps_generation($src,$file,$width_param);
 2434: 		}
 2435: 	    }
 2436: 	} else {
 2437: 	    #original image file doesn't exist so check the alt attribute
 2438: 	    my $alt = 
 2439: 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
 2440: 	    unless ($alt) {
 2441: 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 2442: 	    }
 2443: 
 2444: 	    if ($alt) {
 2445: 		$currentstring .= ' '.$alt.' ';
 2446: 	    } else {
 2447: 		#<allow> tag will care about replication 
 2448: 	    }
 2449: 	}
 2450:     } elsif ($target eq 'edit') {
 2451: 	$currentstring .=&Apache::edit::tag_start($target,$token);
 2452: 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
 2453: 	    &Apache::edit::browse('src',undef,'alt').' '.
 2454: 	    &Apache::edit::search('src',undef,'alt').'<br />';
 2455: 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
 2456: 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
 2457: 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
 2458: 	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
 2459: 	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
 2460: 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 2461: 	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval);
 2462: 	my $alt=&Apache::lonxml::get_param('alt',$parstack,$safeeval);
 2463: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
 2464: 	my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
 2465: 	$currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
 2466: 	if ($width) { $currentstring.=' width="'.$width.'" '; }
 2467: 	if ($height) { $currentstring.=' height="'.$height.'" '; }
 2468: 	$currentstring .= ' />';
 2469:     } elsif ($target eq 'modified') {
 2470: 	my ($osrc,$owidth,$oheight)=
 2471: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 2472: 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
 2473: 					     $safeeval,'src','alt',
 2474: 					     'TeXwidth','TeXheight',
 2475: 					     'width','height');
 2476: 	my ($nsrc,$nwidth,$nheight)=
 2477: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
 2478: 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
 2479: 	&image_replication($loc);
 2480: 	my ($iwidth,$iheight);
 2481: 	if (-e $loc) {
 2482: 	    my $image = Image::Magick->new;
 2483: 	    $image->Read($loc);
 2484: 	    ($iwidth, $iheight) = ($image->Get('width'),
 2485: 				   $image->Get('height'));
 2486: 	}
 2487: 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
 2488: 	    # changed image or no size specified,
 2489:             # if they didn't explicitly change the 
 2490:             # width or height use the ones from the image
 2491: 	    if ($iwidth && $iheight) {
 2492: 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
 2493: 		    $token->[2]{'width'} = $iwidth;$ctag=1;
 2494: 		}
 2495: 		if ($oheight == $nheight || (!$nwidth && !$nheight)) {
 2496: 		    $token->[2]{'height'}=$iheight;$ctag=1;
 2497: 		}
 2498: 	    }
 2499: 	}
 2500: 	my ($cwidth,$cheight)=($token->[2]{'width'},$token->[2]{'height'});
 2501: 	# if we don't have a width or height
 2502: 	if ($iwidth && $cwidth && !$cheight) {
 2503: 	    $token->[2]{'height'}=int(($cwidth/$iwidth)*$iheight);$ctag=1;
 2504: 	}
 2505: 	if ($iheight && $cheight && !$cwidth) {
 2506: 	    $token->[2]{'width'}=int(($cheight/$iheight)*$iwidth);$ctag=1;
 2507: 	}
 2508: 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
 2509:     }
 2510:     return $currentstring;
 2511: }
 2512: 
 2513: sub end_img {
 2514:     my ($target,$token) = @_;
 2515:     my $currentstring = '';
 2516:     if ($target eq 'web') {
 2517: 	$currentstring = $token->[2];
 2518:     } elsif ($target eq 'tex') {
 2519: 	$currentstring = '';
 2520:     }
 2521:     return $currentstring;
 2522: }
 2523: 
 2524: #-- <applet> tag (end tag required)
 2525: sub start_applet {
 2526:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2527:     
 2528:     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
 2529:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$code;
 2530:     
 2531:     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
 2532: 					   undef,1);
 2533:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$archive;
 2534:     
 2535:     my $currentstring = '';
 2536:     if ($target eq 'web') {
 2537: 	if ($ENV{'browser.appletsuppress'} ne 'on') {
 2538: 	    $currentstring = $token->[4];
 2539: 	} else {
 2540: 	    my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 2541: 						   $safeeval,undef,1);
 2542: 	    unless ($alttag) {
 2543: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 2544: 						 $code);
 2545: 	    }
 2546: 	    $currentstring='[APPLET: '.$alttag.']';
 2547: 	}
 2548:     } elsif ($target eq 'tex') {
 2549: 	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
 2550: 					       $safeeval,undef,1);
 2551: 	unless ($alttag) {
 2552: 	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
 2553: 						undef,1);
 2554: 	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
 2555: 					     $code);
 2556: 	}
 2557: 	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
 2558: 	    '.}\end{center}';
 2559:     } 
 2560:     return $currentstring;
 2561: }
 2562: 
 2563: sub end_applet {
 2564:     my ($target,$token) = @_;
 2565:     my $currentstring = '';
 2566:     if ($target eq 'web') {
 2567: 	$currentstring = $token->[2];
 2568:     } elsif ($target eq 'tex') {
 2569:     } 
 2570:     return $currentstring;
 2571: }
 2572: 
 2573: #-- <embed> tag (end tag optional/required)
 2574: sub start_embed {    
 2575:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2576:     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 2577:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$src;
 2578:     my $currentstring = '';
 2579:     if ($target eq 'web') {
 2580: 	if ($ENV{'browser.embedsuppress'} ne 'on') {
 2581: 	    $currentstring = $token->[4];
 2582: 	} else {
 2583: 	    my $alttag=&Apache::lonxml::get_param
 2584: 		('alt',$parstack,$safeeval,undef,1);
 2585: 	    unless ($alttag) {
 2586: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
 2587: 	    }
 2588: 	    $currentstring='[EMBED: '.$alttag.']';
 2589: 	}
 2590:     } elsif ($target eq 'tex') {
 2591:     } 
 2592:     return $currentstring;
 2593: }
 2594: 
 2595: sub end_embed {
 2596:     my ($target,$token) = @_;
 2597:     my $currentstring = '';
 2598:     if ($target eq 'web') {
 2599: 	$currentstring = $token->[2];     
 2600:     } elsif ($target eq 'tex') {  
 2601:     } 
 2602:     return $currentstring;
 2603: }
 2604: 
 2605: #-- <param> tag (end tag forbidden)
 2606: sub start_param {
 2607:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2608:     if (&Apache::lonxml::get_param
 2609: 	('name',$parstack,$safeeval,undef,1)=~/^cabbase$/i) {
 2610: 	$Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
 2611: 	    &Apache::lonxml::get_param('value',$parstack,$safeeval,undef,1);
 2612:     }   
 2613:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=   
 2614: 	&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 2615:     my $currentstring = '';
 2616:     if ($target eq 'web') {
 2617: 	$currentstring = $token->[4];     
 2618:     } elsif ($target eq 'tex') {
 2619:     } 
 2620:     return $currentstring;
 2621: }
 2622: 
 2623: sub end_param {
 2624:     my ($target,$token) = @_;
 2625:     my $currentstring = '';
 2626:     if ($target eq 'web') {
 2627: 	$currentstring = $token->[2];     
 2628:     } elsif ($target eq 'tex') {
 2629:     } 
 2630:     return $currentstring;
 2631: }
 2632: 
 2633: #-- <allow> tag
 2634: sub start_allow {
 2635:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2636:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
 2637:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
 2638:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
 2639: 	&Apache::lonnet::clutter($src);
 2640:     &image_replication($src);
 2641:     my $result;
 2642:     if ($target eq 'edit') {
 2643: 	$result .=&Apache::edit::tag_start($target,$token);
 2644: 	$result .=&Apache::edit::text_arg('File Spec:','src',$token,70);
 2645: 	$result .=&Apache::edit::end_row();#.&Apache::edit::start_spanning_row();
 2646:     } elsif ($target eq 'modified') {
 2647: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
 2648: 						     $safeeval,'src');
 2649: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 2650:     }
 2651:     return $result;
 2652: }
 2653: 
 2654: sub end_allow {
 2655:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2656:     if ( $target eq 'edit') { return (&Apache::edit::end_table()); }
 2657:     return '';
 2658: }
 2659: 
 2660: #-- Frames (end tag required)
 2661: #-- <frameset>
 2662: sub start_frameset {
 2663:     my ($target,$token) = @_;
 2664:     my $currentstring = '';
 2665:     if ($target eq 'web') { 
 2666: 	if (!$Apache::lonxml::registered &&
 2667: 	    $ENV{'request.state'} eq 'published') {
 2668: 	    $currentstring.='<head>'.
 2669: 		&Apache::lonmenu::registerurl(undef,$target).'</head>';
 2670: 	}
 2671: 	my $onLoad='';
 2672: 	foreach my $key (keys(%{$token->[2]})) {
 2673: 	    if ($key =~ /^onload$/i) {
 2674: 		$onLoad.=$token->[2]->{$key}.';';
 2675: 		delete($token->[2]->{$key});
 2676: 	    }
 2677: 	}
 2678: 	$token->[2]->{'onload'}=&Apache::lonmenu::loadevents().';'.$onLoad;
 2679: 	my $onUnload='';
 2680: 	foreach my $key (keys(%{$token->[2]})) {
 2681: 	    if ($key =~ /^onunload$/i) {
 2682: 		$onUnload.=$token->[2]->{$key}.';';
 2683: 		delete($token->[2]->{$key});
 2684: 	    }
 2685: 	}
 2686: 	$token->[2]->{'onunload'}=&Apache::lonmenu::unloadevents().
 2687: 	    ';'.$onUnload;
 2688: 	
 2689: 	$currentstring .= '<'.$token->[1];
 2690: 	foreach (keys %{$token->[2]}) {
 2691: 	    $currentstring.=' '.$_.'="'.$token->[2]->{$_}.'"';
 2692: 	}
 2693: 	$currentstring.='>';
 2694:     }
 2695:     return $currentstring;
 2696: }
 2697: 
 2698: sub end_frameset {
 2699:     my ($target,$token) = @_;
 2700:     my $currentstring = '';
 2701:     if ($target eq 'web') {
 2702: 	$currentstring = $token->[2];
 2703:     }
 2704:     return $currentstring;
 2705: }
 2706: 
 2707: #-- <xmp> (end tag required)
 2708: sub start_xmp {
 2709:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2710:     my $currentstring = '';
 2711:     if ($target eq 'web') {
 2712: 	$currentstring .= $token->[4];
 2713:     } elsif ($target eq 'tex') {
 2714: 	$currentstring .= '\begin{verbatim}';
 2715:     } 
 2716:     return $currentstring;
 2717: }
 2718: 
 2719: sub end_xmp {
 2720:     my ($target,$token) = @_;
 2721:     my $currentstring = '';
 2722:     if ($target eq 'web') {
 2723: 	$currentstring .= $token->[2];
 2724:     } elsif ($target eq 'tex') {
 2725: 	$currentstring .= '\end{verbatim}';
 2726:     }
 2727:     return $currentstring;
 2728: }
 2729: 
 2730: #-- <pre> (end tag required)
 2731: sub start_pre {
 2732:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2733:     my $currentstring = '';
 2734:     if ($target eq 'web') {
 2735: 	$currentstring .= $token->[4];
 2736:     } elsif ($target eq 'tex') {
 2737: 	$currentstring .= '\begin{verbatim}';
 2738:     } 
 2739:     return $currentstring;
 2740: }
 2741: 
 2742: sub end_pre {
 2743:     my ($target,$token) = @_;
 2744:     my $currentstring = '';
 2745:     if ($target eq 'web') {
 2746: 	$currentstring .= $token->[2];
 2747:     } elsif ($target eq 'tex') {
 2748: 	$currentstring .= '\end{verbatim}';
 2749:     }
 2750:     return $currentstring;
 2751: }
 2752: 
 2753: #-- <insert>
 2754: sub start_insert {
 2755:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2756:     my $currentstring = '';
 2757:     if ($target eq 'web') {
 2758: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 2759: 	$currentstring .= '<b>'.$display.'</b>';;
 2760:     }
 2761:     return $currentstring;
 2762: }
 2763: 
 2764: sub end_insert {
 2765:     my ($target,$token) = @_;
 2766:     my $currentstring = '';
 2767:     if ($target eq 'web') {
 2768: 	$currentstring .= '';
 2769:     }
 2770:     return $currentstring;
 2771: }
 2772: 
 2773: #-- <externallink>
 2774: sub start_externallink {
 2775:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2776:     my $currentstring = '';
 2777:     if ($target eq 'web') {
 2778: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
 2779: 	$currentstring .= '<b>'.$display.'</b>';;
 2780:     }
 2781:     return $currentstring;
 2782: }
 2783: 
 2784: sub end_externallink {
 2785:     my ($target,$token) = @_;
 2786:     my $currentstring = '';
 2787:     if ($target eq 'web') {
 2788: 	$currentstring .= '';
 2789:     }
 2790:     return $currentstring;
 2791: }
 2792: 
 2793: #-- <blankspace heigth="">
 2794: sub start_blankspace {
 2795:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
 2796:     my $currentstring = '';
 2797:     if ($target eq 'tex') {
 2798: 	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
 2799: 	$currentstring .= '\vskip '.$howmuch.' ';
 2800:     }
 2801:     return $currentstring;
 2802: }
 2803: 
 2804: sub end_blankspace {
 2805:     my ($target,$token) = @_;
 2806:     my $currentstring = '';
 2807:     if ($target eq 'tex') {
 2808: 	$currentstring .= '';
 2809:     }
 2810:     return $currentstring;
 2811: }
 2812: 
 2813: #-- <abbr> tag (end tag required)
 2814: sub start_abbr {
 2815:     my ($target,$token) = @_;
 2816:     my $currentstring = '';
 2817:     if ($target eq 'web') {
 2818: 	$currentstring = $token->[4];     
 2819:     } 
 2820:     return $currentstring;
 2821: }
 2822: 
 2823: sub end_abbr {
 2824:     my ($target,$token) = @_;
 2825:     my $currentstring = '';
 2826:     if ($target eq 'web') {
 2827: 	$currentstring = $token->[2];    
 2828:     } 
 2829:     return $currentstring;
 2830: }
 2831: 
 2832: #-- <acronym> tag (end tag required)
 2833: sub start_acronym {
 2834:     my ($target,$token) = @_;
 2835:     my $currentstring = '';
 2836:     if ($target eq 'web') {
 2837: 	$currentstring = $token->[4];     
 2838:     } 
 2839:     return $currentstring;
 2840: }
 2841: 
 2842: sub end_acronym {
 2843:     my ($target,$token) = @_;
 2844:     my $currentstring = '';
 2845:     if ($target eq 'web') {
 2846: 	$currentstring = $token->[2];    
 2847:     } 
 2848:     return $currentstring;
 2849: }
 2850: 
 2851: #-- <area> tag (end tag forbidden)
 2852: sub start_area {
 2853:     my ($target,$token) = @_;
 2854:     my $currentstring = '';
 2855:     if ($target eq 'web') {
 2856: 	$currentstring = $token->[4];     
 2857:     } 
 2858:     return $currentstring;
 2859: }
 2860: 
 2861: sub end_area {
 2862:     my ($target,$token) = @_;
 2863:     my $currentstring = '';
 2864:     if ($target eq 'web') {
 2865: 	$currentstring = $token->[2];    
 2866:     } 
 2867:     return $currentstring;
 2868: }
 2869: 
 2870: #-- <base> tag (end tag forbidden)
 2871: sub start_base {
 2872:     my ($target,$token) = @_;
 2873:     my $currentstring = '';
 2874:     if ($target eq 'web') {
 2875: 	$currentstring = $token->[4];     
 2876:     }
 2877:     return $currentstring;
 2878: }
 2879: 
 2880: sub end_base {
 2881:     my ($target,$token) = @_;
 2882:     my $currentstring = '';
 2883:     if ($target eq 'web') {
 2884: 	$currentstring = $token->[2];    
 2885:     } 
 2886:     return $currentstring;
 2887: }
 2888: 
 2889: #-- <bdo> tag (end tag required)
 2890: sub start_bdo {
 2891:     my ($target,$token) = @_;
 2892:     my $currentstring = '';
 2893:     if ($target eq 'web') {
 2894: 	$currentstring = $token->[4];     
 2895:     } 
 2896:     return $currentstring;
 2897: }
 2898: 
 2899: sub end_bdo {
 2900:     my ($target,$token) = @_;
 2901:     my $currentstring = '';
 2902:     if ($target eq 'web') {
 2903: 	$currentstring = $token->[2];    
 2904:     } 
 2905:     return $currentstring;
 2906: }
 2907: 
 2908: #-- <bgsound> tag (end tag optional)
 2909: sub start_bgsound {
 2910:     my ($target,$token) = @_;
 2911:     my $currentstring = '';
 2912:     if ($target eq 'web') {
 2913: 	$currentstring = $token->[4];     
 2914:     } 
 2915:     return $currentstring;
 2916: }
 2917: 
 2918: sub end_bgsound {
 2919:     my ($target,$token) = @_;
 2920:     my $currentstring = '';
 2921:     if ($target eq 'web') {
 2922: 	$currentstring = $token->[2];    
 2923:     } 
 2924:     return $currentstring;
 2925: }
 2926: 
 2927: #-- <blink> tag (end tag required)
 2928: sub start_blink {
 2929:     my ($target,$token) = @_;
 2930:     my $currentstring = '';
 2931:     if ($target eq 'web') {
 2932: 	$currentstring = $token->[4];     
 2933:     } 
 2934:     return $currentstring;
 2935: }
 2936: 
 2937: sub end_blink {
 2938:     my ($target,$token) = @_;
 2939:     my $currentstring = '';
 2940:     if ($target eq 'web') {
 2941: 	$currentstring = $token->[2];    
 2942:     } 
 2943:     return $currentstring;
 2944: }
 2945: 
 2946: #-- <blockquote> tag (end tag required)
 2947: sub start_blockquote {
 2948:     my ($target,$token) = @_;
 2949:     my $currentstring = '';
 2950:     if ($target eq 'web') {
 2951: 	$currentstring = $token->[4];     
 2952:     } 
 2953:     return $currentstring;
 2954: }
 2955: 
 2956: sub end_blockquote {
 2957:     my ($target,$token) = @_;
 2958:     my $currentstring = '';
 2959:     if ($target eq 'web') {
 2960: 	$currentstring = $token->[2];    
 2961:     } 
 2962:     return $currentstring;
 2963: }
 2964: 
 2965: #-- <button> tag (end tag required)
 2966: sub start_button {
 2967:     my ($target,$token) = @_;
 2968:     my $currentstring = '';
 2969:     if ($target eq 'web') {
 2970: 	$currentstring = $token->[4];     
 2971:     } 
 2972:     return $currentstring;
 2973: }
 2974: 
 2975: sub end_button {
 2976:     my ($target,$token) = @_;
 2977:     my $currentstring = '';
 2978:     if ($target eq 'web') {
 2979: 	$currentstring = $token->[2];    
 2980:     } 
 2981:     return $currentstring;
 2982: }
 2983: 
 2984: #-- <caption> tag (end tag required)
 2985: sub start_caption {
 2986:     my ($target,$token) = @_;
 2987:     my $currentstring = '';
 2988:     if ($target eq 'web') {
 2989: 	$currentstring = $token->[4];     
 2990:     } 
 2991:     return $currentstring;
 2992: }
 2993: 
 2994: sub end_caption {
 2995:     my ($target,$token) = @_;
 2996:     my $currentstring = '';
 2997:     if ($target eq 'web') {
 2998: 	$currentstring = $token->[2];    
 2999:     } 
 3000:     return $currentstring;
 3001: }
 3002: 
 3003: #-- <col> tag (end tag forbdden)
 3004: sub start_col {
 3005:     my ($target,$token) = @_;
 3006:     my $currentstring = '';
 3007:     if ($target eq 'web') {
 3008: 	$currentstring = $token->[4];     
 3009:     } 
 3010:     return $currentstring;
 3011: }
 3012: 
 3013: sub end_col {
 3014:     my ($target,$token) = @_;
 3015:     my $currentstring = '';
 3016:     if ($target eq 'web') {
 3017: 	$currentstring = $token->[2];    
 3018:     } 
 3019:     return $currentstring;
 3020: }
 3021: 
 3022: #-- <colgroup> tag (end tag optional)
 3023: sub start_colgroup {
 3024:     my ($target,$token) = @_;
 3025:     my $currentstring = '';
 3026:     if ($target eq 'web') {
 3027: 	$currentstring = $token->[4];     
 3028:     } 
 3029:     return $currentstring;
 3030: }
 3031: 
 3032: sub end_colgroup {
 3033:     my ($target,$token) = @_;
 3034:     my $currentstring = '';
 3035:     if ($target eq 'web') {
 3036: 	$currentstring = $token->[2];    
 3037:     } 
 3038:     return $currentstring;
 3039: }
 3040: 
 3041: #-- <del> tag (end tag required)
 3042: sub start_del {
 3043:     my ($target,$token) = @_;
 3044:     my $currentstring = '';
 3045:     if ($target eq 'web') {
 3046: 	$currentstring = $token->[4];     
 3047:     } 
 3048:     return $currentstring;
 3049: }
 3050: 
 3051: sub end_del {
 3052:     my ($target,$token) = @_;
 3053:     my $currentstring = '';
 3054:     if ($target eq 'web') {
 3055: 	$currentstring = $token->[2];    
 3056:     } 
 3057:     return $currentstring;
 3058: }
 3059: 
 3060: #-- <fieldset> tag (end tag required)
 3061: sub start_fieldset {
 3062:     my ($target,$token) = @_;
 3063:     my $currentstring = '';
 3064:     if ($target eq 'web') {
 3065: 	$currentstring = $token->[4];     
 3066:     } 
 3067:     return $currentstring;
 3068: }
 3069: 
 3070: sub end_fieldset {
 3071:     my ($target,$token) = @_;
 3072:     my $currentstring = '';
 3073:     if ($target eq 'web') {
 3074: 	$currentstring = $token->[2];    
 3075:     } 
 3076:     return $currentstring;
 3077: }
 3078: 
 3079: #-- <frame> tag (end tag forbidden)
 3080: sub start_frame {
 3081:     my ($target,$token) = @_;
 3082:     my $currentstring = '';
 3083:     if ($target eq 'web') {
 3084: 	$currentstring = $token->[4];     
 3085:     } 
 3086:     return $currentstring;
 3087: }
 3088: 
 3089: sub end_frame {
 3090:     my ($target,$token) = @_;
 3091:     my $currentstring = '';
 3092:     if ($target eq 'web') {
 3093: 	$currentstring = $token->[2];    
 3094:     } 
 3095:     return $currentstring;
 3096: }
 3097: 
 3098: #-- <iframe> tag (end tag required)
 3099: sub start_iframe {
 3100:     my ($target,$token) = @_;
 3101:     my $currentstring = '';
 3102:     if ($target eq 'web') {
 3103: 	$currentstring = $token->[4];     
 3104:     } 
 3105:     return $currentstring;
 3106: }
 3107: 
 3108: sub end_iframe {
 3109:     my ($target,$token) = @_;
 3110:     my $currentstring = '';
 3111:     if ($target eq 'web') {
 3112: 	$currentstring = $token->[2];    
 3113:     } 
 3114:     return $currentstring;
 3115: }
 3116: 
 3117: #-- <ins> tag (end tag required)
 3118: sub start_ins {
 3119:     my ($target,$token) = @_;
 3120:     my $currentstring = '';
 3121:     if ($target eq 'web') {
 3122: 	$currentstring = $token->[4];     
 3123:     } 
 3124:     return $currentstring;
 3125: }
 3126: 
 3127: sub end_ins {
 3128:     my ($target,$token) = @_;
 3129:     my $currentstring = '';
 3130:     if ($target eq 'web') {
 3131: 	$currentstring = $token->[2];    
 3132:     } 
 3133:     return $currentstring;
 3134: }
 3135: 
 3136: #-- <isindex> tag (end tag forbidden)
 3137: sub start_isindex {
 3138:     my ($target,$token) = @_;
 3139:     my $currentstring = '';
 3140:     if ($target eq 'web') {
 3141: 	$currentstring = $token->[4];     
 3142:     } 
 3143:     return $currentstring;
 3144: }
 3145: 
 3146: sub end_isindex {
 3147:     my ($target,$token) = @_;
 3148:     my $currentstring = '';
 3149:     if ($target eq 'web') {
 3150: 	$currentstring = $token->[2];    
 3151:     } 
 3152:     return $currentstring;
 3153: }
 3154: 
 3155: #-- <keygen> tag (end tag forbidden)
 3156: sub start_keygen {
 3157:     my ($target,$token) = @_;
 3158:     my $currentstring = '';
 3159:     if ($target eq 'web') {
 3160: 	$currentstring = $token->[4];     
 3161:     } 
 3162:     return $currentstring;
 3163: }
 3164: 
 3165: sub end_keygen {
 3166:     my ($target,$token) = @_;
 3167:     my $currentstring = '';
 3168:     if ($target eq 'web') {
 3169: 	$currentstring = $token->[2];    
 3170:     } 
 3171:     return $currentstring;
 3172: }
 3173: 
 3174: #-- <label> tag
 3175: sub start_label {
 3176:     my ($target,$token) = @_;
 3177:     my $currentstring = '';
 3178:     if ($target eq 'web') {
 3179: 	$currentstring = $token->[4];     
 3180:     } 
 3181:     return $currentstring;
 3182: }
 3183: 
 3184: sub end_label {
 3185:     my ($target,$token) = @_;
 3186:     my $currentstring = '';
 3187:     if ($target eq 'web') {
 3188: 	$currentstring = $token->[2];    
 3189:     } 
 3190:     return $currentstring;
 3191: }
 3192: 
 3193: #-- <layer> tag (end tag required)
 3194: sub start_layer {
 3195:     my ($target,$token) = @_;
 3196:     my $currentstring = '';
 3197:     if ($target eq 'web') {
 3198: 	$currentstring = $token->[4];     
 3199:     } 
 3200:     return $currentstring;
 3201: }
 3202: 
 3203: sub end_layer {
 3204:     my ($target,$token) = @_;
 3205:     my $currentstring = '';
 3206:     if ($target eq 'web') {
 3207: 	$currentstring = $token->[2];    
 3208:     } 
 3209:     return $currentstring;
 3210: }
 3211: 
 3212: #-- <legend> tag (end tag required)
 3213: sub start_legend {
 3214:     my ($target,$token) = @_;
 3215:     my $currentstring = '';
 3216:     if ($target eq 'web') {
 3217: 	$currentstring = $token->[4];     
 3218:     } 
 3219:     return $currentstring;
 3220: }
 3221: 
 3222: sub end_legend {
 3223:     my ($target,$token) = @_;
 3224:     my $currentstring = '';
 3225:     if ($target eq 'web') {
 3226: 	$currentstring = $token->[2];    
 3227:     } 
 3228:     return $currentstring;
 3229: }
 3230: 
 3231: #-- <link> tag (end tag forbidden)
 3232: sub start_link {
 3233:     my ($target,$token) = @_;
 3234:     my $currentstring = '';
 3235:     if ($target eq 'web') {
 3236: 	$currentstring = $token->[4];     
 3237:     } 
 3238:     return $currentstring;
 3239: }
 3240: 
 3241: sub end_link {
 3242:     my ($target,$token) = @_;
 3243:     my $currentstring = '';
 3244:     if ($target eq 'web') {
 3245: 	$currentstring = $token->[2];    
 3246:     } 
 3247:     return $currentstring;
 3248: }
 3249: 
 3250: #-- <marquee> tag (end tag optional)
 3251: sub start_marquee {
 3252:     my ($target,$token) = @_;
 3253:     my $currentstring = '';
 3254:     if ($target eq 'web') {
 3255: 	$currentstring = $token->[4];     
 3256:     } 
 3257:     return $currentstring;
 3258: }
 3259: 
 3260: sub end_marquee {
 3261:     my ($target,$token) = @_;
 3262:     my $currentstring = '';
 3263:     if ($target eq 'web') {
 3264: 	$currentstring = $token->[2];    
 3265:     } 
 3266:     return $currentstring;
 3267: }
 3268: 
 3269: #-- <multicol> tag (end tag required)
 3270: sub start_multicol {
 3271:     my ($target,$token) = @_;
 3272:     my $currentstring = '';
 3273:     if ($target eq 'web') {
 3274: 	$currentstring = $token->[4];     
 3275:     } 
 3276:     return $currentstring;
 3277: }
 3278: 
 3279: sub end_multicol {
 3280:     my ($target,$token) = @_;
 3281:     my $currentstring = '';
 3282:     if ($target eq 'web') {
 3283: 	$currentstring = $token->[2];    
 3284:     } 
 3285:     return $currentstring;
 3286: }
 3287: 
 3288: #-- <nobr> tag (end tag required)
 3289: sub start_nobr {
 3290:     my ($target,$token) = @_;
 3291:     my $currentstring = '';
 3292:     if ($target eq 'web') {
 3293: 	$currentstring = $token->[4];     
 3294:     }  elsif ($target eq 'tex') {
 3295: 	$currentstring='\mbox{';
 3296:     }
 3297:     return $currentstring;
 3298: }
 3299: 
 3300: sub end_nobr {
 3301:     my ($target,$token) = @_;
 3302:     my $currentstring = '';
 3303:     if ($target eq 'web') {
 3304: 	$currentstring = $token->[2];    
 3305:     }   elsif ($target eq 'tex') {
 3306: 	$currentstring='}';
 3307:     }
 3308:     return $currentstring;
 3309: }
 3310: 
 3311: #-- <noembed> tag (end tag required)
 3312: sub start_noembed {
 3313:     my ($target,$token) = @_;
 3314:     my $currentstring = '';
 3315:     if ($target eq 'web') {
 3316: 	$currentstring = $token->[4];     
 3317:     } 
 3318:     return $currentstring;
 3319: }
 3320: 
 3321: sub end_noembed {
 3322:     my ($target,$token) = @_;
 3323:     my $currentstring = '';
 3324:     if ($target eq 'web') {
 3325: 	$currentstring = $token->[2];    
 3326:     } 
 3327:     return $currentstring;
 3328: }
 3329: 
 3330: #-- <noframes> tag (end tag required)
 3331: sub start_noframes {
 3332:     my ($target,$token) = @_;
 3333:     my $currentstring = '';
 3334:     if ($target eq 'web') {
 3335: 	$currentstring = $token->[4];     
 3336:     } 
 3337:     return $currentstring;
 3338: }
 3339: 
 3340: sub end_noframes {
 3341:     my ($target,$token) = @_;
 3342:     my $currentstring = '';
 3343:     if ($target eq 'web') {
 3344: 	$currentstring = $token->[2];    
 3345:     } 
 3346:     return $currentstring;
 3347: }
 3348: 
 3349: #-- <nolayer> tag (end tag required)
 3350: sub start_nolayer {
 3351:     my ($target,$token) = @_;
 3352:     my $currentstring = '';
 3353:     if ($target eq 'web') {
 3354: 	$currentstring = $token->[4];     
 3355:     } 
 3356:     return $currentstring;
 3357: }
 3358: 
 3359: sub end_nolayer {
 3360:     my ($target,$token) = @_;
 3361:     my $currentstring = '';
 3362:     if ($target eq 'web') {
 3363: 	$currentstring = $token->[2];    
 3364:     } 
 3365:     return $currentstring;
 3366: }
 3367: 
 3368: #-- <noscript> tag (end tag required)
 3369: sub start_noscript {
 3370:     my ($target,$token) = @_;
 3371:     my $currentstring = '';
 3372:     if ($target eq 'web') {
 3373: 	$currentstring = $token->[4];     
 3374:     } 
 3375:     return $currentstring;
 3376: }
 3377: 
 3378: sub end_noscript {
 3379:     my ($target,$token) = @_;
 3380:     my $currentstring = '';
 3381:     if ($target eq 'web') {
 3382: 	$currentstring = $token->[2];    
 3383:     } 
 3384:     return $currentstring;
 3385: }
 3386: 
 3387: #-- <object> tag (end tag required)
 3388: sub start_object {
 3389:     my ($target,$token) = @_;
 3390:     my $currentstring = '';
 3391:     if ($target eq 'web') {
 3392: 	$currentstring = $token->[4];     
 3393:     } 
 3394:     return $currentstring;
 3395: }
 3396: 
 3397: sub end_object {
 3398:     my ($target,$token) = @_;
 3399:     my $currentstring = '';
 3400:     if ($target eq 'web') {
 3401: 	$currentstring = $token->[2];    
 3402:     } 
 3403:     return $currentstring;
 3404: }
 3405: 
 3406: #-- <optgroup> tag (end tag required)
 3407: sub start_optgroup {
 3408:     my ($target,$token) = @_;
 3409:     my $currentstring = '';
 3410:     if ($target eq 'web') {
 3411: 	$currentstring = $token->[4];     
 3412:     } 
 3413:     return $currentstring;
 3414: }
 3415: 
 3416: sub end_optgroup {
 3417:     my ($target,$token) = @_;
 3418:     my $currentstring = '';
 3419:     if ($target eq 'web') {
 3420: 	$currentstring = $token->[2];    
 3421:     } 
 3422:     return $currentstring;
 3423: }
 3424: 
 3425: #-- <samp> tag (end tag required)
 3426: sub start_samp {
 3427:     my ($target,$token) = @_;
 3428:     my $currentstring = '';
 3429:     if ($target eq 'web') {
 3430: 	$currentstring = $token->[4];     
 3431:     } elsif ($target eq 'tex') {
 3432: 	$currentstring='\texttt{';
 3433:     }
 3434:     return $currentstring;
 3435: }
 3436: 
 3437: sub end_samp {
 3438:     my ($target,$token) = @_;
 3439:     my $currentstring = '';
 3440:     if ($target eq 'web') {
 3441: 	$currentstring = $token->[2];    
 3442:     } elsif ($target eq 'tex') {
 3443: 	$currentstring='}';
 3444:     }
 3445:     return $currentstring;
 3446: }
 3447: 
 3448: #-- <server> tag
 3449: sub start_server {
 3450:     my ($target,$token) = @_;
 3451:     my $currentstring = '';
 3452:     if ($target eq 'web') {
 3453: 	$currentstring = $token->[4];     
 3454:     } 
 3455:     return $currentstring;
 3456: }
 3457: 
 3458: sub end_server {
 3459:     my ($target,$token) = @_;
 3460:     my $currentstring = '';
 3461:     if ($target eq 'web') {
 3462: 	$currentstring = $token->[2];    
 3463:     } 
 3464:     return $currentstring;
 3465: }
 3466: 
 3467: #-- <spacer> tag (end tag forbidden)
 3468: sub start_spacer {
 3469:     my ($target,$token) = @_;
 3470:     my $currentstring = '';
 3471:     if ($target eq 'web') {
 3472: 	$currentstring = $token->[4];     
 3473:     } 
 3474:     return $currentstring;
 3475: }
 3476: 
 3477: sub end_spacer {
 3478:     my ($target,$token) = @_;
 3479:     my $currentstring = '';
 3480:     if ($target eq 'web') {
 3481: 	$currentstring = $token->[2];    
 3482:     } 
 3483:     return $currentstring;
 3484: }
 3485: 
 3486: #-- <span> tag (end tag required)
 3487: sub start_span {
 3488:     my ($target,$token) = @_;
 3489:     my $currentstring = '';
 3490:     if ($target eq 'web') {
 3491: 	$currentstring = $token->[4];     
 3492:     } 
 3493:     return $currentstring;
 3494: }
 3495: 
 3496: sub end_span {
 3497:     my ($target,$token) = @_;
 3498:     my $currentstring = '';
 3499:     if ($target eq 'web') {
 3500: 	$currentstring = $token->[2];    
 3501:     } 
 3502:     return $currentstring;
 3503: }
 3504: 
 3505: #-- <tbody> tag (end tag optional)
 3506: sub start_tbody {
 3507:     my ($target,$token) = @_;
 3508:     my $currentstring = '';
 3509:     if ($target eq 'web') {
 3510: 	$currentstring = $token->[4];     
 3511:     } 
 3512:     return $currentstring;
 3513: }
 3514: 
 3515: sub end_tbody {
 3516:     my ($target,$token) = @_;
 3517:     my $currentstring = '';
 3518:     if ($target eq 'web') {
 3519: 	$currentstring = $token->[2];    
 3520:     } 
 3521:     return $currentstring;
 3522: }
 3523: 
 3524: #-- <tfoot> tag (end tag optional)
 3525: sub start_tfoot {
 3526:     my ($target,$token) = @_;
 3527:     my $currentstring = '';
 3528:     if ($target eq 'web') {
 3529: 	$currentstring = $token->[4];     
 3530:     } 
 3531:     return $currentstring;
 3532: }
 3533: 
 3534: sub end_tfoot {
 3535:     my ($target,$token) = @_;
 3536:     my $currentstring = '';
 3537:     if ($target eq 'web') {
 3538: 	$currentstring = $token->[2];    
 3539:     } 
 3540:     return $currentstring;
 3541: }
 3542: 
 3543: #-- <thead> tag (end tag optional)
 3544: sub start_thead {
 3545:     my ($target,$token) = @_;
 3546:     my $currentstring = '';
 3547:     if ($target eq 'web') {
 3548: 	$currentstring = $token->[4];     
 3549:     } 
 3550:     return $currentstring;
 3551: }
 3552: 
 3553: sub end_thead {
 3554:     my ($target,$token) = @_;
 3555:     my $currentstring = '';
 3556:     if ($target eq 'web') {
 3557: 	$currentstring = $token->[2];    
 3558:     } 
 3559:     return $currentstring;
 3560: }
 3561: 
 3562: #-- <var> tag
 3563: sub start_var {
 3564:     my ($target,$token) = @_;
 3565:     my $currentstring = '';
 3566:     if ($target eq 'web') {
 3567: 	$currentstring = $token->[4];     
 3568:     } elsif ($target eq 'tex') {
 3569: 	$currentstring = '\textit{'; 
 3570:     }
 3571:     return $currentstring;
 3572: }
 3573: 
 3574: sub end_var {
 3575:     my ($target,$token) = @_;
 3576:     my $currentstring = '';
 3577:     if ($target eq 'web') {
 3578: 	$currentstring = $token->[2];
 3579:     } elsif ($target eq 'tex') {
 3580: 	$currentstring = '}'; 
 3581:     } 
 3582:     return $currentstring;
 3583: }
 3584: 
 3585: #-- <wbr> tag (end tag forbidden)
 3586: sub start_wbr {
 3587:     my ($target,$token) = @_;
 3588:     my $currentstring = '';
 3589:     if ($target eq 'web') {
 3590: 	$currentstring = $token->[4];     
 3591:     } 
 3592:     return $currentstring;
 3593: }
 3594: 
 3595: sub end_wbr {
 3596:     my ($target,$token) = @_;
 3597:     my $currentstring = '';
 3598:     if ($target eq 'web') {
 3599: 	$currentstring = $token->[2];    
 3600:     } 
 3601:     return $currentstring;
 3602: }
 3603: 
 3604: #-- <hideweboutput> tag
 3605: sub start_hideweboutput {
 3606:     my ($target,$token) = @_;
 3607:     if ($target eq 'web') {
 3608: 	&Apache::lonxml::startredirection();     
 3609:     } 
 3610:     return '';
 3611: }
 3612: 
 3613: sub end_hideweboutput {
 3614:     my ($target,$token) = @_;
 3615:     my $currentstring = '';
 3616:     if ($target eq 'web') {
 3617: 	$currentstring = &Apache::lonxml::endredirection();    
 3618:     } 
 3619:     return '';
 3620: }
 3621: 
 3622: 
 3623: sub image_replication {
 3624:     my $src = shift;
 3625:     if (not -e $src) { &Apache::lonnet::repcopy($src); }
 3626:     #replicates eps or ps 
 3627:     my $epssrc = my $pssrc = $src;
 3628:     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
 3629:     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
 3630:     if (not -e $epssrc && not -e $pssrc) {
 3631: 	my $result=&Apache::lonnet::repcopy($epssrc);
 3632: 	if ($result ne OK) { &Apache::lonnet::repcopy($pssrc); }
 3633:     }
 3634:     return '';
 3635: }
 3636: 
 3637: sub image_size {
 3638:     my ($src,$scaling,$parstack,$safeeval)=@_;
 3639:     #size of image from gif/jpg/jpeg/png 
 3640:     my $image = Image::Magick->new;
 3641:     my $current_figure = $image->Read($src);
 3642:     my $width_param = $image->Get('width') * $scaling;;
 3643:     my $height_param = $image->Get('height') * $scaling;;
 3644:     undef $image;
 3645:     #do we have any specified LaTeX size of the picture?
 3646:     my $TeXwidth = &Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval);
 3647:     my $TeXheight = &Apache::lonxml::get_param('TeXheight',$parstack,$safeeval);
 3648:     #do we have any specified web size of the picture?
 3649:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
 3650: 					   undef,1);
 3651:     if ($TeXwidth ne '') {  
 3652: 	if ($TeXwidth=~/(\d+)\s*\%/) {
 3653: 	    $width_param = $1*$ENV{'form.textwidth'}/100;
 3654: 	} else { 
 3655: 	    $width_param = $TeXwidth;
 3656: 	}
 3657:     } elsif ($TeXheight ne '') {
 3658: 	$width_param = $TeXheight/$height_param*$width_param;
 3659:     } elsif ($width ne '') {
 3660: 	$width_param = $width*$scaling;      
 3661:     }
 3662:     if ($width_param > $ENV{'form.textwidth'}) {$width_param =0.95*$ENV{'form.textwidth'}}
 3663:     return $width_param;
 3664: }
 3665: 
 3666: sub eps_generation {
 3667:     my ($src,$file,$width_param) = @_;	     
 3668:     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
 3669:     my $temp_file = Apache::File->new('>>'.$filename); 
 3670:     print $temp_file "$src\n";
 3671:     my $newsrc = $src;
 3672:     $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
 3673:     $newsrc=~s/\/home\/httpd\/html\/res//;
 3674:     $newsrc=~s/\/home\/([^\/]*)\/public_html\//\/$1\//;
 3675:     $newsrc=~s/\/\.\//\//;
 3676:     $newsrc=~s/\/([^\/]+)\.(ps|eps)/\//;
 3677:     if ($newsrc=~/\/home\/httpd\/lonUsers\//) {
 3678: 	$newsrc=~s/\/home\/httpd\/lonUsers//;
 3679: 	$newsrc=~s/\/([^\/]+)\/(\w)\/(\w)\/(\w)\//\/$1\//;
 3680:     }
 3681:     return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
 3682: }
 3683: 
 3684: sub file_path {     
 3685:     my $src=shift;
 3686:     my ($file,$path); 
 3687:     if ($src =~ m!(.*)/([^/]*)$!) {
 3688: 	$file = $2; 
 3689: 	$path = $1.'/'; 
 3690:     } 
 3691:     return $file,$path;
 3692: }
 3693: 
 3694: sub recalc {
 3695:     my $argument = shift;
 3696:     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
 3697:     $argument=~/\s*(\d+\.?\d*)\s*(mm|cm|in|pc|pt)/;
 3698:     my $value=$1;
 3699:     my $units=$2;
 3700:     if ($units eq 'cm') {
 3701: 	$value*=10;
 3702:     } elsif ($units eq 'in') {
 3703: 	$value*=25.4;
 3704:     } elsif ($units eq 'pc') {
 3705: 	$value*=(25.4*12/72.27);
 3706:     } elsif ($units eq 'pt') {
 3707: 	$value*=(25.4/72.27);
 3708:     }
 3709:     return $value.' mm';
 3710: }
 3711: 
 3712: sub LATEX_length {
 3713:     my $garbage=shift;
 3714:     $garbage=~s/^\s+$//;
 3715:     $garbage=~s/^\s+(\S.*)/$1/;#space before 
 3716:     $garbage=~s/(.*\S)\s+$/$1/;#space after 
 3717:     $garbage=~s/(\s)+/$1/;#only one space
 3718:     $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
 3719:     $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
 3720:     $garbage=~s/([^\\])\$/$1/g;#$
 3721:     $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
 3722:    $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;
 3723:     $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;
 3724:     $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;
 3725:     $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;
 3726:     $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;
 3727:     $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;
 3728:     $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
 3729:     #remove some other LaTeX command
 3730:     $garbage=~s|\\(\w+)\\|\\|g;	 
 3731:     $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
 3732:     $garbage=~s|\+|11|g;
 3733:     my  $value=length($garbage);
 3734:     return $value;
 3735: }
 3736: 
 3737: 
 3738: 
 3739: 
 3740: 1;
 3741: __END__

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