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

1.1       sakharuk    1: # The LearningOnline Network with CAPA
                      2: # Tags Default Definition Module 
                      3: #
1.265   ! albertel    4: # $Id: londefdef.pm,v 1.264 2005/03/16 21:35:17 raeburn 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.118     www        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*//;
                    135: 	if ($ENV{'browser.mathml'}) {
                    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.194     albertel  154:     my $options=$ENV{'course.'.$ENV{'request.course.id'}.'.tthoptions'};
1.196     albertel  155:     &Apache::lontexconvert::init_tth();
1.186     albertel  156:     if ($target eq 'web' || $target eq 'edit') {
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.254     www       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.180     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 && 
                    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
                    497: 	if ($ENV{'browser.imagesuppress'} eq 'on') {
                    498: 	    delete($token->[2]->{'background'});
                    499: 	}
                    500: 	if ($ENV{'browser.fontenhance'} eq 'on') {
                    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: 	}
                    510: 	if ($ENV{'browser.blackwhite'} eq 'on') {
                    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.247     albertel  537: 	if ($ENV{'request.state'} ne 'construct') {
                    538: 	    $currentstring .= '<'.$token->[1];
                    539: 	}
1.122     albertel  540: 	foreach (keys %{$token->[2]}) {
                    541: 	    $currentstring.=' '.$_.'="'.$token->[2]->{$_}.'"';
                    542: 	}
1.247     albertel  543: 	if ($ENV{'request.state'} ne 'construct') {
                    544: 	    $currentstring.='>';
                    545: 	}
1.122     albertel  546: 	if ($ENV{'request.state'} ne 'published') {
1.247     albertel  547: 	    my $remote=($ENV{'environment.remote'} ne 'off');
                    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') {
                   1128: 	    $currentstring='\makebox['.$ENV{'form.textwidth'}.']{\hfill\llap{';
                   1129: 	} elsif ($align eq 'left') {
                   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.155     www      1279: 	    if (($ENV{'browser.fontenhance'} eq 'on') || 
                   1280: 		($ENV{'browser.blackwhite'} eq 'on')) { return ''; }
                   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) {
                   1828: 	    $textwidth=&recalc($ENV{'form.textwidth'}); #result is always in mm
                   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') {
                   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:
                   2500: 	my ($path,$file) = &get_eps_image($src);
1.122     albertel 2501: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
1.261     foxr     2502: 	&Apache::lonxml::debug("path = $path file = $file src = $src");
1.192     sakharuk 2503: 	if (-e $src) {
1.261     foxr     2504: 	    &Apache::lonxml::debug("$src exists");
1.256     albertel 2505: 	    my ($height_param,$width_param)=
                   2506: 		&image_size($src,0.3,$parstack,$safeeval);
1.262     foxr     2507: 	    my $destpath = $path;
1.263     foxr     2508: 	    $destpath    =~ s/ /\_/g; # Spaces in path cause LaTex to vomit.
                   2509: 	    my $destfile = $file;
                   2510: 	    $destfile    =~ s/ /\_/g;
1.262     foxr     2511: 	    $currentstring .= '\graphicspath{{'.$destpath.'}}'
1.263     foxr     2512: 		.'\includegraphics[width='.$width_param.' mm,height='.$height_param.'mm]{'.$destfile.'} ';
1.256     albertel 2513: 
1.248     foxr     2514: 	    #    If there's an alignment specification we need to honor it here.
                   2515: 	    #    For the horizontal alignments, we will also honor the
                   2516: 	    #    value of the latex specfication.  The default is parbox,
                   2517: 	    #    and that's used for illegal values too.  
                   2518: 	    #    
                   2519: 	    #    Even though we set a default alignment value, the user
                   2520: 	    #    could have given us an illegal value.  In that case we
                   2521: 	    #    just use the default alignment of bottom..
                   2522: 	    if      ($align eq "top")    {
                   2523: 		$currentstring = '\raisebox{-'.$height_param.'mm}{'.$currentstring.'}';
                   2524: 	    } elsif (($align eq "center") || ($align eq "middle")) { # Being kind
                   2525: 		my $offset = $height_param/2;
                   2526: 		$currentstring = '\raisebox{-'.$offset.'mm}{'.$currentstring.'}';
                   2527: 	    } elsif ($align eq "left")   { 
                   2528: 		if ($latex_rendering eq "parpic") { 
                   2529: 		    $currentstring = '\parpic[l]{'.$currentstring.'}';
                   2530: 		} else {    	                                 # parbox rendering
1.255     albertel 2531: 		    $currentstring = "\\strut\\newline\n".
                   2532: 			'\parbox{'.$width_param.'mm}{'.$currentstring.'}';
1.248     foxr     2533: 		}
                   2534: 	    } elsif ($align eq "right")  {   
                   2535: 		if ($latex_rendering eq "parpic") {
                   2536: 		    $currentstring = '\parpic[r]{'.$currentstring.'}';
                   2537: 		} else {	                                 # parbox rendering. 
                   2538: 		    $currentstring = '\parbox{'.$width_param.'mm}{\begin{flushright}'
                   2539: 			             .$currentstring.'\end{flushright}} \newline'."\n";
                   2540: 		}
                   2541: 	    } else {		# Bottom is also default.
                   2542: 		# $currentstring = '\raisebox{'.$height_param.'mm}{'.$currentstring.'}';
                   2543: 	    }
1.122     albertel 2544: 	} else {
1.261     foxr     2545: 	    &Apache::lonxml::debug("$src does not exist");
1.122     albertel 2546: 	    #original image file doesn't exist so check the alt attribute
                   2547: 	    my $alt = 
                   2548: 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
                   2549: 	    unless ($alt) {
                   2550: 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
                   2551: 	    }
                   2552: 
1.256     albertel 2553: 	    if ($alt) { $currentstring .= ' '.$alt.' '; }
1.59      sakharuk 2554: 	}
1.249     foxr     2555: 
                   2556: 	# And here's where the semi-quote breaks down: allow the user
                   2557:         # to edit the beast as well by rendering the problem for edit:
1.186     albertel 2558:     } elsif ($target eq 'edit') {
                   2559: 	$currentstring .=&Apache::edit::tag_start($target,$token);
                   2560: 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
                   2561: 	    &Apache::edit::browse('src',undef,'alt').' '.
                   2562: 	    &Apache::edit::search('src',undef,'alt').'<br />';
                   2563: 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
                   2564: 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
                   2565: 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
                   2566: 	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
                   2567: 	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
1.234     albertel 2568: 	$currentstring .=&Apache::edit::select_arg('Alignment:','align',
                   2569: 						   ['','bottom','middle','top','left','right'],$token,5);
1.249     foxr     2570: 	$currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap',
                   2571: 						   ['', 'parbox', 'parpic'], $token, 2);
1.186     albertel 2572: 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.249     foxr     2573: 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
                   2574: 	my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
                   2575: 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
                   2576: 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
                   2577: 
                   2578: 
1.186     albertel 2579: 	$currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
                   2580: 	if ($width) { $currentstring.=' width="'.$width.'" '; }
                   2581: 	if ($height) { $currentstring.=' height="'.$height.'" '; }
                   2582: 	$currentstring .= ' />';
                   2583:     } elsif ($target eq 'modified') {
1.210     albertel 2584: 	my ($osrc,$owidth,$oheight)=
                   2585: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
                   2586: 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
1.234     albertel 2587: 					     $safeeval,'src','alt','align',
1.249     foxr     2588: 					     'TeXwidth','TeXheight', 'TeXwrap',
1.210     albertel 2589: 					     'width','height');
                   2590: 	my ($nsrc,$nwidth,$nheight)=
                   2591: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
                   2592: 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
                   2593: 	&image_replication($loc);
                   2594: 	my ($iwidth,$iheight);
                   2595: 	if (-e $loc) {
                   2596: 	    my $image = Image::Magick->new;
                   2597: 	    $image->Read($loc);
                   2598: 	    ($iwidth, $iheight) = ($image->Get('width'),
                   2599: 				   $image->Get('height'));
                   2600: 	}
                   2601: 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
                   2602: 	    # changed image or no size specified,
                   2603:             # if they didn't explicitly change the 
                   2604:             # width or height use the ones from the image
                   2605: 	    if ($iwidth && $iheight) {
                   2606: 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
                   2607: 		    $token->[2]{'width'} = $iwidth;$ctag=1;
                   2608: 		}
                   2609: 		if ($oheight == $nheight || (!$nwidth && !$nheight)) {
                   2610: 		    $token->[2]{'height'}=$iheight;$ctag=1;
1.186     albertel 2611: 		}
                   2612: 	    }
                   2613: 	}
