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

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

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