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

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

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