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

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

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