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

1.1       sakharuk    1: # The LearningOnline Network with CAPA
                      2: # Tags Default Definition Module 
                      3: #
1.383   ! foxr        4: # $Id: londefdef.pm,v 1.382 2008/03/06 20:31:52 www 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.206     sakharuk 2104: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
                   2105: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
                   2106: 		if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]<$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn]) {
                   2107: 		    $Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]=$Apache::londefdef::table[-1]{'TeXlen'}[$in][$jn];
                   2108: 		}	
                   2109: 	    }
                   2110: 	}
                   2111:         #free space and number of empty columns
                   2112: 	my ($available_space,$empty_columns)=($Apache::londefdef::table[-1]{'width'},0);
1.228     sakharuk 2113: 	if ($#Apache::londefdef::table ne 0) {$available_space=0.9*$available_space;} 
1.206     sakharuk 2114: 	for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
                   2115: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]==0) {
                   2116: 		$empty_columns++;
                   2117: 	    } else {
                   2118: 		$available_space=$available_space-$Apache::londefdef::table[-1]{'TeXlen'}[0][$jn];
                   2119: 	    }
                   2120: 	}
1.358     foxr     2121: 
1.206     sakharuk 2122:         #boundaries for contents columns
                   2123: 	my @min_len=();#columns can not be narrower 
                   2124: 	my @max_len=();#maximum length of column
1.357     foxr     2125: 	my $avg_max;
                   2126: 	my $avg_min;
                   2127: 	my $counter_cols = $Apache::londefdef::table[-1]{'counter_columns'};
                   2128: 	for (my $jn=0;$jn<=$counter_cols; $jn++) {
1.206     sakharuk 2129: 		my ($localmin,$localmax)=(0,0);
                   2130: 		for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
                   2131: 		    if ($localmin<$Apache::londefdef::table[-1]{'minlen'}[$in][$jn]) {
                   2132: 			$localmin=$Apache::londefdef::table[-1]{'minlen'}[$in][$jn];
                   2133: 		    }
                   2134: 		    if ($localmax<$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn]) {
                   2135: 			$localmax=$Apache::londefdef::table[-1]{'maxlen'}[$in][$jn];
                   2136: 		    }
                   2137: 		}
                   2138: 		push @min_len, $localmin;
                   2139: 		push @max_len, $localmax;
1.357     foxr     2140: 		$avg_max = $localmax + $avg_max;
                   2141: 		$avg_min = $localmin + $avg_min;
                   2142: 	}
                   2143: 	# Does not really matter what the average max/min are if there are no cols.
                   2144: 	# and this prevents div 0 in that case.
                   2145: 
                   2146: 	if ($counter_cols != 0) {
                   2147: 	    $avg_max = $avg_max/$counter_cols;
                   2148: 	    $avg_min = $avg_min/$counter_cols;
                   2149: 	}
                   2150: 
                   2151: 
                   2152: 	#  I don't think the below is needed.. but just in case:
                   2153: 
                   2154: 	if ($avg_min > $avg_max) {
                   2155: 	    my $temp = $avg_min;
                   2156: 	    $avg_min = $avg_max;
                   2157: 	    $avg_max = $temp;
1.122     albertel 2158: 	}
1.357     foxr     2159: 
                   2160: 
                   2161: 	for (my $jn=0;$jn<=$counter_cols;$jn++) {
1.206     sakharuk 2162: 	    my $localmin=0,;
                   2163: 	    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
                   2164: 		if ($localmin<$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn]) {
                   2165: 		    $localmin=$Apache::londefdef::table[-1]{'objectlen'}[$in][$jn];
                   2166: 		}
                   2167: 	    }
1.229     sakharuk 2168: 	    if ($max_len[$jn]<$localmin) {
                   2169: 		$max_len[$jn]=$localmin;
                   2170: 	    	$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
                   2171: 	    }#object size is bigger
                   2172: 	    if ($min_len[$jn]<$localmin) {
                   2173: 		$min_len[$jn]=$localmin;
                   2174: 		$Apache::londefdef::table[-1]{'objectsignal'}[$jn]=1;
                   2175: 	    }#object size is bigger
1.206     sakharuk 2176: 	    if ($Apache::londefdef::table[-1]{'TeXlen'}[0][$jn]!=0) {
                   2177: 		$min_len[$jn]=0;
                   2178: 		$max_len[$jn]=0;
                   2179: 	    }
1.357     foxr     2180: 	    #  Spans seem to be really bothered by max/min = 0.  So if we have one
                   2181: 	    #  make it an average joe max/min.
                   2182: 	    
                   2183: 	    if ($max_len[$jn] == 0) {
                   2184: 		$max_len[$jn] = $avg_max;
                   2185: 	    }
                   2186: 	    if ($min_len[$jn] == 0) {
                   2187: 		$min_len[$jn] = $avg_min;
                   2188: 	    }
                   2189: 
1.206     sakharuk 2190: 	}
                   2191:        #final adjustment of column width
                   2192: 	my @fwidth=@{$Apache::londefdef::table[-1]{'TeXlen'}[0]};#final width array
                   2193: 	my @adjust=();
                   2194:         #step 1. adjustment by maximum value
1.370     albertel 2195: 	my $space_needed=0;
1.206     sakharuk 2196: 	for (my $jn=0;$jn<=$#max_len;$jn++) {
1.370     albertel 2197: 	    $space_needed=$space_needed+$max_len[$jn];
1.206     sakharuk 2198: 	}
1.370     albertel 2199: 	if ($space_needed<=$available_space) {
1.362     foxr     2200: 
1.206     sakharuk 2201: 	    for (my $jn=0;$jn<=$#max_len;$jn++) {
                   2202: 		if ($fwidth[$jn]==0) {
                   2203: 		    $fwidth[$jn]=$max_len[$jn];
1.53      sakharuk 2204: 		}
1.51      sakharuk 2205: 	    }
1.206     sakharuk 2206: 	} else {
                   2207:         #step 2. adjustment by minimum value (estimation)
1.370     albertel 2208: 	    $space_needed=0;
1.206     sakharuk 2209: 	    for (my $jn=0;$jn<=$#min_len;$jn++) {
1.370     albertel 2210: 		$space_needed+=$min_len[$jn];
1.206     sakharuk 2211: 	    }
1.370     albertel 2212: 	    if ($space_needed>$available_space) {
1.206     sakharuk 2213: 		$WARNING=' \textbf{NOT ENOUGH SPACE FOR TABLE} ';
                   2214: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
                   2215: 		    if ($fwidth[$jn]==0) {
                   2216: 			$fwidth[$jn]=$min_len[$jn];
                   2217: 		    }
                   2218: 		}
1.229     sakharuk 2219: 		#check if we have objects which can be scaled
                   2220: 		my $how_many_to_scale=0;
                   2221: 		my @to_scale=();
                   2222: 		for (my $jn=0;$jn<=$#max_len;$jn++) {
                   2223: 		    if ($Apache::londefdef::table[-1]{'objectsignal'}[$jn] eq '1') {
                   2224: 			$how_many_to_scale++;
                   2225: 			push @to_scale, $jn;
                   2226: 		    }
                   2227: 		}
                   2228: 		if ($how_many_to_scale>0) {
1.370     albertel 2229: 		    my $space_to_adjust=($space_needed-$available_space)/$how_many_to_scale;
1.229     sakharuk 2230: 		    foreach my $jn (@to_scale) {
                   2231: 			for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
                   2232: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/;
                   2233: 			    if ($1 ne '') {
                   2234: 				my $current_length=&recalc($1);
                   2235: 				$current_length=~/(\d+\.?\d*)/;
                   2236: 				$current_length=$current_length-$space_to_adjust;
                   2237: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/width=$current_length mm/;
                   2238: 			    }
                   2239: 			    $Apache::londefdef::table[-1]{'content'}[$in][$jn]=~m/\[(\d+\.?\d*)\s*mm\]/;
                   2240: 			    if ($1 ne '') {
                   2241: 				my $current_length=$1;
                   2242: 				$current_length=$current_length-$space_to_adjust;
                   2243: 				$Apache::londefdef::table[-1]{'content'}[$in][$jn]=~s/\[(\d+\.?\d*)\s*mm\]/\[$current_length mm\]/;
                   2244: 			    }				
                   2245: 			}
                   2246: 			$fwidth[$jn]=$fwidth[$jn]-$space_to_adjust;
                   2247: 		    }
                   2248: 		}
1.206     sakharuk 2249: 	    } else {
                   2250: 	      #step 3. adjustment over minimal + corrections
1.370     albertel 2251: 		my $enlarge_coef=$available_space/$space_needed;
1.206     sakharuk 2252: 		my $acsessive=0;
                   2253: 		for (my $jn=0;$jn<=$#min_len;$jn++) {
                   2254: 		    $adjust[$jn]=$min_len[$jn]*$enlarge_coef;
                   2255: 		    if ($adjust[$jn]>$max_len[$jn]) {
                   2256: 			$fwidth[$jn]=$max_len[$jn];
                   2257: 			$acsessive=$acsessive+$adjust[$jn]-$max_len[$jn];
                   2258: 			$adjust[$jn]=0;
1.357     foxr     2259: 
1.206     sakharuk 2260: 		    }
                   2261: 		}
                   2262: 		if ($acsessive>0) {
                   2263: 		#we have an excess of space and can redistribute it
                   2264: 		    my $notempty_columns=0;
                   2265: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
                   2266: 			if ($adjust[$jn]!=0) {
                   2267: 			    $notempty_columns++;
                   2268: 			}
                   2269: 		    }
                   2270: 		    my $per_column=$acsessive/$notempty_columns;
                   2271: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
                   2272: 			if ($adjust[$jn]!=0) {
                   2273: 			    $adjust[$jn]+=$per_column;
                   2274: 			    $fwidth[$jn]=$adjust[$jn];
                   2275: 			}
1.223     sakharuk 2276: 		    }
                   2277: 		} else {
                   2278: 		    for (my $jn=0;$jn<=$#min_len;$jn++) {
                   2279: 			$fwidth[$jn]=$adjust[$jn];
1.206     sakharuk 2280: 		    }
                   2281: 		}
1.203     sakharuk 2282: 	    }
                   2283: 	}
1.364     foxr     2284:         # use all available width or specified width as if not specified,
                   2285: 	# the specified width gets defaulted to the available width.
                   2286: 
                   2287: 	my $current=0; 
                   2288: 	for (my $i=0;$i<=$#fwidth;$i++) {  
                   2289: 	    $current+=$fwidth[$i];
                   2290: 	}
1.371     albertel 2291: 	if ($current == 0) {
                   2292:             $current = $Apache::londefdef::table[-1]{'width'};
                   2293:         }
1.364     foxr     2294: 	my $coef=$Apache::londefdef::table[-1]{'width'}/$current;
                   2295: 	for (my $i=0;$i<=$#fwidth;$i++) {  
                   2296: 	    $fwidth[$i]*=$coef;
1.206     sakharuk 2297: 	}
                   2298:         #removing of empty columns if allowed
1.208     sakharuk 2299:         my $permission=&Apache::lonxml::get_param('TeXDropEmptyColumns',$parstack,$safeeval,undef,0);
1.206     sakharuk 2300: 	if ($permission eq 'yes') {
                   2301: 	    my @cleaned_table=();
                   2302:             my @cleaned_header=();
                   2303: 	    my $colind=0;
                   2304: 	    for (my $jn=0;$jn<=$Apache::londefdef::table[-1]{'counter_columns'};$jn++) {
                   2305: 		if ($fwidth[$jn]!=0) {
                   2306: 		    #we need to copy column
                   2307: 		    for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
                   2308: 			$cleaned_table[$in][$colind]=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
                   2309: 			$cleaned_header[$colind]=$fwidth[$jn];
                   2310: 		    }
                   2311: 		    $colind++;
                   2312: 		}
1.122     albertel 2313: 	    }
1.206     sakharuk 2314: 	    $Apache::londefdef::table[-1]{'content'}=\@cleaned_table;
                   2315: 	    @fwidth=@cleaned_header;
1.122     albertel 2316: 	}
1.359     foxr     2317: 
1.358     foxr     2318: 
1.206     sakharuk 2319: 	#construct header of the table
                   2320: 	my $header_of_table = '{'.$Apache::londefdef::table[-1]{'vvinc'};
                   2321: 	for (my $in=0;$in<=$#fwidth;$in++) {
                   2322: 	    $header_of_table.='p{'.$fwidth[$in].' mm}'.$Apache::londefdef::table[-1]{'vvinc'};
                   2323: 	}
                   2324: 	$header_of_table .= '}';
1.357     foxr     2325: 
1.126     sakharuk 2326: 	#fill the table
                   2327: 	for (my $in=0;$in<=$Apache::londefdef::table[-1]{'row_number'};$in++) {
1.306     foxr     2328: 	    my $have_rowspan = 0;
1.206     sakharuk 2329: 	    for (my $jn=0;$jn<=$#fwidth;$jn++) {
1.320     foxr     2330: 
                   2331: 		#-----------------------------------------------------------
                   2332:                 #   I think this order of doing things will ensure that
                   2333: 		#   single rowspan, columspan and combined row/colspans will
                   2334:                 #   work correctly.  LaTeX is delicate here.
                   2335: 		#    RF.
1.356     foxr     2336: 		
1.320     foxr     2337: 		# Start a rowspan if necessary:
1.356     foxr     2338: 		
                   2339: 		my $primary_col_width = $fwidth[$jn]; # Width of primary column.
1.320     foxr     2340: 		my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
                   2341: 		my $colspan = $Apache::londefdef::table[-1]{'colspan'}[$in][$jn];
1.304     foxr     2342: 		#
                   2343: 		#  Do the appropriate magic if this has a colspan
                   2344: 		# 
1.356     foxr     2345: 		
1.372     foxr     2346: 		my $border_char = "";
                   2347: 		if ($border) {
                   2348: 		    $border_char = "|";
                   2349: 		}
1.356     foxr     2350: 		my $spanwidth = 0;
1.304     foxr     2351: 		if ($colspan > 1) {
1.336     foxr     2352: 		    for (my $spancol = $jn; $spancol < $jn + $colspan; $spancol++) {
                   2353: 			$spanwidth += $fwidth[$spancol];
                   2354: 		    }
1.304     foxr     2355: 		    $output .= '\multicolumn{'.
                   2356: 			$colspan
1.337     foxr     2357: 			."}";
                   2358: 		    if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
1.372     foxr     2359: 			$output .= '{'.$border_char.'c'.$border_char.'}{';
1.337     foxr     2360: 		    } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
1.372     foxr     2361: 			$output .= '{'.$border_char.'r'.$border_char.'}{';
1.337     foxr     2362: 		    }
                   2363: 		    else {
1.372     foxr     2364: 			$output .= '{'.$border_char."p{$spanwidth mm}".$border_char.'}{';
1.337     foxr     2365: 		    }
1.356     foxr     2366: 		    
                   2367: 		} else {
                   2368: 		    $spanwidth = $primary_col_width; # If no span width will be just colwidth
1.304     foxr     2369: 		}
1.306     foxr     2370: 
1.337     foxr     2371: 		# Rowspan... if colspan is 1, and there's an alignment we'll need
                   2372: 		# to kick in a multicolumn in order to get the alignment spec.
                   2373: 		# this must precede the multirow or LaTex gets quite upset.
                   2374: 		# Naturally if colspan > 1 we've already done that above ^
                   2375: 		#
                   2376: 		my $multirow_aligned = 0;