1.210     albertel 2614: 	my ($cwidth,$cheight)=($token->[2]{'width'},$token->[2]{'height'});
                   2615: 	# if we don't have a width or height
                   2616: 	if ($iwidth && $cwidth && !$cheight) {
                   2617: 	    $token->[2]{'height'}=int(($cwidth/$iwidth)*$iheight);$ctag=1;
                   2618: 	}
                   2619: 	if ($iheight && $cheight && !$cwidth) {
                   2620: 	    $token->[2]{'width'}=int(($cheight/$iheight)*$iwidth);$ctag=1;
                   2621: 	}
                   2622: 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
1.122     albertel 2623:     }
1.261     foxr     2624: 
1.122     albertel 2625:     return $currentstring;
                   2626: }
                   2627: 
                   2628: sub end_img {
                   2629:     my ($target,$token) = @_;
                   2630:     my $currentstring = '';
                   2631:     if ($target eq 'web') {
                   2632: 	$currentstring = $token->[2];
                   2633:     } elsif ($target eq 'tex') {
                   2634: 	$currentstring = '';
                   2635:     }
                   2636:     return $currentstring;
                   2637: }
                   2638: 
1.181     sakharuk 2639: #-- <applet> tag (end tag required)
1.122     albertel 2640: sub start_applet {
                   2641:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2642:     
                   2643:     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
                   2644:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$code;
                   2645:     
                   2646:     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
                   2647: 					   undef,1);
                   2648:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$archive;
                   2649:     
                   2650:     my $currentstring = '';
                   2651:     if ($target eq 'web') {
                   2652: 	if ($ENV{'browser.appletsuppress'} ne 'on') {
1.250     albertel 2653: 	    $currentstring = &Apache::lonenc::encrypt_ref($token,
                   2654: 							  {'code'=>$code,
                   2655: 							   'archive'=>$archive}
                   2656: 							  );
1.122     albertel 2657: 	} else {
                   2658: 	    my $alttag= &Apache::lonxml::get_param('alt',$parstack,
                   2659: 						   $safeeval,undef,1);
                   2660: 	    unless ($alttag) {
                   2661: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
                   2662: 						 $code);
1.44      sakharuk 2663: 	    }
1.122     albertel 2664: 	    $currentstring='[APPLET: '.$alttag.']';
1.1       sakharuk 2665: 	}
1.122     albertel 2666:     } elsif ($target eq 'tex') {
1.177     albertel 2667: 	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
                   2668: 					       $safeeval,undef,1);
                   2669: 	unless ($alttag) {
                   2670: 	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
                   2671: 						undef,1);
                   2672: 	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
                   2673: 					     $code);
1.175     sakharuk 2674: 	}
1.177     albertel 2675: 	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
                   2676: 	    '.}\end{center}';
1.122     albertel 2677:     } 
                   2678:     return $currentstring;
                   2679: }
                   2680: 
                   2681: sub end_applet {
                   2682:     my ($target,$token) = @_;
                   2683:     my $currentstring = '';
                   2684:     if ($target eq 'web') {
                   2685: 	$currentstring = $token->[2];
                   2686:     } elsif ($target eq 'tex') {
                   2687:     } 
                   2688:     return $currentstring;
                   2689: }
                   2690: 
1.181     sakharuk 2691: #-- <embed> tag (end tag optional/required)
1.122     albertel 2692: sub start_embed {    
                   2693:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2694:     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
                   2695:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=$src;
                   2696:     my $currentstring = '';
                   2697:     if ($target eq 'web') {
                   2698: 	if ($ENV{'browser.embedsuppress'} ne 'on') {
1.250     albertel 2699: 	    $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src});
1.122     albertel 2700: 	} else {
                   2701: 	    my $alttag=&Apache::lonxml::get_param
                   2702: 		('alt',$parstack,$safeeval,undef,1);
                   2703: 	    unless ($alttag) {
                   2704: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
1.42      albertel 2705: 	    }
1.122     albertel 2706: 	    $currentstring='[EMBED: '.$alttag.']';
1.10      www      2707: 	}
1.122     albertel 2708:     } elsif ($target eq 'tex') {
                   2709:     } 
                   2710:     return $currentstring;
                   2711: }
                   2712: 
                   2713: sub end_embed {
                   2714:     my ($target,$token) = @_;
                   2715:     my $currentstring = '';
                   2716:     if ($target eq 'web') {
                   2717: 	$currentstring = $token->[2];     
1.175     sakharuk 2718:     } elsif ($target eq 'tex') {  
1.122     albertel 2719:     } 
                   2720:     return $currentstring;
                   2721: }
                   2722: 
1.181     sakharuk 2723: #-- <param> tag (end tag forbidden)
1.122     albertel 2724: sub start_param {
                   2725:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2726:     if (&Apache::lonxml::get_param
                   2727: 	('name',$parstack,$safeeval,undef,1)=~/^cabbase$/i) {
                   2728: 	$Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
                   2729: 	    &Apache::lonxml::get_param('value',$parstack,$safeeval,undef,1);
                   2730:     }   
                   2731:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=   
                   2732: 	&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
                   2733:     my $currentstring = '';
                   2734:     if ($target eq 'web') {
1.250     albertel 2735: 	my %toconvert;
                   2736: 	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
                   2737: 	if ($src) { $toconvert{'src'}= $src; }
                   2738: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval,
                   2739: 					    undef,1);
1.251     albertel 2740: 	if ($name=~/^cabbase$/i) {
                   2741: 	    $toconvert{'value'}=&Apache::lonxml::get_param('value',$parstack,
                   2742: 							   $safeeval,undef,1);
                   2743: 	}
1.250     albertel 2744: 	$currentstring = &Apache::lonenc::encrypt_ref($token,\%toconvert);
1.122     albertel 2745:     } elsif ($target eq 'tex') {
                   2746:     } 
                   2747:     return $currentstring;
                   2748: }
                   2749: 
                   2750: sub end_param {
                   2751:     my ($target,$token) = @_;
                   2752:     my $currentstring = '';
                   2753:     if ($target eq 'web') {
                   2754: 	$currentstring = $token->[2];     
                   2755:     } elsif ($target eq 'tex') {
                   2756:     } 
                   2757:     return $currentstring;
                   2758: }
                   2759: 
                   2760: #-- <allow> tag
                   2761: sub start_allow {
                   2762:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2763:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
                   2764:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
1.138     www      2765:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
                   2766: 	&Apache::lonnet::clutter($src);
1.241     albertel 2767:     if ($target eq 'tex') { &image_replication($src); }
1.122     albertel 2768:     my $result;
                   2769:     if ($target eq 'edit') {
                   2770: 	$result .=&Apache::edit::tag_start($target,$token);
                   2771: 	$result .=&Apache::edit::text_arg('File Spec:','src',$token,70);
                   2772: 	$result .=&Apache::edit::end_row();#.&Apache::edit::start_spanning_row();
                   2773:     } elsif ($target eq 'modified') {
                   2774: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   2775: 						     $safeeval,'src');
                   2776: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   2777:     }
                   2778:     return $result;
                   2779: }
                   2780: 
                   2781: sub end_allow {
                   2782:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2783:     if ( $target eq 'edit') { return (&Apache::edit::end_table()); }
                   2784:     return '';
                   2785: }
