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

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

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