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

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

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