1.119     www      2786: 
1.181     sakharuk 2787: #-- Frames (end tag required)
                   2788: #-- <frameset>
1.122     albertel 2789: sub start_frameset {
                   2790:     my ($target,$token) = @_;
                   2791:     my $currentstring = '';
                   2792:     if ($target eq 'web') { 
1.180     albertel 2793: 	if (!$Apache::lonxml::registered &&
                   2794: 	    $ENV{'request.state'} eq 'published') {
1.122     albertel 2795: 	    $currentstring.='<head>'.
                   2796: 		&Apache::lonmenu::registerurl(undef,$target).'</head>';
1.10      www      2797: 	}
1.133     www      2798: 	my $onLoad='';
                   2799: 	foreach my $key (keys(%{$token->[2]})) {
                   2800: 	    if ($key =~ /^onload$/i) {
                   2801: 		$onLoad.=$token->[2]->{$key}.';';
                   2802: 		delete($token->[2]->{$key});
                   2803: 	    }
                   2804: 	}
                   2805: 	$token->[2]->{'onload'}=&Apache::lonmenu::loadevents().';'.$onLoad;
                   2806: 	my $onUnload='';
                   2807: 	foreach my $key (keys(%{$token->[2]})) {
                   2808: 	    if ($key =~ /^onunload$/i) {
                   2809: 		$onUnload.=$token->[2]->{$key}.';';
                   2810: 		delete($token->[2]->{$key});
                   2811: 	    }
                   2812: 	}
                   2813: 	$token->[2]->{'onunload'}=&Apache::lonmenu::unloadevents().
                   2814: 	    ';'.$onUnload;
                   2815: 	
                   2816: 	$currentstring .= '<'.$token->[1];
                   2817: 	foreach (keys %{$token->[2]}) {
                   2818: 	    $currentstring.=' '.$_.'="'.$token->[2]->{$_}.'"';
                   2819: 	}
                   2820: 	$currentstring.='>';
1.122     albertel 2821:     }
                   2822:     return $currentstring;
                   2823: }
                   2824: 
                   2825: sub end_frameset {
                   2826:     my ($target,$token) = @_;
                   2827:     my $currentstring = '';
                   2828:     if ($target eq 'web') {
                   2829: 	$currentstring = $token->[2];
                   2830:     }
                   2831:     return $currentstring;
                   2832: }
1.162     sakharuk 2833: 
1.181     sakharuk 2834: #-- <xmp> (end tag required)
1.162     sakharuk 2835: sub start_xmp {
                   2836:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2837:     my $currentstring = '';
                   2838:     if ($target eq 'web') {
                   2839: 	$currentstring .= $token->[4];
                   2840:     } elsif ($target eq 'tex') {
                   2841: 	$currentstring .= '\begin{verbatim}';
                   2842:     } 
                   2843:     return $currentstring;
                   2844: }
                   2845: 
                   2846: sub end_xmp {
                   2847:     my ($target,$token) = @_;
                   2848:     my $currentstring = '';
                   2849:     if ($target eq 'web') {
                   2850: 	$currentstring .= $token->[2];
                   2851:     } elsif ($target eq 'tex') {
                   2852: 	$currentstring .= '\end{verbatim}';
                   2853:     }
                   2854:     return $currentstring;
                   2855: }
                   2856: 
1.181     sakharuk 2857: #-- <pre> (end tag required)
1.122     albertel 2858: sub start_pre {
1.126     sakharuk 2859:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
1.122     albertel 2860:     my $currentstring = '';
                   2861:     if ($target eq 'web') {
                   2862: 	$currentstring .= $token->[4];
                   2863:     } elsif ($target eq 'tex') {
1.136     sakharuk 2864: 	$currentstring .= '\begin{verbatim}';
1.122     albertel 2865:     } 
                   2866:     return $currentstring;
                   2867: }
                   2868: 
                   2869: sub end_pre {
                   2870:     my ($target,$token) = @_;
                   2871:     my $currentstring = '';
                   2872:     if ($target eq 'web') {
                   2873: 	$currentstring .= $token->[2];
                   2874:     } elsif ($target eq 'tex') {
1.136     sakharuk 2875: 	$currentstring .= '\end{verbatim}';
1.122     albertel 2876:     }
                   2877:     return $currentstring;
                   2878: }
                   2879: 
                   2880: #-- <insert>
                   2881: sub start_insert {
                   2882:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2883:     my $currentstring = '';
                   2884:     if ($target eq 'web') {
                   2885: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
                   2886: 	$currentstring .= '<b>'.$display.'</b>';;
                   2887:     }
                   2888:     return $currentstring;
                   2889: }
                   2890: 
                   2891: sub end_insert {
                   2892:     my ($target,$token) = @_;
                   2893:     my $currentstring = '';
                   2894:     if ($target eq 'web') {
                   2895: 	$currentstring .= '';
                   2896:     }
                   2897:     return $currentstring;
                   2898: }
                   2899: 
                   2900: #-- <externallink>
                   2901: sub start_externallink {
                   2902:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2903:     my $currentstring = '';
                   2904:     if ($target eq 'web') {
                   2905: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
                   2906: 	$currentstring .= '<b>'.$display.'</b>';;
                   2907:     }
                   2908:     return $currentstring;
                   2909: }
                   2910: 
                   2911: sub end_externallink {
                   2912:     my ($target,$token) = @_;
                   2913:     my $currentstring = '';
                   2914:     if ($target eq 'web') {
                   2915: 	$currentstring .= '';
                   2916:     }
                   2917:     return $currentstring;
                   2918: }
                   2919: 
                   2920: #-- <blankspace heigth="">
                   2921: sub start_blankspace {
                   2922:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2923:     my $currentstring = '';
                   2924:     if ($target eq 'tex') {
                   2925: 	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
                   2926: 	$currentstring .= '\vskip '.$howmuch.' ';
                   2927:     }
                   2928:     return $currentstring;
                   2929: }
                   2930: 
                   2931: sub end_blankspace {
                   2932:     my ($target,$token) = @_;
                   2933:     my $currentstring = '';
                   2934:     if ($target eq 'tex') {
                   2935: 	$currentstring .= '';
                   2936:     }
                   2937:     return $currentstring;
                   2938: }
                   2939: 
1.181     sakharuk 2940: #-- <abbr> tag (end tag required)
1.122     albertel 2941: sub start_abbr {
                   2942:     my ($target,$token) = @_;
                   2943:     my $currentstring = '';
                   2944:     if ($target eq 'web') {
                   2945: 	$currentstring = $token->[4];     
                   2946:     } 
                   2947:     return $currentstring;
                   2948: }
                   2949: 
                   2950: sub end_abbr {
                   2951:     my ($target,$token) = @_;
                   2952:     my $currentstring = '';
                   2953:     if ($target eq 'web') {
                   2954: 	$currentstring = $token->[2];    
                   2955:     } 
                   2956:     return $currentstring;
                   2957: }
                   2958: 
1.181     sakharuk 2959: #-- <acronym> tag (end tag required)
1.122     albertel 2960: sub start_acronym {
                   2961:     my ($target,$token) = @_;
                   2962:     my $currentstring = '';
                   2963:     if ($target eq 'web') {
                   2964: 	$currentstring = $token->[4];     
                   2965:     } 
                   2966:     return $currentstring;
                   2967: }
                   2968: 
                   2969: sub end_acronym {
                   2970:     my ($target,$token) = @_;
                   2971:     my $currentstring = '';
                   2972:     if ($target eq 'web') {
                   2973: 	$currentstring = $token->[2];    
                   2974:     } 
                   2975:     return $currentstring;
                   2976: }
                   2977: 