1.306     foxr     2377: 		if ($rowspan > 1) {
1.337     foxr     2378: 		    if ($colspan == 1) {
                   2379: 			if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
1.372     foxr     2380: 			    $output .= '\multicolumn{1}{'.$border_char.'c'.$border_char.'}{';
1.337     foxr     2381: 			    $multirow_aligned = 1;
                   2382: 			} elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
1.372     foxr     2383: 			    $output .= '\multicolumn{1}{'.$border_char.'r'.$border_char.'}{';
1.337     foxr     2384: 			    $multirow_aligned = 1;
                   2385: 			}
                   2386: 		    }
1.306     foxr     2387: 		    $have_rowspan++;
1.356     foxr     2388: 		    if ($multirow_aligned) {
                   2389: 			$output .= '\multirow{'.$rowspan.'}[0]{*}{';
                   2390: 		    } else {
                   2391: 			$output .= '\multirow{'.$rowspan."}[0]{$spanwidth mm}{";
                   2392: 		    }
1.350     albertel 2393: 		    
                   2394: 		    $Apache::londefdef::table[-1]{'content'}[$in][$jn] =~
                   2395: 			s{^\s*\\par\s*}{};
                   2396: 		    $Apache::londefdef::table[-1]{'content'}[$in][$jn] =~
                   2397: 			s{\s*\\vskip\s*0pt\s*$}{};
1.356     foxr     2398: 			  
1.337     foxr     2399: 		    #
                   2400: 		    # If we did not throw in a multicolumn to align, then add 
                   2401: 		    # an extra {
                   2402: 		    # so we close correctly without having to keep additional state
                   2403: 		    # around
                   2404: 		    #
                   2405: 		    if (!$multirow_aligned) {
                   2406: 			$output .= '{';
                   2407: 		    }
1.306     foxr     2408: 		}
                   2409: 		if (($rowspan eq '^') || ($rowspan eq '_')) {
                   2410: 		    $have_rowspan++;
                   2411: 		}
1.356     foxr     2412: 		    #--------------------------------------------------------------
1.306     foxr     2413: 
1.337     foxr     2414: 
                   2415: 		# For right and center alignment of single cells.
                   2416: 		# we are going to use a multicolumn with a span of 1 to specify alignment.
                   2417: 		#
                   2418: 		if ($colspan == 1  && $rowspan == 1) {
                   2419: 		    if ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') {
1.372     foxr     2420: 			$output .= '\multicolumn{1}{'.$border_char.'c'.$border_char.'}{';
1.337     foxr     2421: 		    } elsif ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r') {
1.372     foxr     2422: 			$output .= '\multicolumn{1}{'.$border_char.'r'.$border_char.'}{';
1.337     foxr     2423: 		    }
1.206     sakharuk 2424: 		}
1.337     foxr     2425: 
1.206     sakharuk 2426: 		$output.=$Apache::londefdef::table[-1]{'content'}[$in][$jn];
1.337     foxr     2427: 
                   2428: 		if (($colspan == 1 && $rowspan == 1)   &&
                   2429: 		    (($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'c') ||
                   2430: 		     ($Apache::londefdef::table[-1]{'align'}[$in][$jn] eq 'r'))) {
                   2431: 		    $output .= '}';
1.206     sakharuk 2432: 		}
1.337     foxr     2433: 
1.306     foxr     2434: 		# Close off any open multirow:
                   2435: 		
                   2436: 		if ($rowspan > 1) {
1.337     foxr     2437: 		    $output .= '}}';
1.306     foxr     2438: 		}
1.304     foxr     2439: 		#  Close off the colspan...
                   2440: 		#
                   2441: 		if ($colspan > 1)  {
                   2442: 		    $output .= '}';
                   2443: 		    $jn += $colspan-1; # Adjust for number of rows really left.
                   2444: 		}
1.206     sakharuk 2445:                 if ($jn!=$#fwidth) {$output.=' '.$Apache::londefdef::table[-1]{'vinc'};}
                   2446: 	    }
1.306     foxr     2447: 	    #  If have_rowspan > 0, and borders are on, then 
                   2448: 	    #  we need to do more than put an \hline at the bottom of row.
                   2449: 	    #  we need to do the appropriate \cline to ensure that
                   2450: 	    #  the spanned rows don't have \hlines through them.
                   2451: 
                   2452: 	    if (($Apache::londefdef::table[-1]{'hinc'} =~ /\\hline/) && $have_rowspan) {
                   2453: 		$output .= ' \\\\ ';
                   2454: 		for (my $jn=0; $jn<=$#fwidth;$jn++) {
                   2455: 		    my $rowspan = $Apache::londefdef::table[-1]{'rowspan'}[$in][$jn];
1.320     foxr     2456: 		    if ($rowspan ne "^") {
                   2457: 			if (($rowspan <= 1) || ($rowspan eq '_')) {
                   2458: 			    my $column = $jn+1;
                   2459: 			    $output .= '\cline{'.$column.'-'.$column.'} ';
                   2460: 			}
1.306     foxr     2461: 		    }
                   2462: 		}
                   2463: 
                   2464: 	    } else {
                   2465: 		$output.=' \\\\ '.$Apache::londefdef::table[-1]{'hinc'}.' ';
                   2466: 	    }
1.122     albertel 2467: 	}
1.281     foxr     2468: 	# Note that \newline destroys alignment env's produced  by e.g. <div>
                   2469: 	# $Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut\newline\strut ';
                   2470: 	$Apache::londefdef::table[-1]{'output'} .= $header_of_table.$Apache::londefdef::table[-1]{'hinc'}.$output.'\end{tabular}\strut'.'\\\\'."\n".'\strut ';
1.127     sakharuk 2471: 	if ($#Apache::londefdef::table > 0) {	    
                   2472: 	    my $inmemory = $Apache::londefdef::table[-1]{'output'};
1.294     foxr     2473: 	    # Figure out max/and min width  by summing us and then
                   2474: 	    # apply that to the current column of the table we nest in
                   2475: 	    # if it's larger than the current width or the current width
                   2476: 	    # is undefined.
                   2477: 	    #
                   2478: 	    my $min_nested_width = 0;
                   2479: 	    my $max_nested_width = 0;
                   2480: 	    for (my $col = 0; $col <= $Apache::londefdef::table[-1]{'counter_columns'}; $col++) {
                   2481: 		$min_nested_width +=  $min_len[$col];
                   2482: 		$max_nested_width +=  $max_len[$col];
1.300     foxr     2483: 		
1.294     foxr     2484: 	    }
                   2485: 	    # Fudge in an extra 5 mm for borders etc:
                   2486: 	    
                   2487: 	    $min_nested_width += 5;
                   2488: 	    $max_nested_width += 5;
                   2489: 
                   2490: 	    my $outer_column = $Apache::londefdef::table[-2]{'counter_columns'};
                   2491: 	    my $outer_row    = $Apache::londefdef::table[-2]{'row_number'};
                   2492: 	    if ($min_nested_width > $Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column]) {
                   2493: 		$Apache::londefdef::table[-2]{'minlen'}[$outer_row][$outer_column] = $min_nested_width;
                   2494: 	    }
                   2495: 	    if ($max_nested_width > $Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column]) {
                   2496: 		$Apache::londefdef::table[-2]{'maxlen'}[$outer_row][$outer_column] = $max_nested_width;
                   2497: 	    }
                   2498: 
1.127     sakharuk 2499: 	    pop @Apache::londefdef::table;
1.129     sakharuk 2500: 	    push @{$Apache::londefdef::table[-1]{'include'}}, $inmemory;
1.127     sakharuk 2501: 	} else {
                   2502: 	    $currentstring .= $Apache::londefdef::table[-1]{'output'};
                   2503: 	    pop @Apache::londefdef::table;
1.143     sakharuk 2504: 	    undef @Apache::londefdef::table;
1.127     sakharuk 2505: 	}
1.122     albertel 2506:     }
                   2507:     return $currentstring;
                   2508: }
                   2509: 
1.166     sakharuk 2510: #-- <tr> tag (end tag optional)
1.122     albertel 2511: sub start_tr {
                   2512:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2513:     my $currentstring = '';
1.325     albertel 2514:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 2515: 	$currentstring = $token->[4];     
                   2516:     } elsif ($target eq 'tex') {
                   2517: 	$Apache::londefdef::table[-1]{'row_number'}++;
1.206     sakharuk 2518: 	my $alignchar=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
1.122     albertel 2519: 	if ($alignchar ne '') {
1.206     sakharuk 2520: 	    push @ {$Apache::londefdef::table[-1]{'rows'} },substr($alignchar,0,1);
1.122     albertel 2521: 	} else {
                   2522: 	    push @ {$Apache::londefdef::table[-1]{'rows'} }, 'l';
                   2523: 	}
                   2524: 	push ( @{ $Apache::londefdef::table[-1]{'rowdata'} }, $Apache::londefdef::table[-1]{'hinc'});
1.300     foxr     2525: 	#
                   2526: 	#  Need to save the number of table columns to preserve the max # columns.
                   2527: 	#
                   2528: 	$Apache::londefdef::table[-1]{'prior_columns'}   = $Apache::londefdef::table[-1]{'counter_columns'};
1.122     albertel 2529: 	$Apache::londefdef::table[-1]{'counter_columns'} = -1;
1.206     sakharuk 2530: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}}, [];
                   2531: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}}, [];
                   2532: 	push @ {$Apache::londefdef::table[-1]{'minlen'}}, [];
                   2533: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}}, [];
                   2534: 	push @ {$Apache::londefdef::table[-1]{'content'}}, [];
1.122     albertel 2535:     } 
                   2536:     return $currentstring;
                   2537: }
                   2538:         
                   2539: sub end_tr {
1.160     sakharuk 2540:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
1.279     foxr     2541:     my $currentstring = &end_p();	# Close any pending <p> in the row.
1.325     albertel 2542:     if ($target eq 'web' || $target eq 'webgrade') {
1.277     foxr     2543: 	$currentstring .= $token->[2];     
1.122     albertel 2544:     } elsif ($target eq 'tex') {
1.160     sakharuk 2545: 	if ($Apache::londefdef::TD_redirection) {
                   2546: 	    &end_td_tex($parstack,$parser,$safeeval);    
                   2547: 	}
1.300     foxr     2548: 	# Counter columns must be the maximum number of columns seen
                   2549: 	# in the table so far so:
                   2550: 	if ($Apache::londefdef::table[-1]{'prior_columns'} > $Apache::londefdef::table[-1]{'counter_columns'}) {
                   2551: 	    $Apache::londefdef::table[-1]{'counter_columns'} = $Apache::londefdef::table[-1]{'prior_columns'};
                   2552: 	}
1.295     foxr     2553: 
                   2554: 
1.294     foxr     2555: 	
1.122     albertel 2556:     }
                   2557:     return $currentstring;
                   2558: }
                   2559: 
1.166     sakharuk 2560: #-- <td> tag (end tag optional)
1.122     albertel 2561: sub start_td {
                   2562:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2563:     my $currentstring = '';
1.325     albertel 2564:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 2565: 	$currentstring = $token->[4];     
                   2566:     } elsif ($target eq 'tex') {
1.160     sakharuk 2567: 	$Apache::londefdef::TD_redirection = 1;
1.159     sakharuk 2568: 	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
1.122     albertel 2569:     } 
                   2570:     return $currentstring;
                   2571: }   
1.159     sakharuk 2572:     
                   2573: sub tag_check {
                   2574:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
1.160     sakharuk 2575:     my @ar=@$parstack; 
                   2576:     for (my $i=$#ar-1;$i>=0;$i--) {
                   2577: 	if (lc($$tagstack[$i]) eq $good_tag) {
                   2578: 	    &start_td_tex($parstack,$parser,$safeeval);
                   2579: 	    last;
                   2580: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
                   2581: 	    splice @ar, $i+1;
                   2582: 	    &end_td_tex(\@ar,$parser,$safeeval);
                   2583: 	    &start_td_tex($parstack,$parser,$safeeval);
                   2584: 	    last;
1.159     sakharuk 2585: 	}
1.160     sakharuk 2586:     }
1.159     sakharuk 2587:     return '';
                   2588: }
                   2589:  
                   2590: sub start_td_tex {
                   2591:     my ($parstack,$parser,$safeeval) = @_;
1.206     sakharuk 2592:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
                   2593:     if ($alignchar eq '') {
                   2594: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
1.159     sakharuk 2595:     }
1.206     sakharuk 2596:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
1.159     sakharuk 2597:     $Apache::londefdef::table[-1]{'counter_columns'}++;
1.206     sakharuk 2598:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
                   2599:     if (defined $TeXwidth) {		
                   2600: 	my $current_length=&recalc($TeXwidth);
                   2601: 	$current_length=~/(\d+\.?\d*)/;
                   2602: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
                   2603:     }
1.159     sakharuk 2604:     &Apache::lonxml::startredirection();
                   2605:     return '';
                   2606: }
                   2607: 
                   2608: sub end_td_tex {
                   2609:     my ($parstack,$parser,$safeeval) = @_;
1.304     foxr     2610:     my $current_row    = $Apache::londefdef::table[-1]{'row_number'};
                   2611:     my $current_column = $Apache::londefdef::table[-1]{'counter_columns'}; 
1.303     foxr     2612:     my $data = &Apache::lonxml::endredirection();
                   2613: 
1.305     foxr     2614:     #  The rowspan array of the table indicates which cells are part of a span.
                   2615:     #  n indicates the start of a span set of n rows.
                   2616:     #  ^ indicates a cell that continues a span set.
1.306     foxr     2617:     #  _ indicates the cell is at the bottom of a span set.
1.305     foxr     2618:     #  If this and subsequent cells are part of a rowspan, we must
                   2619:     #  push along the row until we find one that is not.
                   2620: 
                   2621:     while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column]) 
1.306     foxr     2622: 	   && ($Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] =~ /[\^\_]/)) {
1.305     foxr     2623: 	# Part of a span.
                   2624: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1]}, '';
                   2625: 	$current_column++;
                   2626:     }
                   2627:     $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
                   2628:    
                   2629: 
1.320     foxr     2630:     # Get the column and row spans.
                   2631:     # Colspan can be done via \multicolumn if I can figure out the data structs.
                   2632: 
                   2633:     my $colspan = &Apache::lonxml::get_param('colspan', $parstack, $safeeval, undef, 0);
                   2634:     if (!$colspan) {
                   2635: 	$colspan = 1;
                   2636:     }
