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

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

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