1.181     sakharuk 2978: #-- <area> tag (end tag forbidden)
1.122     albertel 2979: sub start_area {
                   2980:     my ($target,$token) = @_;
                   2981:     my $currentstring = '';
                   2982:     if ($target eq 'web') {
                   2983: 	$currentstring = $token->[4];     
                   2984:     } 
                   2985:     return $currentstring;
                   2986: }
                   2987: 
                   2988: sub end_area {
                   2989:     my ($target,$token) = @_;
                   2990:     my $currentstring = '';
                   2991:     if ($target eq 'web') {
                   2992: 	$currentstring = $token->[2];    
                   2993:     } 
                   2994:     return $currentstring;
                   2995: }
                   2996: 
1.181     sakharuk 2997: #-- <base> tag (end tag forbidden)
1.122     albertel 2998: sub start_base {
                   2999:     my ($target,$token) = @_;
                   3000:     my $currentstring = '';
                   3001:     if ($target eq 'web') {
                   3002: 	$currentstring = $token->[4];     
1.161     sakharuk 3003:     }
1.122     albertel 3004:     return $currentstring;
                   3005: }
                   3006: 
                   3007: sub end_base {
                   3008:     my ($target,$token) = @_;
                   3009:     my $currentstring = '';
                   3010:     if ($target eq 'web') {
                   3011: 	$currentstring = $token->[2];    
                   3012:     } 
                   3013:     return $currentstring;
                   3014: }
                   3015: 
1.181     sakharuk 3016: #-- <bdo> tag (end tag required)
1.122     albertel 3017: sub start_bdo {
                   3018:     my ($target,$token) = @_;
                   3019:     my $currentstring = '';
                   3020:     if ($target eq 'web') {
                   3021: 	$currentstring = $token->[4];     
                   3022:     } 
                   3023:     return $currentstring;
                   3024: }
                   3025: 
                   3026: sub end_bdo {
                   3027:     my ($target,$token) = @_;
                   3028:     my $currentstring = '';
                   3029:     if ($target eq 'web') {
                   3030: 	$currentstring = $token->[2];    
                   3031:     } 
                   3032:     return $currentstring;
                   3033: }
                   3034: 
1.181     sakharuk 3035: #-- <bgsound> tag (end tag optional)
1.122     albertel 3036: sub start_bgsound {
                   3037:     my ($target,$token) = @_;
                   3038:     my $currentstring = '';
                   3039:     if ($target eq 'web') {
                   3040: 	$currentstring = $token->[4];     
                   3041:     } 
                   3042:     return $currentstring;
                   3043: }
                   3044: 
                   3045: sub end_bgsound {
                   3046:     my ($target,$token) = @_;
                   3047:     my $currentstring = '';
                   3048:     if ($target eq 'web') {
                   3049: 	$currentstring = $token->[2];    
                   3050:     } 
                   3051:     return $currentstring;
                   3052: }
                   3053: 
1.181     sakharuk 3054: #-- <blink> tag (end tag required)
1.122     albertel 3055: sub start_blink {
                   3056:     my ($target,$token) = @_;
                   3057:     my $currentstring = '';
                   3058:     if ($target eq 'web') {
                   3059: 	$currentstring = $token->[4];     
                   3060:     } 
                   3061:     return $currentstring;
                   3062: }
                   3063: 
                   3064: sub end_blink {
                   3065:     my ($target,$token) = @_;
                   3066:     my $currentstring = '';
                   3067:     if ($target eq 'web') {
                   3068: 	$currentstring = $token->[2];    
                   3069:     } 
                   3070:     return $currentstring;
                   3071: }
                   3072: 
1.181     sakharuk 3073: #-- <blockquote> tag (end tag required)
1.122     albertel 3074: sub start_blockquote {
                   3075:     my ($target,$token) = @_;
                   3076:     my $currentstring = '';
                   3077:     if ($target eq 'web') {
                   3078: 	$currentstring = $token->[4];     
                   3079:     } 
                   3080:     return $currentstring;
                   3081: }
                   3082: 
                   3083: sub end_blockquote {
                   3084:     my ($target,$token) = @_;
                   3085:     my $currentstring = '';
                   3086:     if ($target eq 'web') {
                   3087: 	$currentstring = $token->[2];    
                   3088:     } 
                   3089:     return $currentstring;
                   3090: }
                   3091: 
1.181     sakharuk 3092: #-- <button> tag (end tag required)
1.122     albertel 3093: sub start_button {
                   3094:     my ($target,$token) = @_;
                   3095:     my $currentstring = '';
                   3096:     if ($target eq 'web') {
                   3097: 	$currentstring = $token->[4];     
                   3098:     } 
                   3099:     return $currentstring;
                   3100: }
                   3101: 
                   3102: sub end_button {
                   3103:     my ($target,$token) = @_;
                   3104:     my $currentstring = '';
                   3105:     if ($target eq 'web') {
                   3106: 	$currentstring = $token->[2];    
                   3107:     } 
                   3108:     return $currentstring;
                   3109: }
                   3110: 
1.181     sakharuk 3111: #-- <caption> tag (end tag required)
1.122     albertel 3112: sub start_caption {
                   3113:     my ($target,$token) = @_;
                   3114:     my $currentstring = '';
                   3115:     if ($target eq 'web') {
                   3116: 	$currentstring = $token->[4];     
                   3117:     } 
                   3118:     return $currentstring;
                   3119: }
                   3120: 
                   3121: sub end_caption {
                   3122:     my ($target,$token) = @_;
                   3123:     my $currentstring = '';
                   3124:     if ($target eq 'web') {
                   3125: 	$currentstring = $token->[2];    
                   3126:     } 
                   3127:     return $currentstring;
                   3128: }
                   3129: 
1.181     sakharuk 3130: #-- <col> tag (end tag forbdden)
1.122     albertel 3131: sub start_col {
                   3132:     my ($target,$token) = @_;
                   3133:     my $currentstring = '';
                   3134:     if ($target eq 'web') {
                   3135: 	$currentstring = $token->[4];     
                   3136:     } 
                   3137:     return $currentstring;
                   3138: }
                   3139: 
                   3140: sub end_col {
                   3141:     my ($target,$token) = @_;
                   3142:     my $currentstring = '';
                   3143:     if ($target eq 'web') {
                   3144: 	$currentstring = $token->[2];    
                   3145:     } 
                   3146:     return $currentstring;
                   3147: }
                   3148: 
1.181     sakharuk 3149: #-- <colgroup> tag (end tag optional)
1.122     albertel 3150: sub start_colgroup {
                   3151:     my ($target,$token) = @_;
                   3152:     my $currentstring = '';
                   3153:     if ($target eq 'web') {
                   3154: 	$currentstring = $token->[4];     
                   3155:     } 
                   3156:     return $currentstring;
                   3157: }
                   3158: 
                   3159: sub end_colgroup {
                   3160:     my ($target,$token) = @_;
                   3161:     my $currentstring = '';
                   3162:     if ($target eq 'web') {
                   3163: 	$currentstring = $token->[2];    
                   3164:     } 
                   3165:     return $currentstring;
                   3166: }
                   3167: 
1.181     sakharuk 3168: #-- <del> tag (end tag required)
1.122     albertel 3169: sub start_del {
                   3170:     my ($target,$token) = @_;
                   3171:     my $currentstring = '';
                   3172:     if ($target eq 'web') {
                   3173: 	$currentstring = $token->[4];     
                   3174:     } 
                   3175:     return $currentstring;
                   3176: }
                   3177: 
                   3178: sub end_del {
                   3179:     my ($target,$token) = @_;
                   3180:     my $currentstring = '';
                   3181:     if ($target eq 'web') {
                   3182: 	$currentstring = $token->[2];    
                   3183:     } 
                   3184:     return $currentstring;
                   3185: }
                   3186: 
