Annotation of loncom/xml/londefdef.pm, revision 1.269

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

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