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

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

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