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

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

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