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

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

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