1.305     foxr     2637: 
                   2638:     my $rowspan = &Apache::lonxml::get_param('rowspan', $parstack, $safeeval, undef, 0);
                   2639:     if (!$rowspan) {
                   2640: 	$rowspan = 1;
                   2641:     }
                   2642: 
1.303     foxr     2643: 
1.305     foxr     2644: 
1.320     foxr     2645:     for (my $c = 0; $c < $colspan; $c++) {
                   2646: 	$Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column+$c] = $rowspan;
                   2647: 	for (my $i = 1; $i < $rowspan; $i++) {
                   2648: 	    $Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '^';
                   2649: 	    if ($i == ($rowspan-1)) {
                   2650: 		$Apache::londefdef::table[-1]{'rowspan'}[$current_row+$i][$current_column+$c] = '_';
                   2651: 	    }
1.306     foxr     2652: 	}
1.305     foxr     2653:     }
1.304     foxr     2654: 
1.159     sakharuk 2655:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
                   2656:     if (defined $TeXwidth) {		
1.357     foxr     2657: 	for (my $c = 0; $c < $colspan; $c++) {
                   2658: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2659: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2660: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2661: 	}
1.159     sakharuk 2662:     } else {
1.206     sakharuk 2663: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
                   2664: 	    my $garbage_data=$data;
                   2665: 	    my $fwidth=0;
                   2666:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
                   2667: 		my $current_length=&recalc($1);
                   2668: 		$current_length=~/(\d+\.?\d*)/;
                   2669: 		if ($fwidth<$1) {$fwidth=$1;}
                   2670: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
                   2671: 	    }
                   2672:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
                   2673: 		my $current_length=$1;
                   2674: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
                   2675: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
                   2676: 	    }
1.357     foxr     2677: 	    for (my $c = 0; $c < $colspan; $c++) {
                   2678: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2679: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
                   2680: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2681: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2682: 	    }
1.231     sakharuk 2683: 	} 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)*/) {
                   2684: 	    my $garbage_data=$data;
                   2685: 	    my $fwidth=0;
                   2686:             while ($garbage_data=~/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)\s*\}/) {
                   2687: 		my $current_length=&recalc($1);
                   2688: 		$current_length=~/(\d+\.?\d*)/;
                   2689: 		if ($fwidth<$1) {$fwidth=$1;}
                   2690: 		$garbage_data=~s/\\parbox\{\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
                   2691: 	    }
                   2692:             while ($garbage_data=~/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
                   2693: 		my $current_length=&recalc($1);
                   2694: 		$current_length=~/(\d+\.?\d*)/;
                   2695: 		if ($fwidth<$1) {$fwidth=$1;}
                   2696: 		$garbage_data=~s/\\epsfxsize\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
                   2697: 	    }
1.357     foxr     2698: 	    for (my $c = 0; $c < $colspan; $c++) {
                   2699: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2700: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
                   2701: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2702: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2703: 	    }
1.231     sakharuk 2704: 	    $data=~s/\\\\\s*$//; 
1.159     sakharuk 2705: 	} else {  
1.166     sakharuk 2706: 	    $data=~s/^\s+(\S.*)/$1/; 
1.159     sakharuk 2707: 	    $data=~s/(.*\S)\s+$/$1/;
1.166     sakharuk 2708: 	    $data=~s/(\s)+/$1/;
1.206     sakharuk 2709: 	    my ($current_length,$min_length)=(0,0);
1.166     sakharuk 2710: 	    if ($data=~/\\vskip/) {
                   2711:                 my $newdata=$data;
                   2712: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
                   2713: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
                   2714: 		foreach my $elementdata (@newdata) {
1.206     sakharuk 2715: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
1.166     sakharuk 2716: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
1.206     sakharuk 2717:                     my @words=split(/ /,$elementdata);
                   2718: 		    foreach my $word (@words) {
                   2719: 			my $lengthword=2.5*&LATEX_length($word);
                   2720: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
                   2721: 		    }
1.166     sakharuk 2722: 		}
                   2723: 	    } else {
1.206     sakharuk 2724: 		$current_length=2.5*&LATEX_length($data);
                   2725:                     my @words=split(/ /,$data);
                   2726: 		    foreach my $word (@words) {
1.228     sakharuk 2727: 			my $lengthword=2*&LATEX_length($word);
1.206     sakharuk 2728: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
                   2729: 		    }
1.166     sakharuk 2730: 	    }
1.357     foxr     2731: 	    for (my $c = 0; $c < $colspan; $c++) {
                   2732: 		push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2733: 		push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2734: 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
                   2735: 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
                   2736: 	    }
1.159     sakharuk 2737: 	}        
                   2738:     }
1.302     foxr     2739:     # Substitute all of the tables nested in this cell in their appropriate places.
                   2740: 
                   2741: 
                   2742:     my $nested_count = $#{$Apache::londefdef::table[-1]{'include'}}; # This one is constant...
                   2743:     for (my $in=0; $in<=$nested_count; $in++) {    
1.301     foxr     2744: 	my $nested = shift @{$Apache::londefdef::table[-1]{'include'}};
                   2745: 	$nested =~ s/\\end\{tabular\}\\strut\\\\/\\end\{tabular\}/;
                   2746: 	# $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
                   2747: 	$data =~ s/\\keephidden\{NEW TABLE ENTRY\}/$nested/;
                   2748: 
                   2749:     }
                   2750:     # Should be be killing off the 'include' elements as they're used up?
1.305     foxr     2751: 
1.206     sakharuk 2752:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
1.305     foxr     2753: 
1.304     foxr     2754: 
                   2755: 
                   2756: 
                   2757:     #  the colspan array will indicate how many columns will be spanned by this
                   2758:     #  cell..this requires that counter_columns also be adjusted accordingly
                   2759:     #  so that the next bunch of text goes in the right cell.  Note that since
                   2760:     #  counter_columns is incremented in the start_td_tex, we adjust by colspan-1.
                   2761:     #
                   2762: 
                   2763:     $Apache::londefdef::table[-1]{'counter_columns'} += $colspan -1;
                   2764:     for (my $i = 0; $i < ($colspan -1); $i++) {
                   2765: 	push @ {$Apache::londefdef::table[-1]{'content'}[-1] },'';
                   2766:     }
1.320     foxr     2767:     for (my $r = 0; $r < $rowspan; $r++) {
                   2768: 	$Apache::londefdef::table[-1]{'colspan'}[$current_row+$r][$current_column] = $colspan;
                   2769: 	# Put empty text in spanned cols.
                   2770: 	
                   2771:     }
                   2772: 
1.304     foxr     2773: 
                   2774: 
1.301     foxr     2775:     return '';
1.159     sakharuk 2776: }
                   2777: 
1.122     albertel 2778: sub end_td {
1.126     sakharuk 2779:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
1.122     albertel 2780:     my $currentstring = '';
1.325     albertel 2781:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 2782: 	$currentstring = $token->[2];     
                   2783:     } elsif ($target eq 'tex') {
1.160     sakharuk 2784:         $Apache::londefdef::TD_redirection =0;
1.159     sakharuk 2785: 	&end_td_tex($parstack,$parser,$safeeval);
1.122     albertel 2786:     }
                   2787:     return $currentstring;
                   2788: }
                   2789: 
1.166     sakharuk 2790: #-- <th> tag (end tag optional)
1.122     albertel 2791: sub start_th {
                   2792:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2793:     my $currentstring = '';
1.325     albertel 2794:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 2795: 	$currentstring = $token->[4];     
                   2796:     } elsif ($target eq 'tex') {
1.230     sakharuk 2797: 	$Apache::londefdef::TD_redirection = 1;
                   2798: 	&tagg_check('tr','th',$tagstack,$parstack,$parser,$safeeval);
1.122     albertel 2799:     } 
                   2800:     return $currentstring;
1.130     sakharuk 2801: }   
1.230     sakharuk 2802:     
                   2803: sub tagg_check {
                   2804:     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
                   2805:     my @ar=@$parstack; 
                   2806:     for (my $i=$#ar-1;$i>=0;$i--) {
                   2807: 	if (lc($$tagstack[$i]) eq $good_tag) {
                   2808: 	    &start_th_tex($parstack,$parser,$safeeval);
                   2809: 	    last;
                   2810: 	} elsif (lc($$tagstack[$i]) eq $bad_tag) {
                   2811: 	    splice @ar, $i+1;
                   2812: 	    &end_th_tex(\@ar,$parser,$safeeval);
                   2813: 	    &start_th_tex($parstack,$parser,$safeeval);
                   2814: 	    last;
                   2815: 	}
                   2816:     }
                   2817:     return '';
                   2818: }
                   2819:  
                   2820: sub start_th_tex {
                   2821:     my ($parstack,$parser,$safeeval) = @_;
                   2822:     my $alignchar = substr(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1),0,1);
                   2823:     if ($alignchar eq '') {
                   2824: 	$alignchar = $Apache::londefdef::table[-1]{'rows'}[-1];
                   2825:     }
                   2826:     push @{ $Apache::londefdef::table[-1]{'align'}[$Apache::londefdef::table[-1]{'row_number'}] }, $alignchar;
                   2827:     $Apache::londefdef::table[-1]{'counter_columns'}++;
                   2828:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
                   2829:     if (defined $TeXwidth) {		
                   2830: 	my $current_length=&recalc($TeXwidth);
                   2831: 	$current_length=~/(\d+\.?\d*)/;
                   2832: 	push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$1;
                   2833:     }
                   2834:     &Apache::lonxml::startredirection();
                   2835:     return '';
                   2836: }
                   2837: 
                   2838: sub end_th_tex {
                   2839:     my ($parstack,$parser,$safeeval) = @_;
                   2840:     my $current_row = $Apache::londefdef::table[-1]{'row_number'};
                   2841:     my $data=&Apache::lonxml::endredirection();
                   2842:     my $TeXwidth=&Apache::lonxml::get_param('TeXwidth',$parstack,$safeeval,undef,0);
                   2843:     if (defined $TeXwidth) {		
                   2844: 	push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2845: 	push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2846: 	push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2847:     } else {
                   2848: 	if (($data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) or ($data=~m/\[(\d+\.?\d*)\s*mm\]/)) {
                   2849: 	    my $garbage_data=$data;
                   2850: 	    my $fwidth=0;
                   2851:             while ($garbage_data=~m/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)/) {
                   2852: 		my $current_length=&recalc($1);
                   2853: 		$current_length=~/(\d+\.?\d*)/;
                   2854: 		if ($fwidth<$1) {$fwidth=$1;}
                   2855: 		$garbage_data=~s/width\s*=\s*(\d+\.?\d*\s*(mm|cm|in|pc|pt)*)//;
                   2856: 	    }
                   2857:             while ($garbage_data=~m/\[(\d+\.?\d*)\s*mm\]/) {
                   2858: 		my $current_length=$1;
                   2859: 		if ($fwidth<$current_length) {$fwidth=$current_length;}
                   2860: 		$garbage_data=~s/\[(\d+\.?\d*)\s*mm\]//;
                   2861: 	    }
                   2862: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2863: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
                   2864: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2865: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2866: 	} else {  
                   2867: 	    $data=~s/^\s+(\S.*)/$1/; 
                   2868: 	    $data=~s/(.*\S)\s+$/$1/;
                   2869: 	    $data=~s/(\s)+/$1/;
                   2870: 	    my ($current_length,$min_length)=(0,0);
                   2871: 	    if ($data=~/\\vskip/) {
                   2872:                 my $newdata=$data;
                   2873: 		$newdata=~s/\\vskip \d*\.?\d*\s*mm/THISISJUSTTEMPORARYSEPARATOR/g;
                   2874: 		my @newdata=split(/THISISJUSTTEMPORARYSEPARATOR/,$newdata);
                   2875: 		foreach my $elementdata (@newdata) {
                   2876: 		    my $lengthnewdata=2.5*&LATEX_length($elementdata);
                   2877: 		    if ($lengthnewdata>$current_length) {$current_length=$lengthnewdata;}
                   2878:                     my @words=split(/ /,$elementdata);
                   2879: 		    foreach my $word (@words) {
                   2880: 			my $lengthword=2.5*&LATEX_length($word);
                   2881: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
                   2882: 		    }
                   2883: 		}
                   2884: 	    } else {
                   2885: 		$current_length=2.5*&LATEX_length($data);
                   2886:                     my @words=split(/ /,$data);
                   2887: 		    foreach my $word (@words) {
                   2888: 			my $lengthword=2*&LATEX_length($word);
                   2889: 			if ($min_length<$lengthword) {$min_length=$lengthword;}
                   2890: 		    }
                   2891: 	    }
                   2892: 	    push @ {$Apache::londefdef::table[-1]{'TeXlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2893: 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
                   2894: 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
                   2895: 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
                   2896: 	}        
                   2897:     }
                   2898: 	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
                   2899: 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
                   2900: 	}
                   2901:     #make data bold
                   2902:     $data='\textbf{'.$data.'}';
                   2903:     push @ {$Apache::londefdef::table[-1]{'content'}[-1] },$data;
                   2904:     return'';
                   2905: }
                   2906: 
1.122     albertel 2907: sub end_th {
1.130     sakharuk 2908:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
1.279     foxr     2909:     my $currentstring = &end_p();	# Close any open <p> in the row.
1.325     albertel 2910:     if ($target eq 'web' || $target eq 'webgrade') {
1.277     foxr     2911: 	$currentstring .= $token->[2];     
1.122     albertel 2912:     } elsif ($target eq 'tex') {
1.230     sakharuk 2913:         $Apache::londefdef::TD_redirection =0;
                   2914: 	&end_th_tex($parstack,$parser,$safeeval);
1.122     albertel 2915:     }
                   2916:     return $currentstring;
                   2917: }
1.230     sakharuk 2918:      
1.181     sakharuk 2919: #-- <img> tag (end tag forbidden)
1.249     foxr     2920: #
                   2921: #  Render the <IMG> tag.
                   2922: #     <IMG> has the following attributes (in addition to the 
                   2923: #     standard HTML ones:
                   2924: #      TeXwrap   - Governs how the tex target will try to wrap text around
                   2925: #                  horizontally aligned images.
                   2926: #      TeXwidth  - The width of the image when rendered for print (mm).
                   2927: #      TeXheight - The height of the image when rendered for print (mm)
                   2928: #         (Note there seems to also be support for this as a % of page size)
                   2929: #      
1.122     albertel 2930: sub start_img {
1.299     albertel 2931:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
1.122     albertel 2932:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
                   2933: 					 undef,1);
1.325     albertel 2934:     if (! $src && 
                   2935: 	($target eq 'web' || $target eq 'webgrade' || $target eq 'tex')
                   2936: 	) { 
1.299     albertel 2937: 	my $inside = &Apache::lonxml::get_all_text("/img",$parser,$style);
1.189     albertel 2938: 	return '';
                   2939:     }
