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

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

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