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

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

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