1.290     albertel 2940:     &Apache::lonxml::extlink($src);
1.122     albertel 2941:     my $currentstring = '';
                   2942:     my $scaling = .3;
1.249     foxr     2943: 
                   2944:    # Render unto browsers that which are the browser's...
                   2945: 
1.325     albertel 2946:     if ($target eq 'web' || $target eq 'webgrade') {
1.267     albertel 2947: 	if ($env{'browser.imagesuppress'} ne 'on') {
1.322     albertel 2948: 	    my $enc = ('yes' eq 
                   2949: 		       lc(&Apache::lonxml::get_param('encrypturl',$parstack,
                   2950: 						     $safeeval)));
                   2951: 	    $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
                   2952: 							 $enc);
1.122     albertel 2953: 	} else {
1.322     albertel 2954: 	    my $alttag = &Apache::lonxml::get_param('alt',$parstack,$safeeval,
                   2955: 						    undef,1);
                   2956: 	    if (!$alttag) {
                   2957: 		$alttag = &Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
                   2958: 						   $src);
1.51      sakharuk 2959: 	    }
1.158     sakharuk 2960: 	    $currentstring.='[IMAGE: '.$alttag.']';
1.1       sakharuk 2961: 	}
1.249     foxr     2962: 
                   2963: 	# and render unto TeX that which is LaTeX
                   2964: 
1.122     albertel 2965:     } elsif ($target eq 'tex') {
1.248     foxr     2966: 	#
                   2967: 	#  The alignment will require some superstructure to be put around
                   2968: 	#  the \includegraphics stuff.  At present we can only partially
                   2969: 	#  simulate the alignments offered by html.
                   2970: 	#
                   2971: 	#
                   2972: 	my $align = lc(&Apache::lonxml::get_param('align', 
                   2973: 						  $parstack,
                   2974: 						  $safeeval,
                   2975: 						  undef,1));
                   2976: 	if(!$align) {
1.287     foxr     2977: 		$align = "bottom";	# This is html's default so it's ours too.
1.248     foxr     2978: 	}
                   2979: 	#
                   2980: 	&Apache::lonxml::debug("Alignemnt = $align");
                   2981: 	#  LaTeX's image/text wrapping is really bad since it wants to
                   2982: 	#  make figures float.  
                   2983:         #   The user has the optional parameter (applicable only to l/r
                   2984: 	# alignment to use the picins/parpic directive to get wrapped text
                   2985: 	# this is also imperfect.. that's why we give them a choice...
                   2986: 	# so they can't yell at us for our choice.
                   2987: 	#
                   2988: 	my $latex_rendering = &Apache::lonxml::get_param('TeXwrap',
                   2989: 							    $parstack,
                   2990: 							    $safeeval,
                   2991: 							    undef,0);
1.312     foxr     2992: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering");
1.248     foxr     2993: 	if(!$latex_rendering) {
1.312     foxr     2994: 		$latex_rendering = "texwrap";
                   2995: 	}
                   2996: 	# using texwrap inside a table does not work. So, if after all of this,
                   2997: 	# texwrap is on, we turn it off if we detect we're in a table:
                   2998: 	#
                   2999: 	if (($latex_rendering eq 'texwrap') && &is_inside_of($tagstack, "table")) {
                   3000: 	    $latex_rendering = 'parpic';
1.248     foxr     3001: 	}
1.323     foxr     3002: 
1.312     foxr     3003: 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
1.248     foxr     3004: 
1.256     albertel 3005: 	#if original gif/jpg/png file exist do following:
1.266     albertel 3006: 	my $origsrc=$src;
1.256     albertel 3007: 	my ($path,$file) = &get_eps_image($src);
1.341     foxr     3008: 	# &Apache::lonnet::logthis("Image source: $src result: $path $file");
1.122     albertel 3009: 	$src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
1.261     foxr     3010: 	&Apache::lonxml::debug("path = $path file = $file src = $src");
1.192     sakharuk 3011: 	if (-e $src) {
1.261     foxr     3012: 	    &Apache::lonxml::debug("$src exists");
1.256     albertel 3013: 	    my ($height_param,$width_param)=
1.266     albertel 3014: 		&image_size($origsrc,0.3,$parstack,$safeeval);
                   3015: 	    my $size;
                   3016: 	    if ($width_param)  { $size.='width='.$width_param.' mm,'; }
                   3017: 	    if ($height_param) { $size.='height='.$height_param.' mm]'; }
1.341     foxr     3018: 	    # Default size if not able to extract that (e.g. eps image).
                   3019: 	    
                   3020: 	    # &Apache::lonnet::logthis("Size = $size");
                   3021: 	    
1.266     albertel 3022: 	    $size='['.$size;
                   3023: 	    $size=~s/,$/]/; 
1.344     albertel 3024: 	    $currentstring .= '\graphicspath{{'.$path.'}}'
                   3025: 		.'\includegraphics'.$size.'{'.$file.'} ';
1.354     foxr     3026: 	    my $closure;
                   3027: 	    ($currentstring, $closure) = &align_latex_image($align, 
                   3028: 							    $latex_rendering, 
                   3029: 							    $currentstring, 
                   3030: 							    $width_param, 
                   3031: 							    $height_param);
                   3032: 	    $currentstring .= $closure;
1.353     foxr     3033: 						
1.122     albertel 3034: 	} else {
1.261     foxr     3035: 	    &Apache::lonxml::debug("$src does not exist");
1.122     albertel 3036: 	    #original image file doesn't exist so check the alt attribute
                   3037: 	    my $alt = 
                   3038: 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
                   3039: 	    unless ($alt) {
                   3040: 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
                   3041: 	    }
                   3042: 
1.256     albertel 3043: 	    if ($alt) { $currentstring .= ' '.$alt.' '; }
1.59      sakharuk 3044: 	}
1.249     foxr     3045: 
                   3046: 	# And here's where the semi-quote breaks down: allow the user
                   3047:         # to edit the beast as well by rendering the problem for edit:
1.186     albertel 3048:     } elsif ($target eq 'edit') {
1.368     banghart 3049:         my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
1.186     albertel 3050: 	$currentstring .=&Apache::edit::tag_start($target,$token);
                   3051: 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
1.368     banghart 3052: 	    &Apache::edit::browse('src',undef,'alt',$only).' '.
1.186     albertel 3053: 	    &Apache::edit::search('src',undef,'alt').'<br />';
                   3054: 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
                   3055: 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
                   3056: 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
                   3057: 	$currentstring .=&Apache::edit::text_arg('TeXwidth (mm):','TeXwidth',$token,5);
                   3058: 	$currentstring .=&Apache::edit::text_arg('TeXheight (mm):','TeXheight',$token,5);
1.234     albertel 3059: 	$currentstring .=&Apache::edit::select_arg('Alignment:','align',
                   3060: 						   ['','bottom','middle','top','left','right'],$token,5);
1.249     foxr     3061: 	$currentstring .=&Apache::edit::select_arg('TeXwrap:', 'TeXwrap',
1.348     albertel 3062: 						   ['', 'none','parbox', 'parpic', 'wrapfigure'], $token, 2);
1.332     www      3063: 	$currentstring .=&Apache::edit::select_arg('Encrypt URL:','encrypturl',
1.322     albertel 3064: 						   ['no','yes'], $token, 2);
1.186     albertel 3065: 	$currentstring .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.249     foxr     3066: 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
                   3067: 	my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
                   3068: 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
                   3069: 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
                   3070: 
1.381     www      3071:         if ($token->[2]{'src'}=~/\$/) {
                   3072:            $currentstring.='Variable image source';
                   3073:         } else {
                   3074: 	   $currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
                   3075: 	   if ($width) { $currentstring.=' width="'.$width.'" '; }
                   3076: 	   if ($height) { $currentstring.=' height="'.$height.'" '; }
                   3077: 	   $currentstring .= ' />';
                   3078:         }
1.186     albertel 3079:     } elsif ($target eq 'modified') {
1.210     albertel 3080: 	my ($osrc,$owidth,$oheight)=
                   3081: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
                   3082: 	my $ctag=&Apache::edit::get_new_args($token,$parstack,
1.234     albertel 3083: 					     $safeeval,'src','alt','align',
1.249     foxr     3084: 					     'TeXwidth','TeXheight', 'TeXwrap',
1.322     albertel 3085: 					     'width','height','encrypturl');
1.210     albertel 3086: 	my ($nsrc,$nwidth,$nheight)=
                   3087: 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
                   3088: 	my $loc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$nsrc);
                   3089: 	&image_replication($loc);
                   3090: 	my ($iwidth,$iheight);
                   3091: 	if (-e $loc) {
                   3092: 	    my $image = Image::Magick->new;
                   3093: 	    $image->Read($loc);
                   3094: 	    ($iwidth, $iheight) = ($image->Get('width'),
                   3095: 				   $image->Get('height'));
                   3096: 	}
                   3097: 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
                   3098: 	    # changed image or no size specified,
                   3099:             # if they didn't explicitly change the 
                   3100:             # width or height use the ones from the image
                   3101: 	    if ($iwidth && $iheight) {
                   3102: 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
                   3103: 		    $token->[2]{'width'} = $iwidth;$ctag=1;
                   3104: 		}
                   3105: 		if ($oheight == $nheight || (!$nwidth && !$nheight)) {
                   3106: 		    $token->[2]{'height'}=$iheight;$ctag=1;
1.186     albertel 3107: 		}
                   3108: 	    }
                   3109: 	}
1.210     albertel 3110: 	my ($cwidth,$cheight)=($token->[2]{'width'},$token->[2]{'height'});
                   3111: 	# if we don't have a width or height
                   3112: 	if ($iwidth && $cwidth && !$cheight) {
                   3113: 	    $token->[2]{'height'}=int(($cwidth/$iwidth)*$iheight);$ctag=1;
                   3114: 	}
                   3115: 	if ($iheight && $cheight && !$cwidth) {
                   3116: 	    $token->[2]{'width'}=int(($cheight/$iheight)*$iwidth);$ctag=1;
                   3117: 	}
                   3118: 	if ($ctag) {$currentstring=&Apache::edit::rebuild_tag($token);}
1.122     albertel 3119:     }
1.261     foxr     3120: 
1.122     albertel 3121:     return $currentstring;
                   3122: }
                   3123: 
                   3124: sub end_img {
                   3125:     my ($target,$token) = @_;
                   3126:     my $currentstring = '';
1.325     albertel 3127:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3128: 	$currentstring = $token->[2];
                   3129:     } elsif ($target eq 'tex') {
                   3130: 	$currentstring = '';
                   3131:     }
                   3132:     return $currentstring;
                   3133: }
                   3134: 
1.181     sakharuk 3135: #-- <applet> tag (end tag required)
1.122     albertel 3136: sub start_applet {
                   3137:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   3138:     
                   3139:     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
1.290     albertel 3140:     &Apache::lonxml::extlink($code);
1.122     albertel 3141:     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
                   3142: 					   undef,1);
1.290     albertel 3143:     &Apache::lonxml::extlink($archive);
1.122     albertel 3144:     my $currentstring = '';
1.325     albertel 3145:     if ($target eq 'web' || $target eq 'webgrade') {
1.267     albertel 3146: 	if ($env{'browser.appletsuppress'} ne 'on') {
1.250     albertel 3147: 	    $currentstring = &Apache::lonenc::encrypt_ref($token,
                   3148: 							  {'code'=>$code,
                   3149: 							   'archive'=>$archive}
                   3150: 							  );
1.122     albertel 3151: 	} else {
                   3152: 	    my $alttag= &Apache::lonxml::get_param('alt',$parstack,
                   3153: 						   $safeeval,undef,1);
                   3154: 	    unless ($alttag) {
                   3155: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
                   3156: 						 $code);
1.44      sakharuk 3157: 	    }
1.122     albertel 3158: 	    $currentstring='[APPLET: '.$alttag.']';
1.1       sakharuk 3159: 	}
1.122     albertel 3160:     } elsif ($target eq 'tex') {
1.361     foxr     3161: 	# Turn off some stuff we can't be inside thank you LaTeX
                   3162: 	
                   3163: 
                   3164: 	my $restart_sub = 0;
                   3165: 	my $restart_sup = 0;
                   3166: 
                   3167: 	# Since <sub> and <sup> are simple tags it's ok to turn off/on
                   3168: 	# using the start_ stop_ functions.. those tags only care about
                   3169: 	# $target.
                   3170: 
                   3171: 	if (&is_inside_of($tagstack, "sub")) {
                   3172: 	    $restart_sub = 1;
                   3173: 	    $currentstring .= &end_sub($target, $token, $tagstack, 
                   3174: 				       $parstack, $parser, $safeeval);
                   3175: 	}
                   3176: 	if (&is_inside_of($tagstack, "sup")) {
                   3177: 	    $restart_sup = 1;
                   3178: 	    $currentstring .= &end_sup($target, $token, $tagstack,
                   3179: 				       $parstack, $parser, $safeeval);
                   3180: 	}
                   3181: 
                   3182: 	# Now process the applet; just replace it with its alt attribute.
                   3183: 
1.177     albertel 3184: 	my $alttag= &Apache::lonxml::get_param('alt',$parstack,
                   3185: 					       $safeeval,undef,1);
                   3186: 	unless ($alttag) {
                   3187: 	    my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,
                   3188: 						undef,1);
                   3189: 	    $alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],
                   3190: 					     $code);
1.175     sakharuk 3191: 	}
1.177     albertel 3192: 	$currentstring.='\begin{center} \fbox{Java Applet: '.$alttag.
                   3193: 	    '.}\end{center}';
1.361     foxr     3194: 
                   3195: 	# Turn stuff back on that we can't be inside of.
                   3196: 
                   3197: 	if ($restart_sub) {
                   3198: 	    $currentstring .= &start_sub($target, $token, $tagstack,
                   3199: 					$parstack, $parser, $safeeval);
                   3200: 	}
                   3201: 	if ($restart_sup) {
                   3202: 	    $currentstring .= &start_sup($target, $token, $tagstack,
                   3203: 					 $parstack, $parser, $safeeval);
                   3204: 	}
1.122     albertel 3205:     } 
                   3206:     return $currentstring;
                   3207: }
                   3208: 
                   3209: sub end_applet {
                   3210:     my ($target,$token) = @_;
                   3211:     my $currentstring = '';
1.325     albertel 3212:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3213: 	$currentstring = $token->[2];
                   3214:     } elsif ($target eq 'tex') {
                   3215:     } 
                   3216:     return $currentstring;
                   3217: }
                   3218: 
