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

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

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