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

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

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