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

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

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