1.181     sakharuk 3219: #-- <embed> tag (end tag optional/required)
1.122     albertel 3220: sub start_embed {    
                   3221:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   3222:     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
1.290     albertel 3223:     &Apache::lonxml::extlink($src);
1.122     albertel 3224:     my $currentstring = '';
1.325     albertel 3225:     if ($target eq 'web' || $target eq 'webgrade') {
1.267     albertel 3226: 	if ($env{'browser.embedsuppress'} ne 'on') {
1.250     albertel 3227: 	    $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src});
1.122     albertel 3228: 	} else {
                   3229: 	    my $alttag=&Apache::lonxml::get_param
                   3230: 		('alt',$parstack,$safeeval,undef,1);
                   3231: 	    unless ($alttag) {
                   3232: 		$alttag=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
1.42      albertel 3233: 	    }
1.122     albertel 3234: 	    $currentstring='[EMBED: '.$alttag.']';
1.10      www      3235: 	}
1.122     albertel 3236:     } elsif ($target eq 'tex') {
                   3237:     } 
                   3238:     return $currentstring;
                   3239: }
                   3240: 
                   3241: sub end_embed {
                   3242:     my ($target,$token) = @_;
                   3243:     my $currentstring = '';
1.325     albertel 3244:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3245: 	$currentstring = $token->[2];     
1.175     sakharuk 3246:     } elsif ($target eq 'tex') {  
1.122     albertel 3247:     } 
                   3248:     return $currentstring;
                   3249: }
                   3250: 
1.181     sakharuk 3251: #-- <param> tag (end tag forbidden)
1.122     albertel 3252: sub start_param {
                   3253:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
1.290     albertel 3254:     if (&Apache::lonxml::get_param('name',$parstack,
                   3255: 				   $safeeval,undef,1)=~/^cabbase$/i) {
                   3256: 	my $value=&Apache::lonxml::get_param('value',$parstack,
                   3257: 					     $safeeval,undef,1);
                   3258: 	&Apache::lonxml::extlink($value);
                   3259:     } 
                   3260:   
                   3261:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
                   3262:     &Apache::lonxml::extlink($src);
1.122     albertel 3263:     my $currentstring = '';
1.325     albertel 3264:     if ($target eq 'web' || $target eq 'webgrade') {
1.250     albertel 3265: 	my %toconvert;
                   3266: 	my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
                   3267: 	if ($src) { $toconvert{'src'}= $src; }
                   3268: 	my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval,
                   3269: 					    undef,1);
1.251     albertel 3270: 	if ($name=~/^cabbase$/i) {
                   3271: 	    $toconvert{'value'}=&Apache::lonxml::get_param('value',$parstack,
                   3272: 							   $safeeval,undef,1);
                   3273: 	}
1.250     albertel 3274: 	$currentstring = &Apache::lonenc::encrypt_ref($token,\%toconvert);
1.122     albertel 3275:     } elsif ($target eq 'tex') {
                   3276:     } 
                   3277:     return $currentstring;
                   3278: }
                   3279: 
                   3280: sub end_param {
                   3281:     my ($target,$token) = @_;
                   3282:     my $currentstring = '';
1.325     albertel 3283:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3284: 	$currentstring = $token->[2];     
                   3285:     } elsif ($target eq 'tex') {
                   3286:     } 
                   3287:     return $currentstring;
                   3288: }
                   3289: 
                   3290: #-- <allow> tag
                   3291: sub start_allow {
                   3292:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   3293:     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
1.290     albertel 3294:     &Apache::lonxml::extlink($src);
                   3295: 
1.241     albertel 3296:     if ($target eq 'tex') { &image_replication($src); }
1.122     albertel 3297:     my $result;
                   3298:     if ($target eq 'edit') {
                   3299: 	$result .=&Apache::edit::tag_start($target,$token);
                   3300: 	$result .=&Apache::edit::text_arg('File Spec:','src',$token,70);
                   3301: 	$result .=&Apache::edit::end_row();#.&Apache::edit::start_spanning_row();
                   3302:     } elsif ($target eq 'modified') {
                   3303: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   3304: 						     $safeeval,'src');
                   3305: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   3306:     }
                   3307:     return $result;
                   3308: }
                   3309: 
                   3310: sub end_allow {
                   3311:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   3312:     if ( $target eq 'edit') { return (&Apache::edit::end_table()); }
                   3313:     return '';
                   3314: }
1.119     www      3315: 
1.181     sakharuk 3316: #-- Frames (end tag required)
                   3317: #-- <frameset>
1.122     albertel 3318: sub start_frameset {
                   3319:     my ($target,$token) = @_;
1.277     foxr     3320:     my $currentstring = '';	# Close any pending para.
1.325     albertel 3321:     if ($target eq 'web' || $target eq 'webgrade') { 
1.328     albertel 3322: 	$currentstring = 
                   3323: 	    &Apache::loncommon::start_page($Apache::londefdef::title,
                   3324: 					   $Apache::londefdef::head,
                   3325: 					   {'add_entries'    => $token->[2],
                   3326: 					    'no_title'       => 1,
                   3327: 					    'force_register' => 1,
                   3328: 					    'frameset'       => 1,});
                   3329: 
1.122     albertel 3330:     }
                   3331:     return $currentstring;
                   3332: }
                   3333: 
                   3334: sub end_frameset {
                   3335:     my ($target,$token) = @_;
                   3336:     my $currentstring = '';
1.325     albertel 3337:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3338: 	$currentstring = $token->[2];
                   3339:     }
                   3340:     return $currentstring;
                   3341: }
1.162     sakharuk 3342: 
1.181     sakharuk 3343: #-- <xmp> (end tag required)
1.162     sakharuk 3344: sub start_xmp {
                   3345:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   3346:     my $currentstring = '';
1.325     albertel 3347:     if ($target eq 'web' || $target eq 'webgrade') {
1.162     sakharuk 3348: 	$currentstring .= $token->[4];
                   3349:     } elsif ($target eq 'tex') {
                   3350: 	$currentstring .= '\begin{verbatim}';
                   3351:     } 
                   3352:     return $currentstring;
                   3353: }
                   3354: 
                   3355: sub end_xmp {
                   3356:     my ($target,$token) = @_;
                   3357:     my $currentstring = '';
1.325     albertel 3358:     if ($target eq 'web' || $target eq 'webgrade') {
1.162     sakharuk 3359: 	$currentstring .= $token->[2];
                   3360:     } elsif ($target eq 'tex') {
                   3361: 	$currentstring .= '\end{verbatim}';
                   3362:     }
                   3363:     return $currentstring;
                   3364: }
                   3365: 
1.181     sakharuk 3366: #-- <pre> (end tag required)
1.122     albertel 3367: sub start_pre {
1.126     sakharuk 3368:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
1.279     foxr     3369:     my $currentstring = &end_p();	# close off pending <p>
1.325     albertel 3370:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3371: 	$currentstring .= $token->[4];
                   3372:     } elsif ($target eq 'tex') {
1.136     sakharuk 3373: 	$currentstring .= '\begin{verbatim}';
1.319     albertel 3374: 	&Apache::lonxml::disable_LaTeX_substitutions();
1.122     albertel 3375:     } 
                   3376:     return $currentstring;
                   3377: }
                   3378: 
                   3379: sub end_pre {
                   3380:     my ($target,$token) = @_;
                   3381:     my $currentstring = '';
1.325     albertel 3382:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3383: 	$currentstring .= $token->[2];
                   3384:     } elsif ($target eq 'tex') {
1.136     sakharuk 3385: 	$currentstring .= '\end{verbatim}';
1.319     albertel 3386: 	&Apache::lonxml::enable_LaTeX_substitutions();
1.122     albertel 3387:     }
                   3388:     return $currentstring;
                   3389: }
                   3390: 
                   3391: #-- <insert>
                   3392: sub start_insert {
                   3393:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   3394:     my $currentstring = '';
1.325     albertel 3395:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3396: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
                   3397: 	$currentstring .= '<b>'.$display.'</b>';;
                   3398:     }
                   3399:     return $currentstring;
                   3400: }
                   3401: 
                   3402: sub end_insert {
                   3403:     my ($target,$token) = @_;
                   3404:     my $currentstring = '';
1.325     albertel 3405:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3406: 	$currentstring .= '';
                   3407:     }
                   3408:     return $currentstring;
                   3409: }
                   3410: 
                   3411: #-- <externallink>
                   3412: sub start_externallink {
                   3413:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
                   3414:     my $currentstring = '';
1.325     albertel 3415:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3416: 	my $display = &Apache::lonxml::get_param('display',$parstack,$safeeval,undef,1);
                   3417: 	$currentstring .= '<b>'.$display.'</b>';;
                   3418:     }
                   3419:     return $currentstring;
                   3420: }
                   3421: 
                   3422: sub end_externallink {
                   3423:     my ($target,$token) = @_;
                   3424:     my $currentstring = '';
1.325     albertel 3425:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3426: 	$currentstring .= '';
                   3427:     }
                   3428:     return $currentstring;
                   3429: }
                   3430: 
                   3431: #-- <blankspace heigth="">
                   3432: sub start_blankspace {
                   3433:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
1.279     foxr     3434:     my $currentstring = &end_p();	# closes off any unclosed <p>
1.122     albertel 3435:     if ($target eq 'tex') {
                   3436: 	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
                   3437: 	$currentstring .= '\vskip '.$howmuch.' ';
                   3438:     }
                   3439:     return $currentstring;
                   3440: }
                   3441: 
                   3442: sub end_blankspace {
                   3443:     my ($target,$token) = @_;
                   3444:     my $currentstring = '';
                   3445:     if ($target eq 'tex') {
                   3446: 	$currentstring .= '';
                   3447:     }
                   3448:     return $currentstring;
                   3449: }
                   3450: 
1.181     sakharuk 3451: #-- <abbr> tag (end tag required)
1.122     albertel 3452: sub start_abbr {
                   3453:     my ($target,$token) = @_;
                   3454:     my $currentstring = '';
1.325     albertel 3455:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3456: 	$currentstring = $token->[4];     
                   3457:     } 
                   3458:     return $currentstring;
                   3459: }
                   3460: 
                   3461: sub end_abbr {
                   3462:     my ($target,$token) = @_;
                   3463:     my $currentstring = '';
1.325     albertel 3464:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3465: 	$currentstring = $token->[2];    
                   3466:     } 
                   3467:     return $currentstring;
                   3468: }
                   3469: 
1.181     sakharuk 3470: #-- <acronym> tag (end tag required)
1.122     albertel 3471: sub start_acronym {
                   3472:     my ($target,$token) = @_;
                   3473:     my $currentstring = '';
1.325     albertel 3474:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3475: 	$currentstring = $token->[4];     
                   3476:     } 
                   3477:     return $currentstring;
                   3478: }
                   3479: 
                   3480: sub end_acronym {
                   3481:     my ($target,$token) = @_;
                   3482:     my $currentstring = '';
1.325     albertel 3483:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3484: 	$currentstring = $token->[2];    
                   3485:     } 
                   3486:     return $currentstring;
                   3487: }
                   3488: 
1.181     sakharuk 3489: #-- <area> tag (end tag forbidden)
1.122     albertel 3490: sub start_area {
                   3491:     my ($target,$token) = @_;
                   3492:     my $currentstring = '';
1.325     albertel 3493:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3494: 	$currentstring = $token->[4];     
                   3495:     } 
                   3496:     return $currentstring;
                   3497: }
                   3498: 
                   3499: sub end_area {
                   3500:     my ($target,$token) = @_;
                   3501:     my $currentstring = '';
1.325     albertel 3502:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3503: 	$currentstring = $token->[2];    
                   3504:     } 
                   3505:     return $currentstring;
                   3506: }
                   3507: 
1.181     sakharuk 3508: #-- <base> tag (end tag forbidden)
1.122     albertel 3509: sub start_base {
                   3510:     my ($target,$token) = @_;
                   3511:     my $currentstring = '';
1.325     albertel 3512:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3513: 	$currentstring = $token->[4];     
1.161     sakharuk 3514:     }
1.122     albertel 3515:     return $currentstring;
                   3516: }
                   3517: 
                   3518: sub end_base {
                   3519:     my ($target,$token) = @_;
                   3520:     my $currentstring = '';
1.325     albertel 3521:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3522: 	$currentstring = $token->[2];    
                   3523:     } 
                   3524:     return $currentstring;
                   3525: }
                   3526: 
1.181     sakharuk 3527: #-- <bdo> tag (end tag required)
1.122     albertel 3528: sub start_bdo {
                   3529:     my ($target,$token) = @_;
                   3530:     my $currentstring = '';
1.325     albertel 3531:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3532: 	$currentstring = $token->[4];     
                   3533:     } 
                   3534:     return $currentstring;
                   3535: }
                   3536: 
                   3537: sub end_bdo {
                   3538:     my ($target,$token) = @_;
                   3539:     my $currentstring = '';
1.325     albertel 3540:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3541: 	$currentstring = $token->[2];    
                   3542:     } 
                   3543:     return $currentstring;
                   3544: }
                   3545: 
1.181     sakharuk 3546: #-- <bgsound> tag (end tag optional)
1.122     albertel 3547: sub start_bgsound {
                   3548:     my ($target,$token) = @_;
                   3549:     my $currentstring = '';
1.325     albertel 3550:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3551: 	$currentstring = $token->[4];     
                   3552:     } 
                   3553:     return $currentstring;
                   3554: }
                   3555: 
                   3556: sub end_bgsound {
                   3557:     my ($target,$token) = @_;
                   3558:     my $currentstring = '';
1.325     albertel 3559:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3560: 	$currentstring = $token->[2];    
                   3561:     } 
                   3562:     return $currentstring;
                   3563: }
                   3564: 
1.181     sakharuk 3565: #-- <blink> tag (end tag required)
1.122     albertel 3566: sub start_blink {
                   3567:     my ($target,$token) = @_;
                   3568:     my $currentstring = '';
1.325     albertel 3569:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3570: 	$currentstring = $token->[4];     
                   3571:     } 
                   3572:     return $currentstring;
                   3573: }
                   3574: 
                   3575: sub end_blink {
                   3576:     my ($target,$token) = @_;
                   3577:     my $currentstring = '';
1.325     albertel 3578:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3579: 	$currentstring = $token->[2];    
                   3580:     } 
                   3581:     return $currentstring;
                   3582: }
                   3583: 
1.181     sakharuk 3584: #-- <blockquote> tag (end tag required)
1.122     albertel 3585: sub start_blockquote {
                   3586:     my ($target,$token) = @_;
1.279     foxr     3587:     my $currentstring = &end_p();	# Close any unclosed <p>
1.325     albertel 3588:     if ($target eq 'web' || $target eq 'webgrade') {
1.277     foxr     3589: 	$currentstring .= $token->[4];     
1.122     albertel 3590:     } 
1.339     foxr     3591:     if ($target eq 'tex') {
                   3592: 	$currentstring .= '\begin{quote}';
                   3593:     }
1.122     albertel 3594:     return $currentstring;
                   3595: }
                   3596: 
                   3597: sub end_blockquote {
                   3598:     my ($target,$token) = @_;
                   3599:     my $currentstring = '';
1.325     albertel 3600:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3601: 	$currentstring = $token->[2];    
                   3602:     } 
