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

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

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