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

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

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