1.181     sakharuk 3187: #-- <fieldset> tag (end tag required)
1.122     albertel 3188: sub start_fieldset {
                   3189:     my ($target,$token) = @_;
                   3190:     my $currentstring = '';
                   3191:     if ($target eq 'web') {
                   3192: 	$currentstring = $token->[4];     
                   3193:     } 
                   3194:     return $currentstring;
                   3195: }
                   3196: 
                   3197: sub end_fieldset {
                   3198:     my ($target,$token) = @_;
                   3199:     my $currentstring = '';
                   3200:     if ($target eq 'web') {
                   3201: 	$currentstring = $token->[2];    
                   3202:     } 
                   3203:     return $currentstring;
                   3204: }
                   3205: 
1.181     sakharuk 3206: #-- <frame> tag (end tag forbidden)
1.122     albertel 3207: sub start_frame {
                   3208:     my ($target,$token) = @_;
                   3209:     my $currentstring = '';
                   3210:     if ($target eq 'web') {
                   3211: 	$currentstring = $token->[4];     
                   3212:     } 
                   3213:     return $currentstring;
                   3214: }
                   3215: 
                   3216: sub end_frame {
                   3217:     my ($target,$token) = @_;
                   3218:     my $currentstring = '';
                   3219:     if ($target eq 'web') {
                   3220: 	$currentstring = $token->[2];    
                   3221:     } 
                   3222:     return $currentstring;
                   3223: }
                   3224: 
1.181     sakharuk 3225: #-- <iframe> tag (end tag required)
1.122     albertel 3226: sub start_iframe {
                   3227:     my ($target,$token) = @_;
                   3228:     my $currentstring = '';
                   3229:     if ($target eq 'web') {
                   3230: 	$currentstring = $token->[4];     
                   3231:     } 
                   3232:     return $currentstring;
                   3233: }
                   3234: 
                   3235: sub end_iframe {
                   3236:     my ($target,$token) = @_;
                   3237:     my $currentstring = '';
                   3238:     if ($target eq 'web') {
                   3239: 	$currentstring = $token->[2];    
                   3240:     } 
                   3241:     return $currentstring;
                   3242: }
                   3243: 
1.181     sakharuk 3244: #-- <ins> tag (end tag required)
1.122     albertel 3245: sub start_ins {
                   3246:     my ($target,$token) = @_;
                   3247:     my $currentstring = '';
                   3248:     if ($target eq 'web') {
                   3249: 	$currentstring = $token->[4];     
                   3250:     } 
                   3251:     return $currentstring;
                   3252: }
                   3253: 
                   3254: sub end_ins {
                   3255:     my ($target,$token) = @_;
                   3256:     my $currentstring = '';
                   3257:     if ($target eq 'web') {
                   3258: 	$currentstring = $token->[2];    
                   3259:     } 
                   3260:     return $currentstring;
                   3261: }
                   3262: 
1.181     sakharuk 3263: #-- <isindex> tag (end tag forbidden)
1.122     albertel 3264: sub start_isindex {
                   3265:     my ($target,$token) = @_;
                   3266:     my $currentstring = '';
                   3267:     if ($target eq 'web') {
                   3268: 	$currentstring = $token->[4];     
                   3269:     } 
                   3270:     return $currentstring;
                   3271: }
                   3272: 
                   3273: sub end_isindex {
                   3274:     my ($target,$token) = @_;
                   3275:     my $currentstring = '';
                   3276:     if ($target eq 'web') {
                   3277: 	$currentstring = $token->[2];    
                   3278:     } 
                   3279:     return $currentstring;
                   3280: }
                   3281: 
1.181     sakharuk 3282: #-- <keygen> tag (end tag forbidden)
1.122     albertel 3283: sub start_keygen {
                   3284:     my ($target,$token) = @_;
                   3285:     my $currentstring = '';
                   3286:     if ($target eq 'web') {
                   3287: 	$currentstring = $token->[4];     
                   3288:     } 
                   3289:     return $currentstring;
                   3290: }
                   3291: 
                   3292: sub end_keygen {
                   3293:     my ($target,$token) = @_;
                   3294:     my $currentstring = '';
                   3295:     if ($target eq 'web') {
                   3296: 	$currentstring = $token->[2];    
                   3297:     } 
                   3298:     return $currentstring;
                   3299: }
                   3300: 
                   3301: #-- <label> tag
                   3302: sub start_label {
                   3303:     my ($target,$token) = @_;
                   3304:     my $currentstring = '';
                   3305:     if ($target eq 'web') {
                   3306: 	$currentstring = $token->[4];     
                   3307:     } 
                   3308:     return $currentstring;
                   3309: }
                   3310: 
                   3311: sub end_label {
                   3312:     my ($target,$token) = @_;
                   3313:     my $currentstring = '';
                   3314:     if ($target eq 'web') {
                   3315: 	$currentstring = $token->[2];    
                   3316:     } 
                   3317:     return $currentstring;
                   3318: }
                   3319: 
1.181     sakharuk 3320: #-- <layer> tag (end tag required)
1.122     albertel 3321: sub start_layer {
                   3322:     my ($target,$token) = @_;
                   3323:     my $currentstring = '';
                   3324:     if ($target eq 'web') {
                   3325: 	$currentstring = $token->[4];     
                   3326:     } 
                   3327:     return $currentstring;
                   3328: }
                   3329: 
                   3330: sub end_layer {
                   3331:     my ($target,$token) = @_;
                   3332:     my $currentstring = '';
                   3333:     if ($target eq 'web') {
                   3334: 	$currentstring = $token->[2];    
                   3335:     } 
                   3336:     return $currentstring;
                   3337: }
                   3338: 
1.181     sakharuk 3339: #-- <legend> tag (end tag required)
1.122     albertel 3340: sub start_legend {
                   3341:     my ($target,$token) = @_;
                   3342:     my $currentstring = '';
                   3343:     if ($target eq 'web') {
                   3344: 	$currentstring = $token->[4];     
                   3345:     } 
                   3346:     return $currentstring;
                   3347: }
                   3348: 
                   3349: sub end_legend {
                   3350:     my ($target,$token) = @_;
                   3351:     my $currentstring = '';
                   3352:     if ($target eq 'web') {
                   3353: 	$currentstring = $token->[2];    
                   3354:     } 
                   3355:     return $currentstring;
                   3356: }
                   3357: 
1.181     sakharuk 3358: #-- <link> tag (end tag forbidden)
1.122     albertel 3359: sub start_link {
                   3360:     my ($target,$token) = @_;
                   3361:     my $currentstring = '';
                   3362:     if ($target eq 'web') {
                   3363: 	$currentstring = $token->[4];     
                   3364:     } 
                   3365:     return $currentstring;
                   3366: }
                   3367: 
                   3368: sub end_link {
                   3369:     my ($target,$token) = @_;
                   3370:     my $currentstring = '';
                   3371:     if ($target eq 'web') {
                   3372: 	$currentstring = $token->[2];    
                   3373:     } 
                   3374:     return $currentstring;
                   3375: }
                   3376: 
1.181     sakharuk 3377: #-- <marquee> tag (end tag optional)
1.122     albertel 3378: sub start_marquee {
                   3379:     my ($target,$token) = @_;
                   3380:     my $currentstring = '';
                   3381:     if ($target eq 'web') {
                   3382: 	$currentstring = $token->[4];     
                   3383:     } 
                   3384:     return $currentstring;
                   3385: }
                   3386: 
                   3387: sub end_marquee {
                   3388:     my ($target,$token) = @_;
                   3389:     my $currentstring = '';
                   3390:     if ($target eq 'web') {
                   3391: 	$currentstring = $token->[2];    
                   3392:     } 
                   3393:     return $currentstring;
                   3394: }
                   3395: 
