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

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

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