1.339     foxr     3603:     if ($target eq 'tex') {
                   3604: 	$currentstring = '\end{quote}';
                   3605:     }
1.122     albertel 3606:     return $currentstring;
                   3607: }
                   3608: 
1.181     sakharuk 3609: #-- <button> tag (end tag required)
1.122     albertel 3610: sub start_button {
                   3611:     my ($target,$token) = @_;
                   3612:     my $currentstring = '';
1.325     albertel 3613:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3614: 	$currentstring = $token->[4];     
                   3615:     } 
                   3616:     return $currentstring;
                   3617: }
                   3618: 
                   3619: sub end_button {
                   3620:     my ($target,$token) = @_;
                   3621:     my $currentstring = '';
1.325     albertel 3622:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3623: 	$currentstring = $token->[2];    
                   3624:     } 
                   3625:     return $currentstring;
                   3626: }
                   3627: 
1.181     sakharuk 3628: #-- <caption> tag (end tag required)
1.122     albertel 3629: sub start_caption {
                   3630:     my ($target,$token) = @_;
                   3631:     my $currentstring = '';
1.325     albertel 3632:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3633: 	$currentstring = $token->[4];     
                   3634:     } 
                   3635:     return $currentstring;
                   3636: }
                   3637: 
                   3638: sub end_caption {
                   3639:     my ($target,$token) = @_;
                   3640:     my $currentstring = '';
1.325     albertel 3641:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3642: 	$currentstring = $token->[2];    
                   3643:     } 
                   3644:     return $currentstring;
                   3645: }
                   3646: 
1.181     sakharuk 3647: #-- <col> tag (end tag forbdden)
1.122     albertel 3648: sub start_col {
                   3649:     my ($target,$token) = @_;
                   3650:     my $currentstring = '';
1.325     albertel 3651:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3652: 	$currentstring = $token->[4];     
                   3653:     } 
                   3654:     return $currentstring;
                   3655: }
                   3656: 
                   3657: sub end_col {
                   3658:     my ($target,$token) = @_;
                   3659:     my $currentstring = '';
1.325     albertel 3660:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3661: 	$currentstring = $token->[2];    
                   3662:     } 
                   3663:     return $currentstring;
                   3664: }
                   3665: 
1.181     sakharuk 3666: #-- <colgroup> tag (end tag optional)
1.122     albertel 3667: sub start_colgroup {
                   3668:     my ($target,$token) = @_;
                   3669:     my $currentstring = '';
1.325     albertel 3670:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3671: 	$currentstring = $token->[4];     
                   3672:     } 
                   3673:     return $currentstring;
                   3674: }
                   3675: 
                   3676: sub end_colgroup {
                   3677:     my ($target,$token) = @_;
                   3678:     my $currentstring = '';
1.325     albertel 3679:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3680: 	$currentstring = $token->[2];    
                   3681:     } 
                   3682:     return $currentstring;
                   3683: }
                   3684: 
1.181     sakharuk 3685: #-- <del> tag (end tag required)
1.122     albertel 3686: sub start_del {
                   3687:     my ($target,$token) = @_;
                   3688:     my $currentstring = '';
1.325     albertel 3689:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3690: 	$currentstring = $token->[4];     
                   3691:     } 
                   3692:     return $currentstring;
                   3693: }
                   3694: 
                   3695: sub end_del {
                   3696:     my ($target,$token) = @_;
                   3697:     my $currentstring = '';
1.325     albertel 3698:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3699: 	$currentstring = $token->[2];    
                   3700:     } 
                   3701:     return $currentstring;
                   3702: }
                   3703: 
1.181     sakharuk 3704: #-- <fieldset> tag (end tag required)
1.122     albertel 3705: sub start_fieldset {
                   3706:     my ($target,$token) = @_;
                   3707:     my $currentstring = '';
1.325     albertel 3708:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3709: 	$currentstring = $token->[4];     
                   3710:     } 
                   3711:     return $currentstring;
                   3712: }
                   3713: 
                   3714: sub end_fieldset {
                   3715:     my ($target,$token) = @_;
                   3716:     my $currentstring = '';
1.325     albertel 3717:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3718: 	$currentstring = $token->[2];    
                   3719:     } 
                   3720:     return $currentstring;
                   3721: }
                   3722: 
1.181     sakharuk 3723: #-- <frame> tag (end tag forbidden)
1.122     albertel 3724: sub start_frame {
                   3725:     my ($target,$token) = @_;
                   3726:     my $currentstring = '';
1.325     albertel 3727:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3728: 	$currentstring = $token->[4];     
                   3729:     } 
                   3730:     return $currentstring;
                   3731: }
                   3732: 
                   3733: sub end_frame {
                   3734:     my ($target,$token) = @_;
                   3735:     my $currentstring = '';
1.325     albertel 3736:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3737: 	$currentstring = $token->[2];    
                   3738:     } 
                   3739:     return $currentstring;
                   3740: }
                   3741: 
1.181     sakharuk 3742: #-- <iframe> tag (end tag required)
1.122     albertel 3743: sub start_iframe {
                   3744:     my ($target,$token) = @_;
                   3745:     my $currentstring = '';
1.325     albertel 3746:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3747: 	$currentstring = $token->[4];     
                   3748:     } 
                   3749:     return $currentstring;
                   3750: }
                   3751: 
                   3752: sub end_iframe {
                   3753:     my ($target,$token) = @_;
                   3754:     my $currentstring = '';
1.325     albertel 3755:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3756: 	$currentstring = $token->[2];    
                   3757:     } 
                   3758:     return $currentstring;
                   3759: }
                   3760: 
1.181     sakharuk 3761: #-- <ins> tag (end tag required)
1.122     albertel 3762: sub start_ins {
                   3763:     my ($target,$token) = @_;
                   3764:     my $currentstring = '';
1.325     albertel 3765:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3766: 	$currentstring = $token->[4];     
                   3767:     } 
                   3768:     return $currentstring;
                   3769: }
                   3770: 
                   3771: sub end_ins {
                   3772:     my ($target,$token) = @_;
                   3773:     my $currentstring = '';
1.325     albertel 3774:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3775: 	$currentstring = $token->[2];    
                   3776:     } 
                   3777:     return $currentstring;
                   3778: }
                   3779: 
1.181     sakharuk 3780: #-- <isindex> tag (end tag forbidden)
1.122     albertel 3781: sub start_isindex {
                   3782:     my ($target,$token) = @_;
                   3783:     my $currentstring = '';
1.325     albertel 3784:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3785: 	$currentstring = $token->[4];     
                   3786:     } 
                   3787:     return $currentstring;
                   3788: }
                   3789: 
                   3790: sub end_isindex {
                   3791:     my ($target,$token) = @_;
                   3792:     my $currentstring = '';
1.325     albertel 3793:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3794: 	$currentstring = $token->[2];    
                   3795:     } 
                   3796:     return $currentstring;
                   3797: }
                   3798: 
1.181     sakharuk 3799: #-- <keygen> tag (end tag forbidden)
1.122     albertel 3800: sub start_keygen {
                   3801:     my ($target,$token) = @_;
                   3802:     my $currentstring = '';
1.325     albertel 3803:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3804: 	$currentstring = $token->[4];     
                   3805:     } 
                   3806:     return $currentstring;
                   3807: }
                   3808: 
                   3809: sub end_keygen {
                   3810:     my ($target,$token) = @_;
                   3811:     my $currentstring = '';
1.325     albertel 3812:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3813: 	$currentstring = $token->[2];    
                   3814:     } 
                   3815:     return $currentstring;
                   3816: }
                   3817: 
                   3818: #-- <label> tag
                   3819: sub start_label {
                   3820:     my ($target,$token) = @_;
                   3821:     my $currentstring = '';
1.325     albertel 3822:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3823: 	$currentstring = $token->[4];     
                   3824:     } 
                   3825:     return $currentstring;
                   3826: }
                   3827: 
                   3828: sub end_label {
                   3829:     my ($target,$token) = @_;
                   3830:     my $currentstring = '';
1.325     albertel 3831:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3832: 	$currentstring = $token->[2];    
                   3833:     } 
                   3834:     return $currentstring;
                   3835: }
                   3836: 
1.181     sakharuk 3837: #-- <layer> tag (end tag required)
1.122     albertel 3838: sub start_layer {
                   3839:     my ($target,$token) = @_;
                   3840:     my $currentstring = '';
1.325     albertel 3841:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3842: 	$currentstring = $token->[4];     
                   3843:     } 
                   3844:     return $currentstring;
                   3845: }
                   3846: 
                   3847: sub end_layer {
                   3848:     my ($target,$token) = @_;
                   3849:     my $currentstring = '';
1.325     albertel 3850:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3851: 	$currentstring = $token->[2];    
                   3852:     } 
                   3853:     return $currentstring;
                   3854: }
                   3855: 
1.181     sakharuk 3856: #-- <legend> tag (end tag required)
1.122     albertel 3857: sub start_legend {
                   3858:     my ($target,$token) = @_;
                   3859:     my $currentstring = '';
1.325     albertel 3860:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3861: 	$currentstring = $token->[4];     
                   3862:     } 
                   3863:     return $currentstring;
                   3864: }
                   3865: 
                   3866: sub end_legend {
                   3867:     my ($target,$token) = @_;
                   3868:     my $currentstring = '';
1.325     albertel 3869:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3870: 	$currentstring = $token->[2];    
                   3871:     } 
                   3872:     return $currentstring;
                   3873: }
                   3874: 
1.181     sakharuk 3875: #-- <link> tag (end tag forbidden)
1.122     albertel 3876: sub start_link {
1.292     albertel 3877:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
1.122     albertel 3878:     my $currentstring = '';
1.324     albertel 3879:     if ($target eq 'web' || $target eq 'webgrade') {
1.291     albertel 3880: 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
                   3881: 					    undef,1);
                   3882: 	&Apache::lonxml::extlink($href);
1.122     albertel 3883: 	$currentstring = $token->[4];     
                   3884:     } 
                   3885:     return $currentstring;
                   3886: }
                   3887: 
                   3888: sub end_link {
                   3889:     my ($target,$token) = @_;
                   3890:     my $currentstring = '';
1.325     albertel 3891:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3892: 	$currentstring = $token->[2];    
                   3893:     } 
                   3894:     return $currentstring;
                   3895: }
                   3896: 
1.181     sakharuk 3897: #-- <marquee> tag (end tag optional)
1.122     albertel 3898: sub start_marquee {
                   3899:     my ($target,$token) = @_;
                   3900:     my $currentstring = '';
1.325     albertel 3901:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3902: 	$currentstring = $token->[4];     
                   3903:     } 
                   3904:     return $currentstring;
                   3905: }
                   3906: 
                   3907: sub end_marquee {
                   3908:     my ($target,$token) = @_;
                   3909:     my $currentstring = '';
1.325     albertel 3910:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3911: 	$currentstring = $token->[2];    
                   3912:     } 
                   3913:     return $currentstring;
                   3914: }
                   3915: 
1.179     sakharuk 3916: #-- <multicol> tag (end tag required)
                   3917: sub start_multicol {
1.122     albertel 3918:     my ($target,$token) = @_;
1.279     foxr     3919:     my $currentstring = &end_p();	# Close any pending <p>
1.325     albertel 3920:     if ($target eq 'web' || $target eq 'webgrade') {
1.277     foxr     3921: 	$currentstring .= $token->[4];     
1.122     albertel 3922:     } 
                   3923:     return $currentstring;
                   3924: }
                   3925: 
1.179     sakharuk 3926: sub end_multicol {
1.122     albertel 3927:     my ($target,$token) = @_;
                   3928:     my $currentstring = '';
1.325     albertel 3929:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3930: 	$currentstring = $token->[2];    
                   3931:     } 
                   3932:     return $currentstring;
                   3933: }
                   3934: 
1.179     sakharuk 3935: #-- <nobr> tag (end tag required)
1.122     albertel 3936: sub start_nobr {
                   3937:     my ($target,$token) = @_;
                   3938:     my $currentstring = '';
1.325     albertel 3939:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3940: 	$currentstring = $token->[4];     
1.179     sakharuk 3941:     }  elsif ($target eq 'tex') {
                   3942: 	$currentstring='\mbox{';
                   3943:     }
1.122     albertel 3944:     return $currentstring;
                   3945: }
                   3946: 
                   3947: sub end_nobr {
                   3948:     my ($target,$token) = @_;
                   3949:     my $currentstring = '';
1.325     albertel 3950:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3951: 	$currentstring = $token->[2];    
1.179     sakharuk 3952:     }   elsif ($target eq 'tex') {
                   3953: 	$currentstring='}';
                   3954:     }
1.122     albertel 3955:     return $currentstring;
                   3956: }
                   3957: 
1.179     sakharuk 3958: #-- <noembed> tag (end tag required)
1.122     albertel 3959: sub start_noembed {
                   3960:     my ($target,$token) = @_;
                   3961:     my $currentstring = '';
1.325     albertel 3962:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3963: 	$currentstring = $token->[4];     
                   3964:     } 
                   3965:     return $currentstring;
                   3966: }
                   3967: 
                   3968: sub end_noembed {
                   3969:     my ($target,$token) = @_;
                   3970:     my $currentstring = '';
1.325     albertel 3971:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3972: 	$currentstring = $token->[2];    
                   3973:     } 
                   3974:     return $currentstring;
                   3975: }
                   3976: 
1.179     sakharuk 3977: #-- <noframes> tag (end tag required)
1.122     albertel 3978: sub start_noframes {
                   3979:     my ($target,$token) = @_;
                   3980:     my $currentstring = '';
1.325     albertel 3981:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3982: 	$currentstring = $token->[4];     
                   3983:     } 
                   3984:     return $currentstring;
                   3985: }
                   3986: 
                   3987: sub end_noframes {
                   3988:     my ($target,$token) = @_;
                   3989:     my $currentstring = '';
1.325     albertel 3990:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 3991: 	$currentstring = $token->[2];    
                   3992:     } 
                   3993:     return $currentstring;
                   3994: }
                   3995: 
1.179     sakharuk 3996: #-- <nolayer> tag (end tag required)
1.122     albertel 3997: sub start_nolayer {
                   3998:     my ($target,$token) = @_;
                   3999:     my $currentstring = '';
1.325     albertel 4000:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4001: 	$currentstring = $token->[4];     
                   4002:     } 
                   4003:     return $currentstring;
                   4004: }
                   4005: 
                   4006: sub end_nolayer {
                   4007:     my ($target,$token) = @_;
                   4008:     my $currentstring = '';
1.325     albertel 4009:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4010: 	$currentstring = $token->[2];    
                   4011:     } 
                   4012:     return $currentstring;
                   4013: }
                   4014: 