1.179     sakharuk 3396: #-- <multicol> tag (end tag required)
                   3397: sub start_multicol {
1.122     albertel 3398:     my ($target,$token) = @_;
                   3399:     my $currentstring = '';
                   3400:     if ($target eq 'web') {
                   3401: 	$currentstring = $token->[4];     
                   3402:     } 
                   3403:     return $currentstring;
                   3404: }
                   3405: 
1.179     sakharuk 3406: sub end_multicol {
1.122     albertel 3407:     my ($target,$token) = @_;
                   3408:     my $currentstring = '';
                   3409:     if ($target eq 'web') {
                   3410: 	$currentstring = $token->[2];    
                   3411:     } 
                   3412:     return $currentstring;
                   3413: }
                   3414: 
1.179     sakharuk 3415: #-- <nobr> tag (end tag required)
1.122     albertel 3416: sub start_nobr {
                   3417:     my ($target,$token) = @_;
                   3418:     my $currentstring = '';
                   3419:     if ($target eq 'web') {
                   3420: 	$currentstring = $token->[4];     
1.179     sakharuk 3421:     }  elsif ($target eq 'tex') {
                   3422: 	$currentstring='\mbox{';
                   3423:     }
1.122     albertel 3424:     return $currentstring;
                   3425: }
                   3426: 
                   3427: sub end_nobr {
                   3428:     my ($target,$token) = @_;
                   3429:     my $currentstring = '';
                   3430:     if ($target eq 'web') {
                   3431: 	$currentstring = $token->[2];    
1.179     sakharuk 3432:     }   elsif ($target eq 'tex') {
                   3433: 	$currentstring='}';
                   3434:     }
1.122     albertel 3435:     return $currentstring;
                   3436: }
                   3437: 
1.179     sakharuk 3438: #-- <noembed> tag (end tag required)
1.122     albertel 3439: sub start_noembed {
                   3440:     my ($target,$token) = @_;
                   3441:     my $currentstring = '';
                   3442:     if ($target eq 'web') {
                   3443: 	$currentstring = $token->[4];     
                   3444:     } 
                   3445:     return $currentstring;
                   3446: }
                   3447: 
                   3448: sub end_noembed {
                   3449:     my ($target,$token) = @_;
                   3450:     my $currentstring = '';
                   3451:     if ($target eq 'web') {
                   3452: 	$currentstring = $token->[2];    
                   3453:     } 
                   3454:     return $currentstring;
                   3455: }
                   3456: 
1.179     sakharuk 3457: #-- <noframes> tag (end tag required)
1.122     albertel 3458: sub start_noframes {
                   3459:     my ($target,$token) = @_;
                   3460:     my $currentstring = '';
                   3461:     if ($target eq 'web') {
                   3462: 	$currentstring = $token->[4];     
                   3463:     } 
                   3464:     return $currentstring;
                   3465: }
                   3466: 
                   3467: sub end_noframes {
                   3468:     my ($target,$token) = @_;
                   3469:     my $currentstring = '';
                   3470:     if ($target eq 'web') {
                   3471: 	$currentstring = $token->[2];    
                   3472:     } 
                   3473:     return $currentstring;
                   3474: }
                   3475: 
1.179     sakharuk 3476: #-- <nolayer> tag (end tag required)
1.122     albertel 3477: sub start_nolayer {
                   3478:     my ($target,$token) = @_;
                   3479:     my $currentstring = '';
                   3480:     if ($target eq 'web') {
                   3481: 	$currentstring = $token->[4];     
                   3482:     } 
                   3483:     return $currentstring;
                   3484: }
                   3485: 
                   3486: sub end_nolayer {
                   3487:     my ($target,$token) = @_;
                   3488:     my $currentstring = '';
                   3489:     if ($target eq 'web') {
                   3490: 	$currentstring = $token->[2];    
                   3491:     } 
                   3492:     return $currentstring;
                   3493: }
                   3494: 
1.179     sakharuk 3495: #-- <noscript> tag (end tag required)
1.122     albertel 3496: sub start_noscript {
                   3497:     my ($target,$token) = @_;
                   3498:     my $currentstring = '';
                   3499:     if ($target eq 'web') {
                   3500: 	$currentstring = $token->[4];     
                   3501:     } 
                   3502:     return $currentstring;
                   3503: }
                   3504: 
                   3505: sub end_noscript {
                   3506:     my ($target,$token) = @_;
                   3507:     my $currentstring = '';
                   3508:     if ($target eq 'web') {
                   3509: 	$currentstring = $token->[2];    
                   3510:     } 
                   3511:     return $currentstring;
                   3512: }
                   3513: 
1.179     sakharuk 3514: #-- <object> tag (end tag required)
1.122     albertel 3515: sub start_object {
                   3516:     my ($target,$token) = @_;
                   3517:     my $currentstring = '';
                   3518:     if ($target eq 'web') {
                   3519: 	$currentstring = $token->[4];     
                   3520:     } 
                   3521:     return $currentstring;
                   3522: }
                   3523: 
                   3524: sub end_object {
                   3525:     my ($target,$token) = @_;
                   3526:     my $currentstring = '';
                   3527:     if ($target eq 'web') {
                   3528: 	$currentstring = $token->[2];    
                   3529:     } 
                   3530:     return $currentstring;
                   3531: }
                   3532: 
1.179     sakharuk 3533: #-- <optgroup> tag (end tag required)
1.122     albertel 3534: sub start_optgroup {
                   3535:     my ($target,$token) = @_;
                   3536:     my $currentstring = '';
                   3537:     if ($target eq 'web') {
                   3538: 	$currentstring = $token->[4];     
                   3539:     } 
                   3540:     return $currentstring;
                   3541: }
                   3542: 
                   3543: sub end_optgroup {
                   3544:     my ($target,$token) = @_;
                   3545:     my $currentstring = '';
                   3546:     if ($target eq 'web') {
                   3547: 	$currentstring = $token->[2];    
                   3548:     } 
                   3549:     return $currentstring;
                   3550: }
                   3551: 
1.179     sakharuk 3552: #-- <samp> tag (end tag required)
1.122     albertel 3553: sub start_samp {
                   3554:     my ($target,$token) = @_;
                   3555:     my $currentstring = '';
                   3556:     if ($target eq 'web') {
                   3557: 	$currentstring = $token->[4];     
1.179     sakharuk 3558:     } elsif ($target eq 'tex') {
                   3559: 	$currentstring='\texttt{';
                   3560:     }
1.122     albertel 3561:     return $currentstring;
                   3562: }
                   3563: 
                   3564: sub end_samp {
                   3565:     my ($target,$token) = @_;
                   3566:     my $currentstring = '';
                   3567:     if ($target eq 'web') {
                   3568: 	$currentstring = $token->[2];    
1.179     sakharuk 3569:     } elsif ($target eq 'tex') {
                   3570: 	$currentstring='}';
                   3571:     }
1.122     albertel 3572:     return $currentstring;
                   3573: }
                   3574: 
                   3575: #-- <server> tag
                   3576: sub start_server {
                   3577:     my ($target,$token) = @_;
                   3578:     my $currentstring = '';
                   3579:     if ($target eq 'web') {
                   3580: 	$currentstring = $token->[4];     
                   3581:     } 
                   3582:     return $currentstring;
                   3583: }
                   3584: 
                   3585: sub end_server {
                   3586:     my ($target,$token) = @_;
                   3587:     my $currentstring = '';
                   3588:     if ($target eq 'web') {
                   3589: 	$currentstring = $token->[2];    
                   3590:     } 
                   3591:     return $currentstring;
                   3592: }
                   3593: 
1.179     sakharuk 3594: #-- <spacer> tag (end tag forbidden)
1.122     albertel 3595: sub start_spacer {
                   3596:     my ($target,$token) = @_;
                   3597:     my $currentstring = '';
                   3598:     if ($target eq 'web') {
                   3599: 	$currentstring = $token->[4];     
                   3600:     } 
                   3601:     return $currentstring;
                   3602: }
                   3603: 
                   3604: sub end_spacer {
                   3605:     my ($target,$token) = @_;
                   3606:     my $currentstring = '';
                   3607:     if ($target eq 'web') {
                   3608: 	$currentstring = $token->[2];    
                   3609:     } 
                   3610:     return $currentstring;
                   3611: }
                   3612: 
