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

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

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