1.179     sakharuk 4015: #-- <noscript> tag (end tag required)
1.122     albertel 4016: sub start_noscript {
                   4017:     my ($target,$token) = @_;
                   4018:     my $currentstring = '';
1.325     albertel 4019:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4020: 	$currentstring = $token->[4];     
                   4021:     } 
                   4022:     return $currentstring;
                   4023: }
                   4024: 
                   4025: sub end_noscript {
                   4026:     my ($target,$token) = @_;
                   4027:     my $currentstring = '';
1.325     albertel 4028:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4029: 	$currentstring = $token->[2];    
                   4030:     } 
                   4031:     return $currentstring;
                   4032: }
                   4033: 
1.179     sakharuk 4034: #-- <object> tag (end tag required)
1.122     albertel 4035: sub start_object {
                   4036:     my ($target,$token) = @_;
                   4037:     my $currentstring = '';
1.325     albertel 4038:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4039: 	$currentstring = $token->[4];     
                   4040:     } 
                   4041:     return $currentstring;
                   4042: }
                   4043: 
                   4044: sub end_object {
                   4045:     my ($target,$token) = @_;
                   4046:     my $currentstring = '';
1.325     albertel 4047:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4048: 	$currentstring = $token->[2];    
                   4049:     } 
                   4050:     return $currentstring;
                   4051: }
                   4052: 
1.179     sakharuk 4053: #-- <optgroup> tag (end tag required)
1.122     albertel 4054: sub start_optgroup {
                   4055:     my ($target,$token) = @_;
                   4056:     my $currentstring = '';
1.325     albertel 4057:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4058: 	$currentstring = $token->[4];     
                   4059:     } 
                   4060:     return $currentstring;
                   4061: }
                   4062: 
                   4063: sub end_optgroup {
                   4064:     my ($target,$token) = @_;
                   4065:     my $currentstring = '';
1.325     albertel 4066:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4067: 	$currentstring = $token->[2];    
                   4068:     } 
                   4069:     return $currentstring;
                   4070: }
                   4071: 
1.179     sakharuk 4072: #-- <samp> tag (end tag required)
1.122     albertel 4073: sub start_samp {
                   4074:     my ($target,$token) = @_;
                   4075:     my $currentstring = '';
1.325     albertel 4076:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4077: 	$currentstring = $token->[4];     
1.179     sakharuk 4078:     } elsif ($target eq 'tex') {
                   4079: 	$currentstring='\texttt{';
                   4080:     }
1.122     albertel 4081:     return $currentstring;
                   4082: }
                   4083: 
                   4084: sub end_samp {
                   4085:     my ($target,$token) = @_;
                   4086:     my $currentstring = '';
1.325     albertel 4087:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4088: 	$currentstring = $token->[2];    
1.179     sakharuk 4089:     } elsif ($target eq 'tex') {
                   4090: 	$currentstring='}';
                   4091:     }
1.122     albertel 4092:     return $currentstring;
                   4093: }
                   4094: 
                   4095: #-- <server> tag
                   4096: sub start_server {
                   4097:     my ($target,$token) = @_;
                   4098:     my $currentstring = '';
1.325     albertel 4099:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4100: 	$currentstring = $token->[4];     
                   4101:     } 
                   4102:     return $currentstring;
                   4103: }
                   4104: 
                   4105: sub end_server {
                   4106:     my ($target,$token) = @_;
                   4107:     my $currentstring = '';
1.325     albertel 4108:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4109: 	$currentstring = $token->[2];    
                   4110:     } 
                   4111:     return $currentstring;
                   4112: }
                   4113: 
1.179     sakharuk 4114: #-- <spacer> tag (end tag forbidden)
1.122     albertel 4115: sub start_spacer {
                   4116:     my ($target,$token) = @_;
1.279     foxr     4117:     my $currentstring = &end_p();	# Close off any open <p> tag.
1.325     albertel 4118:     if ($target eq 'web' || $target eq 'webgrade') {
1.277     foxr     4119: 	$currentstring .= $token->[4];     
1.122     albertel 4120:     } 
                   4121:     return $currentstring;
                   4122: }
                   4123: 
                   4124: sub end_spacer {
                   4125:     my ($target,$token) = @_;
                   4126:     my $currentstring = '';
1.325     albertel 4127:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4128: 	$currentstring = $token->[2];    
                   4129:     } 
                   4130:     return $currentstring;
                   4131: }
                   4132: 
1.179     sakharuk 4133: #-- <span> tag (end tag required)
1.122     albertel 4134: sub start_span {
                   4135:     my ($target,$token) = @_;
                   4136:     my $currentstring = '';
1.325     albertel 4137:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4138: 	$currentstring = $token->[4];     
                   4139:     } 
                   4140:     return $currentstring;
                   4141: }
                   4142: 
                   4143: sub end_span {
                   4144:     my ($target,$token) = @_;
                   4145:     my $currentstring = '';
1.325     albertel 4146:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4147: 	$currentstring = $token->[2];    
                   4148:     } 
                   4149:     return $currentstring;
                   4150: }
                   4151: 
1.179     sakharuk 4152: #-- <tbody> tag (end tag optional)
1.122     albertel 4153: sub start_tbody {
                   4154:     my ($target,$token) = @_;
                   4155:     my $currentstring = '';
1.325     albertel 4156:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4157: 	$currentstring = $token->[4];     
                   4158:     } 
                   4159:     return $currentstring;
                   4160: }
                   4161: 
                   4162: sub end_tbody {
                   4163:     my ($target,$token) = @_;
                   4164:     my $currentstring = '';
1.325     albertel 4165:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4166: 	$currentstring = $token->[2];    
                   4167:     } 
                   4168:     return $currentstring;
                   4169: }
                   4170: 
1.166     sakharuk 4171: #-- <tfoot> tag (end tag optional)
1.122     albertel 4172: sub start_tfoot {
                   4173:     my ($target,$token) = @_;
                   4174:     my $currentstring = '';
1.325     albertel 4175:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4176: 	$currentstring = $token->[4];     
                   4177:     } 
                   4178:     return $currentstring;
                   4179: }
                   4180: 
                   4181: sub end_tfoot {
                   4182:     my ($target,$token) = @_;
                   4183:     my $currentstring = '';
1.325     albertel 4184:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4185: 	$currentstring = $token->[2];    
                   4186:     } 
                   4187:     return $currentstring;
                   4188: }
                   4189: 
1.166     sakharuk 4190: #-- <thead> tag (end tag optional)
1.122     albertel 4191: sub start_thead {
                   4192:     my ($target,$token) = @_;
                   4193:     my $currentstring = '';
1.325     albertel 4194:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4195: 	$currentstring = $token->[4];     
                   4196:     } 
                   4197:     return $currentstring;
                   4198: }
1.10      www      4199: 
1.122     albertel 4200: sub end_thead {
                   4201:     my ($target,$token) = @_;
                   4202:     my $currentstring = '';
1.325     albertel 4203:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4204: 	$currentstring = $token->[2];    
                   4205:     } 
                   4206:     return $currentstring;
                   4207: }
1.119     www      4208: 
1.122     albertel 4209: #-- <var> tag
                   4210: sub start_var {
1.44      sakharuk 4211:     my ($target,$token) = @_;
                   4212:     my $currentstring = '';
1.325     albertel 4213:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4214: 	$currentstring = $token->[4];     
1.163     sakharuk 4215:     } elsif ($target eq 'tex') {
                   4216: 	$currentstring = '\textit{'; 
                   4217:     }
1.44      sakharuk 4218:     return $currentstring;
                   4219: }
1.10      www      4220: 
1.122     albertel 4221: sub end_var {
                   4222:     my ($target,$token) = @_;
1.44      sakharuk 4223:     my $currentstring = '';
1.325     albertel 4224:     if ($target eq 'web' || $target eq 'webgrade') {
1.163     sakharuk 4225: 	$currentstring = $token->[2];
                   4226:     } elsif ($target eq 'tex') {
                   4227: 	$currentstring = '}'; 
1.44      sakharuk 4228:     } 
                   4229:     return $currentstring;
                   4230: }
1.119     www      4231: 
1.163     sakharuk 4232: #-- <wbr> tag (end tag forbidden)
1.122     albertel 4233: sub start_wbr {
                   4234:     my ($target,$token) = @_;
                   4235:     my $currentstring = '';
1.325     albertel 4236:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4237: 	$currentstring = $token->[4];     
                   4238:     } 
                   4239:     return $currentstring;
1.98      albertel 4240: }
                   4241: 
1.122     albertel 4242: sub end_wbr {
                   4243:     my ($target,$token) = @_;
                   4244:     my $currentstring = '';
1.325     albertel 4245:     if ($target eq 'web' || $target eq 'webgrade') {
1.122     albertel 4246: 	$currentstring = $token->[2];    
                   4247:     } 
                   4248:     return $currentstring;
1.98      albertel 4249: }
1.134     sakharuk 4250: 
                   4251: #-- <hideweboutput> tag
                   4252: sub start_hideweboutput {
                   4253:     my ($target,$token) = @_;
1.325     albertel 4254:     if ($target eq 'web' || $target eq 'webgrade') {
1.134     sakharuk 4255: 	&Apache::lonxml::startredirection();     
                   4256:     } 
                   4257:     return '';
                   4258: }
                   4259: 
                   4260: sub end_hideweboutput {
                   4261:     my ($target,$token) = @_;
                   4262:     my $currentstring = '';
1.325     albertel 4263:     if ($target eq 'web' || $target eq 'webgrade') {
1.134     sakharuk 4264: 	$currentstring = &Apache::lonxml::endredirection();    
                   4265:     } 
                   4266:     return '';
                   4267: }
                   4268: 
1.94      sakharuk 4269: 
                   4270: sub image_replication {
                   4271:     my $src = shift;
1.187     albertel 4272:     if (not -e $src) { &Apache::lonnet::repcopy($src); }
                   4273:     #replicates eps or ps 
                   4274:     my $epssrc = my $pssrc = $src;
                   4275:     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
                   4276:     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
                   4277:     if (not -e $epssrc && not -e $pssrc) {
                   4278: 	my $result=&Apache::lonnet::repcopy($epssrc);
1.264     raeburn  4279: 	if ($result ne 'ok') { &Apache::lonnet::repcopy($pssrc); }
1.94      sakharuk 4280:     }
                   4281:     return '';
1.195     sakharuk 4282: }
1.275     foxr     4283: #
                   4284: #   Get correct sizing parameter for an image given
                   4285: #   it's initial ht. and wid.  This allows sizing of
                   4286: #   images that are generated on-the-fly (e.g. gnuplot)
                   4287: #   as well as serving as a utility for image_size.
                   4288: # 
                   4289: #  Parameter:
                   4290: #        height_param
                   4291: #        width_param    - Initial picture dimensions.
                   4292: #        scaling        - A scale factor.
                   4293: #        parstack,      - the current stack of tag attributes 
                   4294: #                         from the xml parser
                   4295: #        safeeval,      - pointer to the safespace
                   4296: #        depth,         - from what level in the stack to look for attributes
                   4297: #                         (assumes -1 if unspecified)
                   4298: #        cis            - look for attrubutes case insensitively
                   4299: #                         (assumes false)
                   4300: #
                   4301: # Returns:
                   4302: #   height, width   - new dimensions.
                   4303: #
                   4304: sub resize_image {
                   4305:     my ($height_param, $width_param, $scaling,
                   4306: 	$parstack, $safeeval, $depth, $cis) = @_;
                   4307: 
                   4308:     # First apply the scaling...
1.271     foxr     4309: 
1.275     foxr     4310:     $height_param = $height_param * $scaling;
                   4311:     $width_param  = $width_param  * $scaling;
1.261     foxr     4312: 
1.197     sakharuk 4313:     #do we have any specified LaTeX size of the picture?
1.261     foxr     4314:     my $toget='TeXwidth'; 
                   4315:     if ($cis) { 
                   4316: 	$toget=lc($toget); 
                   4317:     }
1.256     albertel 4318:     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
                   4319: 					      $safeeval,$depth,$cis);
                   4320:     $toget='TeXheight'; if ($cis) { $toget=lc($toget); }
                   4321:     my $TeXheight = &Apache::lonxml::get_param($toget,$parstack,
                   4322: 					       $safeeval,$depth,$cis);
1.197     sakharuk 4323:     #do we have any specified web size of the picture?
                   4324:     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
1.256     albertel 4325: 					   $depth,1);
                   4326:     if ($TeXwidth) { 
1.252     matthew  4327: 	my $old_width_param=$width_param;
1.197     sakharuk 4328: 	if ($TeXwidth=~/(\d+)\s*\%/) {
1.267     albertel 4329: 	    $width_param = $1*$env{'form.textwidth'}/100;
1.197     sakharuk 4330: 	} else { 
                   4331: 	    $width_param = $TeXwidth;
                   4332: 	}
1.266     albertel 4333: 	if ($TeXheight) {
                   4334: 	    $height_param = $TeXheight;
                   4335: 	} elsif ($old_width_param) {
                   4336: 	    $height_param=$TeXwidth/$old_width_param*$height_param;
                   4337: 	}
1.256     albertel 4338:     } elsif ($TeXheight) {
1.248     foxr     4339: 	$height_param = $TeXheight;
1.266     albertel 4340: 	if ($height_param) {
                   4341: 	    $width_param  = $TeXheight/$height_param*$width_param;
                   4342: 	}
1.256     albertel 4343:     } elsif ($width) {
1.252     matthew  4344: 	my $old_width_param=$width_param;
1.256     albertel 4345: 	$width_param = $width*$scaling;
1.266     albertel 4346: 	if ($old_width_param) {
                   4347: 	    $height_param=$width_param/$old_width_param*$height_param;
                   4348: 	}
1.252     matthew  4349:     }
1.267     albertel 4350:     if ($width_param > $env{'form.textwidth'}) {
1.252     matthew  4351:         my $old_width_param=$width_param;
1.267     albertel 4352: 	$width_param =0.95*$env{'form.textwidth'};
1.266     albertel 4353: 	if ($old_width_param) {
                   4354: 	    $height_param=$width_param/$old_width_param*$height_param;
                   4355: 	}
1.197     sakharuk 4356:     }
1.275     foxr     4357: 
                   4358:     return ($height_param, $width_param);
                   4359: }
                   4360: 
                   4361: sub image_size {
                   4362:     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
                   4363: 
                   4364:     #size of image from gif/jpg/jpeg/png 
                   4365:     my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
                   4366:     if (-e $ressrc) {
                   4367: 	$src = $ressrc;
                   4368:     }
                   4369:     my $image = Image::Magick->new;
                   4370:     my $current_figure = $image->Read($src);
                   4371:     my $width_param = $image->Get('width');
                   4372:     my $height_param = $image->Get('height');
                   4373:     &Apache::lonxml::debug("Image magick says: $src :  Height = $height_param width = $width_param");
                   4374:     undef($image);
                   4375: 
                   4376:     ($height_param, $width_param) = &resize_image($height_param, $width_param,
                   4377: 						  $scaling, $parstack, $safeeval, 
                   4378: 						  $depth, $cis);
                   4379: 
1.248     foxr     4380:     return ($height_param, $width_param);
                   4381: }
                   4382: 
                   4383: sub image_width {
                   4384:     my ($height, $width) = &image_size(@_);
                   4385:     return $width;
                   4386: }
                   4387: #  Not yet 100% sure this is correct in all circumstances..
                   4388: #  due to my uncertainty about mods to image_size.
                   4389: #
                   4390: sub image_height {
                   4391:     my ($height, $width) = &image_size(@_);
                   4392:     return $height;
1.197     sakharuk 4393: }
                   4394: 
