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

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

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