1.179     sakharuk 3613: #-- <span> tag (end tag required)
1.122     albertel 3614: sub start_span {
                   3615:     my ($target,$token) = @_;
                   3616:     my $currentstring = '';
                   3617:     if ($target eq 'web') {
                   3618: 	$currentstring = $token->[4];     
                   3619:     } 
                   3620:     return $currentstring;
                   3621: }
                   3622: 
                   3623: sub end_span {
                   3624:     my ($target,$token) = @_;
                   3625:     my $currentstring = '';
                   3626:     if ($target eq 'web') {
                   3627: 	$currentstring = $token->[2];    
                   3628:     } 
                   3629:     return $currentstring;
                   3630: }
                   3631: 
1.179     sakharuk 3632: #-- <tbody> tag (end tag optional)
1.122     albertel 3633: sub start_tbody {
                   3634:     my ($target,$token) = @_;
                   3635:     my $currentstring = '';
                   3636:     if ($target eq 'web') {
                   3637: 	$currentstring = $token->[4];     
                   3638:     } 
                   3639:     return $currentstring;
                   3640: }
                   3641: 
                   3642: sub end_tbody {
                   3643:     my ($target,$token) = @_;
                   3644:     my $currentstring = '';
                   3645:     if ($target eq 'web') {
                   3646: 	$currentstring = $token->[2];    
                   3647:     } 
                   3648:     return $currentstring;
                   3649: }
                   3650: 
1.166     sakharuk 3651: #-- <tfoot> tag (end tag optional)
1.122     albertel 3652: sub start_tfoot {
                   3653:     my ($target,$token) = @_;
                   3654:     my $currentstring = '';
                   3655:     if ($target eq 'web') {
                   3656: 	$currentstring = $token->[4];     
                   3657:     } 
                   3658:     return $currentstring;
                   3659: }
                   3660: 
                   3661: sub end_tfoot {
                   3662:     my ($target,$token) = @_;
                   3663:     my $currentstring = '';
                   3664:     if ($target eq 'web') {
                   3665: 	$currentstring = $token->[2];    
                   3666:     } 
                   3667:     return $currentstring;
                   3668: }
                   3669: 
1.166     sakharuk 3670: #-- <thead> tag (end tag optional)
1.122     albertel 3671: sub start_thead {
                   3672:     my ($target,$token) = @_;
                   3673:     my $currentstring = '';
                   3674:     if ($target eq 'web') {
                   3675: 	$currentstring = $token->[4];     
                   3676:     } 
                   3677:     return $currentstring;
                   3678: }
1.10      www      3679: 
1.122     albertel 3680: sub end_thead {
                   3681:     my ($target,$token) = @_;
                   3682:     my $currentstring = '';
                   3683:     if ($target eq 'web') {
                   3684: 	$currentstring = $token->[2];    
                   3685:     } 
                   3686:     return $currentstring;
                   3687: }
1.119     www      3688: 
1.122     albertel 3689: #-- <var> tag
                   3690: sub start_var {
1.44      sakharuk 3691:     my ($target,$token) = @_;
                   3692:     my $currentstring = '';
                   3693:     if ($target eq 'web') {
1.122     albertel 3694: 	$currentstring = $token->[4];     
1.163     sakharuk 3695:     } elsif ($target eq 'tex') {
                   3696: 	$currentstring = '\textit{'; 
                   3697:     }
1.44      sakharuk 3698:     return $currentstring;
                   3699: }
1.10      www      3700: 
1.122     albertel 3701: sub end_var {
                   3702:     my ($target,$token) = @_;
1.44      sakharuk 3703:     my $currentstring = '';
                   3704:     if ($target eq 'web') {
1.163     sakharuk 3705: 	$currentstring = $token->[2];
                   3706:     } elsif ($target eq 'tex') {
                   3707: 	$currentstring = '}'; 
1.44      sakharuk 3708:     } 
                   3709:     return $currentstring;
                   3710: }
1.119     www      3711: 
1.163     sakharuk 3712: #-- <wbr> tag (end tag forbidden)
1.122     albertel 3713: sub start_wbr {
                   3714:     my ($target,$token) = @_;
                   3715:     my $currentstring = '';
                   3716:     if ($target eq 'web') {
                   3717: 	$currentstring = $token->[4];     
                   3718:     } 
                   3719:     return $currentstring;
1.98      albertel 3720: }
                   3721: 
1.122     albertel 3722: sub end_wbr {
                   3723:     my ($target,$token) = @_;
                   3724:     my $currentstring = '';
                   3725:     if ($target eq 'web') {
                   3726: 	$currentstring = $token->[2];    
                   3727:     } 
                   3728:     return $currentstring;
1.98      albertel 3729: }
1.134     sakharuk 3730: 
                   3731: #-- <hideweboutput> tag
                   3732: sub start_hideweboutput {
                   3733:     my ($target,$token) = @_;
                   3734:     if ($target eq 'web') {
                   3735: 	&Apache::lonxml::startredirection();     
                   3736:     } 
                   3737:     return '';
                   3738: }
                   3739: 
                   3740: sub end_hideweboutput {
                   3741:     my ($target,$token) = @_;
                   3742:     my $currentstring = '';
                   3743:     if ($target eq 'web') {
                   3744: 	$currentstring = &Apache::lonxml::endredirection();    
                   3745:     } 
                   3746:     return '';
                   3747: }
                   3748: 
1.94      sakharuk 3749: 
                   3750: sub image_replication {
                   3751:     my $src = shift;
1.187     albertel 3752:     if (not -e $src) { &Apache::lonnet::repcopy($src); }
                   3753:     #replicates eps or ps 
                   3754:     my $epssrc = my $pssrc = $src;
                   3755:     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
                   3756:     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
                   3757:     if (not -e $epssrc && not -e $pssrc) {
                   3758: 	my $result=&Apache::lonnet::repcopy($epssrc);
1.264     raeburn  3759: 	if ($result ne 'ok') { &Apache::lonnet::repcopy($pssrc); }
1.94      sakharuk 3760:     }
                   3761:     return '';
1.195     sakharuk 3762: }
                   3763: 
1.197     sakharuk 3764: sub image_size {
1.256     albertel 3765:     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
1.261     foxr     3766: 
1.197     sakharuk 3767:     #size of image from gif/jpg/jpeg/png 
1.261     foxr     3768:     my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
                   3769:     if (-e $ressrc) {
                   3770: 	$src = $ressrc;
                   3771:     }
1.197     sakharuk 3772:     my $image = Image::Magick->new;
                   3773:     my $current_figure = $image->Read($src);
                   3774:     my $width_param = $image->Get('width') * $scaling;;
1.261     foxr     3775:     my $height_param = $image->Get('height') * $scaling;
                   3776:     &Apache::lonxml::debug("Image magick says: $src :  Height = $height_param width = $width_param");
1.256     albertel 3777:     undef($image);
1.197     sakharuk 3778:     #do we have any specified LaTeX size of the picture?
1.261     foxr     3779:     my $toget='TeXwidth'; 
                   3780:     if ($cis) { 
                   3781: 	$toget=lc($toget); 
                   3782:     }
1.256     albertel 3783:     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
                   3784: 					      $safeeval,$depth,$cis);
                   3785:     $toget='TeXheight'; if ($cis) { $toget=lc($toget); }
                   3786:     my $TeXheight = &Apache::lonxml::get_param($toget,$parstack,
                   3787: 					       $safeeval,$depth,$cis);
1.197     sakharuk 3788:     #do we have any specified web size of the picture?
                   3789:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