1.256     albertel 4395: sub get_eps_image {
                   4396:     my ($src)=@_;
1.261     foxr     4397:     my $orig_src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1], $src);
1.285     foxr     4398: 
                   4399:     # In order to prevent the substitution of the alt text, we need to
                   4400:     # be sure the orig_src file is on system now so:
                   4401: 
                   4402:     if (! -e $orig_src) {
                   4403: 	&Apache::lonnet::repcopy($orig_src); # Failure is not completely fatal.
                   4404:     }
1.275     foxr     4405:     &Apache::lonxml::debug("get_eps_image: Original image: $orig_src");
1.344     albertel 4406:     my ($spath, $sname, $sext) = &fileparse($src, qr/\.(gif|png|jpg|jpeg)/i);
1.256     albertel 4407:     $src=~s/\.(gif|png|jpg|jpeg)$/\.eps/i;
                   4408:     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
1.275     foxr     4409:     &Apache::lonxml::debug("Filelocation gives: $src");
1.256     albertel 4410:     if (! -e $src) {
1.261     foxr     4411: 	&Apache::lonxml::debug("$src does not exist");
1.264     raeburn  4412: 	if (&Apache::lonnet::repcopy($src) ne 'ok' ) {
1.261     foxr     4413: 	    &Apache::lonxml::debug("Repcopy of $src failed (1)");
1.256     albertel 4414: 	    #if replication failed try to find ps file
                   4415: 	    $src=~s/\.eps$/\.ps/;
1.261     foxr     4416: 	    &Apache::lonxml::debug("Now looking for $src");
1.270     foxr     4417: 	    #if no ps file try to replicate it.
                   4418: 	    my $didrepcopy = &Apache::lonnet::repcopy($src);
                   4419: 	    &Apache::lonxml::debug("repcopy of $src ... $didrepcopy");
                   4420: 	    if ( (not -e $src) ||
                   4421: 		($didrepcopy ne 'ok')) {
1.261     foxr     4422: 		&Apache::lonxml::debug("Failed to find or replicate $src");
                   4423: 
1.256     albertel 4424: 		#if replication failed try to produce eps file dynamically
                   4425: 		$src=~s/\.ps$/\.eps/;
                   4426: 		my $temp_file;
1.267     albertel 4427: 		open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
1.256     albertel 4428: 		my $newsrc=$orig_src;
                   4429: 		$newsrc =~ s|(.*)/res/|/home/httpd/html/res/|;
1.275     foxr     4430: 		&Apache::lonxml::debug("queueing $newsrc for dynamic eps production.");
1.344     albertel 4431: 		print FILE ("$newsrc\n");
                   4432: 		close(FILE);
1.256     albertel 4433: 		$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
                   4434: 		$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
1.283     albertel 4435: 		if ($sext ne "") {	 # Put the ext. back in to uniquify.
                   4436: 		    $src =~ s/\.eps$/$sext.eps/;
                   4437: 		}
1.341     foxr     4438: 
1.256     albertel 4439: 	    }
1.343     foxr     4440: 
1.256     albertel 4441: 	}
1.341     foxr     4442:     } else {
                   4443: 	# If the postscript file has spaces in its name,
                   4444: 	# LaTeX will gratuitiously vomit.  Therefore
                   4445: 	# queue such files for copy with " " replaced by "_".
                   4446: 	# printout.pm will know them by their .ps  or .eps extensions.
                   4447: 	my $newsrc = $orig_src;
                   4448: 	$newsrc    =~  s|(.*)/res/|/home/httpd/html/res/|;
                   4449: 	open(FILE,">>/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat");
1.343     foxr     4450: 	print FILE "$src\n";
1.341     foxr     4451: 	close FILE;
                   4452: 	$src=~s|/home/httpd/html/res|/home/httpd/prtspool|;
                   4453: 	$src=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
1.256     albertel 4454:     }
                   4455:     my ($path,$file)=($src=~m|(.*)/([^/]*)$|);
1.344     albertel 4456:     $path =~ s/ /\_/g;
1.343     foxr     4457:     $file =~ s/ /\_/g;
1.261     foxr     4458:     &Apache::lonxml::debug("get_eps_image returning: $path / $file<BR />");
1.256     albertel 4459:     return ($path.'/',$file);
                   4460: }
                   4461: 
1.195     sakharuk 4462: sub eps_generation {
                   4463:     my ($src,$file,$width_param) = @_;	     
1.267     albertel 4464:     my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
1.195     sakharuk 4465:     my $temp_file = Apache::File->new('>>'.$filename); 
                   4466:     print $temp_file "$src\n";
                   4467:     my $newsrc = $src;
                   4468:     $newsrc =~ s/(\.gif|\.jpg|\.jpeg)$/\.eps/i;
1.345     albertel 4469:     $newsrc=~s{/home/httpd/html/res}{};
                   4470:     $newsrc=~s{/home/($LONCAPA::username_re)/public_html/}{/$1/};
                   4471:     $newsrc=~s{/\./}{/};
                   4472:     $newsrc=~s{/([^/]+)\.(ps|eps)}{/};
                   4473:     if ($newsrc=~m{/home/httpd/lonUsers/}) {
                   4474: 	$newsrc=~s{/home/httpd/lonUsers}{};
                   4475: 	$newsrc=~s{/($LONCAPA::domain_re)/./././}{/$1/};
1.213     sakharuk 4476:     }
1.345     albertel 4477:     if ($newsrc=~m{/userfiles/}) {
1.239     sakharuk 4478: 	return ' \graphicspath{{'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
                   4479:     } else {
                   4480: 	return ' \graphicspath{{/home/httpd/prtspool'.$newsrc.'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
                   4481:     }
1.197     sakharuk 4482: }
                   4483: 
                   4484: sub file_path {     
                   4485:     my $src=shift;
                   4486:     my ($file,$path); 
                   4487:     if ($src =~ m!(.*)/([^/]*)$!) {
                   4488: 	$file = $2; 
                   4489: 	$path = $1.'/'; 
                   4490:     } 
                   4491:     return $file,$path;
1.126     sakharuk 4492: }
1.294     foxr     4493: #  Converts a measurement in to mm from any of 
                   4494: #  the other valid LaTeX units of measure.
                   4495: #  If the units of measure are missing from the 
                   4496: #  parameter, it is assumed to be in and returned
                   4497: #  with mm units of measure
1.126     sakharuk 4498: sub recalc {
                   4499:     my $argument = shift;
                   4500:     if (not $argument=~/(mm|cm|in|pc|pt)/) {return $argument.' mm';}
1.132     sakharuk 4501:     $argument=~/\s*(\d+\.?\d*)\s*(mm|cm|in|pc|pt)/;
1.126     sakharuk 4502:     my $value=$1;
                   4503:     my $units=$2;
                   4504:     if ($units eq 'cm') {
                   4505: 	$value*=10;
                   4506:     } elsif ($units eq 'in') {
                   4507: 	$value*=25.4;
                   4508:     } elsif ($units eq 'pc') {
                   4509: 	$value*=(25.4*12/72.27);
                   4510:     } elsif ($units eq 'pt') {
                   4511: 	$value*=(25.4/72.27);
                   4512:     }
                   4513:     return $value.' mm';
1.94      sakharuk 4514: }
1.184     sakharuk 4515: 
                   4516: sub LATEX_length {
                   4517:     my $garbage=shift;
1.206     sakharuk 4518:     $garbage=~s/^\s+$//;
                   4519:     $garbage=~s/^\s+(\S.*)/$1/;#space before 
                   4520:     $garbage=~s/(.*\S)\s+$/$1/;#space after 
                   4521:     $garbage=~s/(\s)+/$1/;#only one space
                   4522:     $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
                   4523:     $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
                   4524:     $garbage=~s/([^\\])\$/$1/g;#$
                   4525:     $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
                   4526:    $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 4527:     $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;
                   4528:     $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;
                   4529:     $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 4530:     $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;
                   4531:     $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;
                   4532:     $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
                   4533:     #remove some other LaTeX command
                   4534:     $garbage=~s|\\(\w+)\\|\\|g;	 
                   4535:     $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
                   4536:     $garbage=~s|\+|11|g;
1.184     sakharuk 4537:     my  $value=length($garbage);
                   4538:     return $value;
                   4539: }
                   4540: 
1.353     foxr     4541: #   Wrap image 'stuff' inside of the LaTeX required to implement 
                   4542: #   alignment:
                   4543: #     align_tex_image(align, latex_rendering, image)
                   4544: #   Where:
                   4545: #     align   - The HTML alignment specification.
                   4546: #     latex_rendering - rendering hint for latex.
                   4547: #     image   - The LaTeX needed to insert the image itsef.
                   4548: #     width,height - dimensions of the image.
1.354     foxr     4549: # Returns:
                   4550: #    The 1/2 wrapped image and the stuff required to close the
                   4551: #    wrappage.  This allows e.g. randomlabel to insert more stuff
                   4552: #    into the closure.
1.353     foxr     4553: #
                   4554: sub align_latex_image {
                   4555:     my ($align, $latex_rendering, $image, $width, $height) = @_;
1.354     foxr     4556:     my $currentstring;        # The 1/2 wrapped image.
                   4557:     my $closure;              # The closure of the wrappage.
1.379     albertel 4558: 
                   4559:     # if it's none just return it back
                   4560:     if ($latex_rendering eq 'none') {
                   4561: 	return ($image,'');
                   4562:     }
                   4563: 
1.353     foxr     4564:     #    If there's an alignment specification we need to honor it here.
                   4565:     #    For the horizontal alignments, we will also honor the
                   4566:     #    value of the latex specfication.  The default is parbox,
                   4567:     #    and that's used for illegal values too.  
                   4568:     #    
                   4569:     #    Even though we set a default alignment value, the user
                   4570:     #    could have given us an illegal value.  In that case we
                   4571:     #    just use the default alignment of bottom..
1.354     foxr     4572:     $currentstring = "\n% figurewrapping \n";
1.353     foxr     4573:     if      ($align eq "top")    {
1.354     foxr     4574: 	$currentstring .= '\raisebox{-'.$height.'mm}{'.$image;
                   4575: 	$closure = '}';
1.353     foxr     4576:     } elsif (($align eq "center") || ($align eq "middle")) { # Being kind
                   4577: 	my $offset = $height/2;
1.354     foxr     4578: 	$currentstring .= '\raisebox{-'.$offset.'mm}{'.$image;
                   4579: 	$closure       = '}';
1.353     foxr     4580:     } elsif ($align eq "left")   { 
                   4581: 	if ($latex_rendering eq "parpic") { 
1.354     foxr     4582: 	    $currentstring .= '\parpic[l]{'.$image;
                   4583: 	    $closure       = '}';
1.353     foxr     4584: 	} elsif ($latex_rendering eq "parbox") {
1.354     foxr     4585: 	    $currentstring .= '\begin{minipage}[l]{'.$width.'mm}'
                   4586: 		.$image;
                   4587: 	    $closure = '\end{minipage}';
1.353     foxr     4588: 	} elsif ($latex_rendering eq "wrapfigure"
                   4589: 		 || $latex_rendering ne 'none') {  # wrapfig render
1.354     foxr     4590: 	    $currentstring .= 
1.353     foxr     4591: 		'\begin{wrapfigure}{l}{'.$width.'mm}'
1.354     foxr     4592: 		.'\scalebox{1.0}{'.$image;
                   4593: 	    $closure = '}\end{wrapfigure}';
1.353     foxr     4594: 	}
                   4595:     } elsif ($align eq "right")  {   
                   4596: 	if ($latex_rendering eq "parpic") {
1.354     foxr     4597: 	    $currentstring .= '\parpic[r]{'.$image;
                   4598: 	    $closure = '}';
1.353     foxr     4599: 	} elsif ($latex_rendering eq "parbox") {
1.354     foxr     4600: 	    $currentstring .=  '\begin{minipage}[r]{'.$width.'mm}'
                   4601: 		.$image;
                   4602: 	    $closure = '\end{minipage}';
1.353     foxr     4603: 	} elsif ($latex_rendering eq "wrapfigure"
                   4604: 		 || $latex_rendering ne 'none') {  # wrapfig render
1.354     foxr     4605: 	    $currentstring .= 
1.353     foxr     4606: 		'\begin{wrapfigure}{r}{'.$width.'mm}'
1.354     foxr     4607: 		.'\scalebox{1.0}{'.$image;
                   4608: 	    $closure = '}\end{wrapfigure}';
1.353     foxr     4609: 	}
                   4610:     } else {		# Bottom is also default.
                   4611: 	# $currentstring = '\raisebox{'.$height.'mm}{'.$image.'}';
1.354     foxr     4612: 	$currentstring .= "{$image";
                   4613: 	$closure       = '}';
1.353     foxr     4614:     }
1.354     foxr     4615:     $currentstring .= "\n% end wrappage\n";
                   4616:     $closure        = "\n% Begin closure\n".$closure."\n% End closure\n";
                   4617:     return ($currentstring, $closure);
1.353     foxr     4618: }
1.184     sakharuk 4619: 
1.287     foxr     4620: # is_inside_of $tagstack $tag
                   4621: #    This sub returns true if the current state of Xml processing
                   4622: #    is inside of the tag.   
                   4623: # Parameters:
                   4624: #     tagstack   - The tagstack from the parser.
                   4625: #     tag        - The tag (without the <>'s.).
                   4626: # Sample usage:
                   4627: #     if (is_inside_of($tagstack "table")) {
                   4628: #          # I'm in a table....
                   4629: #      }
                   4630: sub is_inside_of {
                   4631:     my ($tagstack, $tag) = @_;
                   4632:     my @stack = @$tagstack;
                   4633:     for (my $i = ($#stack - 1); $i >= 0; $i--) {
                   4634: 	if ($stack[$i] eq $tag) {
                   4635: 	    return 1;
                   4636: 	}
                   4637:     }
                   4638:     return 0;
                   4639: }
1.184     sakharuk 4640: 
1.94      sakharuk 4641: 
1.1       sakharuk 4642: 1;
                   4643: __END__

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