1.256     albertel 3790: 					   $depth,1);
                   3791:     if ($TeXwidth) { 
1.252     matthew  3792: 	my $old_width_param=$width_param;
1.197     sakharuk 3793: 	if ($TeXwidth=~/(\d+)\s*\%/) {
                   3794: 	    $width_param = $1*$ENV{'form.textwidth'}/100;
                   3795: 	} else { 
                   3796: 	    $width_param = $TeXwidth;
                   3797: 	}
1.252     matthew  3798: 	$height_param=$TeXwidth/$old_width_param*$height_param;
1.256     albertel 3799:     } elsif ($TeXheight) {
1.248     foxr     3800: 	$height_param = $TeXheight;
                   3801: 	$width_param  = $TeXheight/$height_param*$width_param;
1.256     albertel 3802:     } elsif ($width) {
1.252     matthew  3803: 	my $old_width_param=$width_param;
1.256     albertel 3804: 	$width_param = $width*$scaling;
1.252     matthew  3805:         $height_param=$width_param/$old_width_param*$height_param;
                   3806:     }
                   3807:     if ($width_param > $ENV{'form.textwidth'}) {
                   3808:         my $old_width_param=$width_param;
                   3809: 	$width_param =0.95*$ENV{'form.textwidth'};
                   3810:         $height_param=$width_param/$old_width_param*$height_param;
1.197     sakharuk 3811:     }
1.248     foxr     3812:     return ($height_param, $width_param);
                   3813: }
                   3814: 
                   3815: sub image_width {
                   3816:     my ($height, $width) = &image_size(@_);
                   3817:     return $width;
                   3818: }
                   3819: #  Not yet 100% sure this is correct in all circumstances..
                   3820: #  due to my uncertainty about mods to image_size.
                   3821: #
                   3822: sub image_height {
                   3823:     my ($height, $width) = &image_size(@_);
                   3824:     return $height;
1.197     sakharuk 3825: }
                   3826: 
1.256     albertel 3827: sub get_eps_image {
                   3828:     my ($src)=@_;
1.261     foxr     3829:     my $orig_src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1], $src);
                   3830:     &Apache::lonxml::debug("get_eps_image: Original image: $orig_src<BR />");
1.256     albertel 3831:     $src=~s/\.(gif|png|jpg|jpeg)$/\.eps/i;
                   3832:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
1.261     foxr     3833:     &Apache::lonxml::debug("Filelocation gives; $src <BR />");
1.256     albertel 3834:     if (! -e $src) {
1.261     foxr     3835: 	&Apache::lonxml::debug("$src does not exist");
1.264     raeburn  3836: 	if (&Apache::lonnet::repcopy($src) ne 'ok' ) {
1.261     foxr     3837: 	    &Apache::lonxml::debug("Repcopy of $src failed (1)");
1.256     albertel 3838: 	    #if replication failed try to find ps file
                   3839: 	    $src=~s/\.eps$/\.ps/;
1.261     foxr     3840: 	    &Apache::lonxml::debug("Now looking for $src");
1.256     albertel 3841: 	    #if no ps file try to replicate it
                   3842: 	    if (not -e $src &&
1.264     raeburn  3843: 		&Apache::lonnet::repcopy($src) ne 'ok') {
1.261     foxr     3844: 		&Apache::lonxml::debug("Failed to find or replicate $src");
                   3845: 
1.256     albertel 3846: 		#if replication failed try to produce eps file dynamically
                   3847: 		$src=~s/\.ps$/\.eps/;
                   3848: 		my $temp_file;
                   3849: 		open(FILE,">>/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat");
                   3850: 		my $newsrc=$orig_src;
                   3851: 		$newsrc =~ s|(.*)/res/|/home/httpd/html/res/|;
1.261     foxr     3852: 		&Apache::lonxml::debug("queueing $newsrc for dynamic eps production. <BR/>");
1.256     albertel 3853: 		print FILE "$newsrc\n";
1.261     foxr     3854: 		close FILE;
1.256     albertel 3855: 		$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
                   3856: 		$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
                   3857: 	    }
                   3858: 	}
                   3859:     }
                   3860:     my ($path,$file)=($src=~m|(.*)/([^/]*)$|);
1.261     foxr     3861:     &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />");
1.256     albertel 3862:     return ($path.'/',$file);
                   3863: }
                   3864: 
1.195     sakharuk 3865: sub eps_generation {
                   3866:     my ($src,$file,$width_param) = @_;	     
                   3867:     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
                   3868:     my $temp_file = Apache::File->new('>>'.$filename); 
                   3869:     print $temp_file "$src\n";
                   3870:     my $newsrc = $src;
                   3871:     $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
                   3872:     $newsrc=~s/\/home\/httpd\/html\/res//;
                   3873:     $newsrc=~s/\/home\/([^\/]*)\/public_html\//\/$1\//;
                   3874:     $newsrc=~s/\/\.\//\//;
                   3875:     $newsrc=~s/\/([^\/]+)\.(ps|eps)/\//;
1.213     sakharuk 3876:     if ($newsrc=~/\/home\/httpd\/lonUsers\//) {
                   3877: 	$newsrc=~s/\/home\/httpd\/lonUsers//;
                   3878: 	$newsrc=~s/\/([^\/]+)\/(\w)\/(\w)\/(\w)\//\/$1\//;
                   3879:     }
1.239     sakharuk 3880:     if ($newsrc=~/\/userfiles\//) {
                   3881: 	return ' \graphicspath{{'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
                   3882:     } else {
                   3883: 	return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
                   3884:     }
1.197     sakharuk 3885: }
                   3886: 
                   3887: sub file_path {     
                   3888:     my $src=shift;
                   3889:     my ($file,$path); 
                   3890:     if ($src =~ m!(.*)/([^/]*)$!) {
                   3891: 	$file = $2; 
                   3892: 	$path = $1.'/'; 
                   3893:     } 
                   3894:     return $file,$path;
1.126     sakharuk 3895: }
                   3896: 
                   3897: sub recalc {
                   3898:     my $argument = shift;
                   3899:     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
1.132     sakharuk 3900:     $argument=~/\s*(\d+\.?\d*)\s*(mm|cm|in|pc|pt)/;
1.126     sakharuk 3901:     my $value=$1;
                   3902:     my $units=$2;
                   3903:     if ($units eq 'cm') {
                   3904: 	$value*=10;
                   3905:     } elsif ($units eq 'in') {
                   3906: 	$value*=25.4;
                   3907:     } elsif ($units eq 'pc') {
                   3908: 	$value*=(25.4*12/72.27);
                   3909:     } elsif ($units eq 'pt') {
                   3910: 	$value*=(25.4/72.27);
                   3911:     }
                   3912:     return $value.' mm';
1.94      sakharuk 3913: }
1.184     sakharuk 3914: 
                   3915: sub LATEX_length {
                   3916:     my $garbage=shift;
1.206     sakharuk 3917:     $garbage=~s/^\s+$//;
                   3918:     $garbage=~s/^\s+(\S.*)/$1/;#space before 
                   3919:     $garbage=~s/(.*\S)\s+$/$1/;#space after 
                   3920:     $garbage=~s/(\s)+/$1/;#only one space
                   3921:     $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
                   3922:     $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
                   3923:     $garbage=~s/([^\\])\$/$1/g;#$
                   3924:     $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
                   3925:    $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 3926:     $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;
                   3927:     $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;
                   3928:     $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 3929:     $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;
                   3930:     $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;
                   3931:     $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
                   3932:     #remove some other LaTeX command
                   3933:     $garbage=~s|\\(\w+)\\|\\|g;	 
                   3934:     $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
                   3935:     $garbage=~s|\+|11|g;
1.184     sakharuk 3936:     my  $value=length($garbage);
                   3937:     return $value;
                   3938: }
                   3939: 
                   3940: 
                   3941: 
1.94      sakharuk 3942: 
1.1       sakharuk 3943: 1;
                   3944: __END__

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